Skip to content
This repository has been archived by the owner on May 1, 2020. It is now read-only.

Commit

Permalink
Add client disconnected error type
Browse files Browse the repository at this point in the history
Add client disconnected error type and add integration test cases
to test client.Request, client.Signal and client.RestoreSession
on disconnected client.
  • Loading branch information
romshark committed Mar 18, 2018
1 parent 323c76d commit 132698f
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 9 deletions.
2 changes: 1 addition & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func (clt *Client) Signal(name string, payload webwire.Payload) error {
defer clt.apiLock.RUnlock()

if atomic.LoadInt32(&clt.isConnected) < 1 {
return fmt.Errorf("Trying to send a signal on a disconnected client")
return DisconnectedErr{}
}

msgBytes := webwire.NewSignalMessage(name, payload)
Expand Down
8 changes: 8 additions & 0 deletions client/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package client

// DisconnectedErr is an error type indicating that the client isn't connected to the server
type DisconnectedErr struct{}

func (err DisconnectedErr) Error() string {
return "Client is disconnected"
}
5 changes: 1 addition & 4 deletions client/sendNamelessRequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ func (clt *Client) sendNamelessRequest(
timeout time.Duration,
) (webwire.Payload, error) {
if atomic.LoadInt32(&clt.isConnected) < 1 {
return webwire.Payload{}, webwire.ReqErr{
Code: "DISCONNECTED",
Message: "Trying to send a request on a disconnected socket",
}
return webwire.Payload{}, DisconnectedErr{}
}

request := clt.requestManager.Create(timeout)
Expand Down
5 changes: 1 addition & 4 deletions client/sendRequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ func (clt *Client) sendRequest(
timeout time.Duration,
) (webwire.Payload, error) {
if atomic.LoadInt32(&clt.isConnected) < 1 {
return webwire.Payload{}, webwire.ReqErr{
Code: "DISCONNECTED",
Message: "Trying to send a request on a disconnected socket",
}
return webwire.Payload{}, DisconnectedErr{}
}

request := clt.requestManager.Create(timeout)
Expand Down
36 changes: 36 additions & 0 deletions test/clientRestoreSessionDisconnected_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package test

import (
"reflect"
"testing"
"time"

wwr "github.com/qbeon/webwire-go"
wwrclt "github.com/qbeon/webwire-go/client"
)

// TestClientRestoreSessionDisconnected tests manual session restoration on disconnected client
func TestClientRestoreSessionDisconnected(t *testing.T) {
// Initialize webwire server
_, addr := setupServer(
t,
wwr.ServerOptions{},
)

// Initialize client
client := wwrclt.NewClient(
addr,
wwrclt.Options{
DefaultRequestTimeout: 2 * time.Second,
},
)

err := client.RestoreSession([]byte("somekey"))
if _, isDisconnErr := err.(wwrclt.DisconnectedErr); !isDisconnErr {
t.Fatalf(
"Expected disconnected error, got: %s | %s",
reflect.TypeOf(err),
err,
)
}
}
37 changes: 37 additions & 0 deletions test/clientSignalDisconnected_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package test

import (
"reflect"
"testing"
"time"

wwr "github.com/qbeon/webwire-go"
wwrclt "github.com/qbeon/webwire-go/client"
)

// TestClientSignalDisconnected tests sending signals on disconnected clients
func TestClientSignalDisconnected(t *testing.T) {
// Initialize webwire server given only the request
_, addr := setupServer(
t,
wwr.ServerOptions{},
)

// Initialize client
client := wwrclt.NewClient(
addr,
wwrclt.Options{
DefaultRequestTimeout: 2 * time.Second,
},
)

// Send request and await reply
err := client.Signal("", wwr.Payload{Data: []byte("testdata")})
if _, isDisconnErr := err.(wwrclt.DisconnectedErr); !isDisconnErr {
t.Fatalf(
"Expected disconnected error, got: %s | %s",
reflect.TypeOf(err),
err,
)
}
}
37 changes: 37 additions & 0 deletions test/cllientRequestDisconnected_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package test

import (
"reflect"
"testing"
"time"

wwr "github.com/qbeon/webwire-go"
wwrclt "github.com/qbeon/webwire-go/client"
)

// TestClientRequestDisconnected tests sending requests on disconnected clients
func TestClientRequestDisconnected(t *testing.T) {
// Initialize webwire server given only the request
_, addr := setupServer(
t,
wwr.ServerOptions{},
)

// Initialize client
client := wwrclt.NewClient(
addr,
wwrclt.Options{
DefaultRequestTimeout: 2 * time.Second,
},
)

// Send request and await reply
_, err := client.Request("", wwr.Payload{Data: []byte("testdata")})
if _, isDisconnErr := err.(wwrclt.DisconnectedErr); !isDisconnErr {
t.Fatalf(
"Expected disconnected error, got: %s | %s",
reflect.TypeOf(err),
err,
)
}
}

0 comments on commit 132698f

Please sign in to comment.