Skip to content

Commit

Permalink
Add tests for timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
KSDaemon committed Nov 29, 2023
1 parent 2396637 commit f53dd84
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
2 changes: 1 addition & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1687,7 +1687,7 @@ func (c *Client) runHandleInvocation(msg *wamp.Invocation) {
case result = <-resChan:
// If the handler returns InvocationCanceled, this means the
// handler canceled the call.
if result.Err == wamp.ErrCanceled {
if result.Err == wamp.ErrCanceled || result.Err == wamp.ErrTimeout {
c.log.Println("INVOCATION", reqID, "canceled by callee")
isProcessing = false
break
Expand Down
67 changes: 66 additions & 1 deletion router/dealer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,10 @@ func TestCallTimeoutOnRouter(t *testing.T) {

// Test that callee received an INVOCATION message.
rsp = <-callee.Recv()
_, ok = rsp.(*wamp.Invocation)
invk, ok := rsp.(*wamp.Invocation)
require.True(t, ok, "expected INVOCATION")
_, ok = invk.Details[wamp.OptTimeout]
require.False(t, ok, "Invocation should not include timeout option")

select {
case <-time.After(time.Duration(callTimeout*2) * time.Millisecond):
Expand All @@ -306,6 +308,69 @@ func TestCallTimeoutOnRouter(t *testing.T) {
require.Equal(t, "call timeout", s, "Did not get error message from caller")
}

func TestCallTimeoutOnClient(t *testing.T) {
dealer, metaClient := newTestDealer(t)

callTimeout := 800

calleeRoles := wamp.Dict{
"roles": wamp.Dict{
"callee": wamp.Dict{
"features": wamp.Dict{
"call_canceling": true,
"call_timeout": true,
},
},
},
}

// Register a procedure.
callee := newTestPeer()
calleeSess := wamp.NewSession(callee, 0, nil, calleeRoles)
dealer.register(calleeSess,
&wamp.Register{Request: 123, Options: wamp.Dict{wamp.OptForwardTimeout: true}, Procedure: testProcedure})
rsp := <-callee.Recv()
_, ok := rsp.(*wamp.Registered)
require.True(t, ok, "did not receive REGISTERED response")

checkMetaReg(t, metaClient, calleeSess.ID)

caller := newTestPeer()
callerSession := wamp.NewSession(caller, 0, nil, nil)

// Test calling valid procedure
dealer.call(callerSession,
&wamp.Call{Request: 125, Options: wamp.Dict{wamp.OptTimeout: callTimeout}, Procedure: testProcedure})

// Test that callee received an INVOCATION message.
// Test that callee received an INVOCATION message.
rsp = <-callee.Recv()
invk, ok := rsp.(*wamp.Invocation)
require.True(t, ok, "expected INVOCATION")
timeOutOpt, ok := invk.Details[wamp.OptTimeout]
require.True(t, ok, "Invocation should include timeout option")
require.Equal(t, int64(callTimeout), timeOutOpt)

errMsg := &wamp.Error{
Type: wamp.INVOCATION,
Request: invk.Request,
Details: wamp.Dict{},
Error: wamp.ErrTimeout,
Arguments: wamp.List{"call timeout"},
}
dealer.error(errMsg)

rsp = <-caller.Recv()

// Check that caller receives the ERROR message.
rslt, ok := rsp.(*wamp.Error)
require.True(t, ok, "expected ERROR")
require.Equal(t, wamp.ErrTimeout, rslt.Error)
require.NotZero(t, len(rslt.Arguments), "expected response argument")
s, _ := wamp.AsString(rslt.Arguments[0])
require.Equal(t, "call timeout", s, "Did not get error message from caller")
}

func TestCancelCallModeKill(t *testing.T) {
dealer, metaClient := newTestDealer(t)

Expand Down

0 comments on commit f53dd84

Please sign in to comment.