Skip to content

Commit

Permalink
Added Chris' tests
Browse files Browse the repository at this point in the history
  • Loading branch information
damienstamates committed Jan 25, 2019
1 parent 1d84fcd commit 626691a
Show file tree
Hide file tree
Showing 8 changed files with 214 additions and 250 deletions.
29 changes: 11 additions & 18 deletions client_test.go
Expand Up @@ -32,14 +32,10 @@ import (

func TestDial(t *testing.T) {
Convey("Given a mock dialer", t, func() {
readCount = 1
dialer := &mockDialer{}
logger := &testLogger{}
var c *Client
dialer := &mockDialerStruct{}
Convey("And connection is successful", func() {
connect = nil
Convey("When Dial is called with the mock dialer", func() {
c, _ = Dial(dialer, WithLogger(logger))
c, _ := Dial(dialer, WithLogger(dialer.logger))
Convey("Then c.Schema should not be nil", func() {
So(c.IsBroken(), ShouldBeFalse)
})
Expand All @@ -48,9 +44,9 @@ func TestDial(t *testing.T) {
})

Convey("And connection is unsuccessful", func() {
connect = errors.New("ERR")
dialer.connect = errors.New("ERR")
Convey("When Dial is called with the mock dialer", func() {
c, _ = Dial(dialer, WithLogger(logger))
c, _ := Dial(dialer, WithLogger(dialer.logger))
Convey("Then c.Schema should not be nil", func() {
So(c.IsBroken(), ShouldBeTrue)
})
Expand All @@ -61,13 +57,12 @@ func TestDial(t *testing.T) {
}

func TestDialWithWebSocket(t *testing.T) {
readCount = 1
tempNewWebSocketDialer := NewWebSocketDialer
defer func() {
NewWebSocketDialer = tempNewWebSocketDialer
gremconnect.GenUUID = uuid.NewUUID
}()
NewWebSocketDialer = func(string) gremconnect.Dialer { return &mockDialer{} }
NewWebSocketDialer = func(string) gremconnect.Dialer { return &mockDialerStruct{} }
gremconnect.GenUUID = func() (uuid.UUID, error) {
var a [16]byte
copy(a[:], "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa")
Expand All @@ -76,7 +71,6 @@ func TestDialWithWebSocket(t *testing.T) {
Convey("Given a host string and error channel", t, func() {
host := ""
Convey("And connection is successful", func() {
connect = nil
Convey("When NewClient is called", func() {
c, _ := DialWithWebSocket(host)
Convey("Then client shouldn't be nil", func() {
Expand All @@ -89,21 +83,20 @@ func TestDialWithWebSocket(t *testing.T) {

func TestSetLogger(t *testing.T) {
Convey("Given a client and a logger", t, func() {
dialer := &mockDialer{}
dialer := &mockDialerStruct{}
c, _ := Dial(dialer)
var l testLogger
Convey("and SetLogger is called", func() {
c.SetLogger(l)
c.SetLogger(dialer.logger)
Convey("Then the client logger should resemble l", func() {
So(c.logger, ShouldResemble, l)
So(c.logger, ShouldResemble, dialer.logger)
})
})
})
}

func TestIsBroken(t *testing.T) {
Convey("Given a client", t, func() {
dialer := &mockDialer{}
dialer := &mockDialerStruct{}
c, _ := Dial(dialer)
c.broken = false
Convey("And IsBroken is called", func() {
Expand All @@ -117,7 +110,7 @@ func TestIsBroken(t *testing.T) {

func TestAddress(t *testing.T) {
Convey("Given a client", t, func() {
dialer := &mockDialer{}
dialer := &mockDialerStruct{}
c, _ := Dial(dialer)
Convey("And Address is called", func() {
a := c.Address()
Expand All @@ -130,7 +123,7 @@ func TestAddress(t *testing.T) {

func TestAuth(t *testing.T) {
Convey("Given a client", t, func() {
dialer := &mockDialer{}
dialer := &mockDialerStruct{}
c, _ := Dial(dialer)
Convey("And Auth is called", func() {
auth, _ := c.Auth()
Expand Down
45 changes: 22 additions & 23 deletions configuration_test.go
Expand Up @@ -31,7 +31,7 @@ import (
func TestWithErrorChannel(t *testing.T) {
Convey("Given an error channel and dialer", t, func() {
var errs chan error
dialer := &mockDialer{}
dialer := &mockDialerStruct{}
Convey("When Dial is called with error channel", func() {
c, _ := Dial(dialer, WithErrorChannel(errs))
Convey("Then the client error channel should be set", func() {
Expand All @@ -43,12 +43,11 @@ func TestWithErrorChannel(t *testing.T) {

func TestWithLogger(t *testing.T) {
Convey("Given a logger and dialer", t, func() {
var l testLogger
dialer := &mockDialer{}
dialer := &mockDialerStruct{}
Convey("When Dial is called with logger", func() {
c, _ := Dial(dialer, WithLogger(l))
c, _ := mockDial(dialer, WithLogger(dialer.logger))
Convey("Then the client logger should be set", func() {
So(c.logger, ShouldResemble, l)
So(c.logger, ShouldResemble, dialer.logger)
})
})
})
Expand All @@ -57,9 +56,9 @@ func TestWithLogger(t *testing.T) {
func TestWithGremlinVersion(t *testing.T) {
Convey("Given a Gremlin version and dialer", t, func() {
v := 3
dialer := &mockDialer{}
dialer := &mockDialerStruct{}
Convey("When Dial is called with Gremlin Version", func() {
c, _ := Dial(dialer, WithGremlinVersion(v))
c, _ := mockDial(dialer, WithGremlinVersion(v))
Convey("Then the client Gremlin version should be set", func() {
So(c.gremlinVersion, ShouldEqual, strconv.Itoa(v))
})
Expand All @@ -70,9 +69,9 @@ func TestWithGremlinVersion(t *testing.T) {
func TestWithMaxConcurrentMessages(t *testing.T) {
Convey("Given an int and dialer", t, func() {
m := 2
dialer := &mockDialer{}
dialer := &mockDialerStruct{}
Convey("When Dial is called with max concurrent messages", func() {
c, _ := Dial(dialer, WithMaxConcurrentMessages(m))
c, _ := mockDial(dialer, WithMaxConcurrentMessages(m))
Convey("Then the client request channel should be set", func() {
So(c.request, ShouldNotBeNil)
})
Expand All @@ -84,9 +83,9 @@ func TestWithAuthUserPass(t *testing.T) {
Convey("Given a username, password and dialer", t, func() {
user := "testuser"
pass := "testpass"
dialer := &mockDialer{}
dialer := &mockDialerStruct{}
Convey("And Dial is called with username and password", func() {
_, err := Dial(dialer, WithAuthUserPass(user, pass))
_, err := mockDial(dialer, WithAuthUserPass(user, pass))
Convey("Then no error should be encountered", func() {
So(err, ShouldBeNil)
})
Expand All @@ -96,10 +95,10 @@ func TestWithAuthUserPass(t *testing.T) {

func TestWithTimeout(t *testing.T) {
Convey("Given a timeout and dialer", t, func() {
t := 5 * time.Second
dialer := &mockDialer{}
dialer := &mockDialerStruct{}
dialer.timeout = 5 * time.Second
Convey("And Dial is called with timeout", func() {
_, err := Dial(dialer, WithTimeout(t))
_, err := mockDial(dialer, WithTimeout(dialer.timeout))
Convey("Then no error should be encountered", func() {
So(err, ShouldBeNil)
})
Expand All @@ -109,10 +108,10 @@ func TestWithTimeout(t *testing.T) {

func TestWithPingInterval(t *testing.T) {
Convey("Given a ping interval and dialer", t, func() {
p := 5 * time.Second
dialer := &mockDialer{}
dialer := &mockDialerStruct{}
dialer.pingInterval = 5 * time.Second
Convey("And Dial is called with ping interval", func() {
_, err := Dial(dialer, WithPingInterval(p))
_, err := mockDial(dialer, WithPingInterval(dialer.pingInterval))
Convey("Then no error should be encountered", func() {
So(err, ShouldBeNil)
})
Expand All @@ -122,10 +121,10 @@ func TestWithPingInterval(t *testing.T) {

func TestWithWritingWait(t *testing.T) {
Convey("Given a writing wait and dialer", t, func() {
w := 5 * time.Second
dialer := &mockDialer{}
dialer := &mockDialerStruct{}
dialer.writingWait = 5 * time.Second
Convey("And Dial is called with writing wait", func() {
_, err := Dial(dialer, WithWritingWait(w))
_, err := mockDial(dialer, WithWritingWait(dialer.writingWait))
Convey("Then no error should be encountered", func() {
So(err, ShouldBeNil)
})
Expand All @@ -135,10 +134,10 @@ func TestWithWritingWait(t *testing.T) {

func TestWithReadingWait(t *testing.T) {
Convey("Given a reading wait and dialer", t, func() {
r := 5 * time.Second
dialer := &mockDialer{}
dialer := &mockDialerStruct{}
dialer.readingWait = 5 * time.Second
Convey("And Dial is called with reading wait", func() {
_, err := Dial(dialer, WithReadingWait(r))
_, err := mockDial(dialer, WithReadingWait(dialer.readingWait))
Convey("Then no error should be encountered", func() {
So(err, ShouldBeNil)
})
Expand Down
47 changes: 21 additions & 26 deletions connection_test.go
Expand Up @@ -31,7 +31,6 @@ import (
)

func TestLaunchConnection(t *testing.T) {
readCount = 1
defer func() {
gremconnect.GenUUID = uuid.NewUUID
}()
Expand All @@ -41,10 +40,10 @@ func TestLaunchConnection(t *testing.T) {
return uuid.UUID(a), nil
}

Convey("Given an error channel and a client", t, func() {
connect = nil
dialer := &mockDialer{}
c, _ := Dial(dialer)
Convey("Given a client", t, func() {
dialer := &mockDialerStruct{}
dialer.connect = nil
c, _ := mockDial(dialer)
Convey("and launchConnection() is called", func() {
err := c.launchConnection()
Convey("Then the err should be nil", func() {
Expand All @@ -55,7 +54,6 @@ func TestLaunchConnection(t *testing.T) {
}

func TestClose(t *testing.T) {
readCount = 1
defer func() {
gremconnect.GenUUID = uuid.NewUUID
}()
Expand All @@ -65,9 +63,9 @@ func TestClose(t *testing.T) {
return uuid.UUID(a), nil
}

Convey("Given an error channel and a client", t, func() {
connect = nil
dialer := &mockDialer{}
Convey("Given a client", t, func() {
dialer := &mockDialerStruct{}
dialer.connect = nil
c, _ := Dial(dialer)
Convey("Then no errors or panics should be thrown when Close() is called", func() {
c.Close()
Expand All @@ -76,7 +74,6 @@ func TestClose(t *testing.T) {
}

func TestIsConnected(t *testing.T) {
readCount = 1
defer func() {
gremconnect.GenUUID = uuid.NewUUID
}()
Expand All @@ -86,22 +83,23 @@ func TestIsConnected(t *testing.T) {
copy(a[:], "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa")
return uuid.UUID(a), nil
}
Convey("Given an error channel and a client", t, func() {
dialer := &mockDialer{}
Convey("Given a client", t, func() {
dialer := &mockDialerStruct{}
dialer.connect = nil
c, _ := Dial(dialer)
Convey("When IsConnected() is called", func() {
connection := c.IsConnected()
Convey("Then the return value should match the isConnected var", func() {
So(connection, ShouldEqual, isConnected)
So(connection, ShouldEqual, dialer.isConnected)
})
})
})
}

func TestRedial(t *testing.T) {
Convey("Given a client", t, func() {
dialer := &mockDialer{}
c, _ := Dial(dialer)
dialer := &mockDialerStruct{}
c, _ := mockDial(dialer)
Convey("When Redial is called", func() {
err := c.Redial(dialer)
Convey("Then the error should be nil", func() {
Expand All @@ -116,16 +114,16 @@ func TestConnect(t *testing.T) {
defer func() {
NewWebSocketDialer = tempNewWebSocketDialer
}()
NewWebSocketDialer = func(string) gremconnect.Dialer { return &mockDialer{} }
NewWebSocketDialer = func(string) gremconnect.Dialer { return &mockDialerStruct{} }
Convey("Given a client", t, func() {
dialer := &mockDialer{}
c, _ := Dial(dialer)
dialer := &mockDialerStruct{}
c, _ := mockDial(dialer)
c.conn = dialer
Convey("And Connect is called with the client", func() {
isDisposed = true
dialer.isDisposed = true
err := c.Connect()
Convey("Then no error should be returned", func() {
So(err, ShouldBeNil)
isDisposed = false
})
})
})
Expand All @@ -144,16 +142,13 @@ func TestConnectNoConnection(t *testing.T) {
}

func TestConnectErrorLaunchingConnection(t *testing.T) {
defer func() {
isDisposed = false
}()
Convey("Given a client", t, func() {
dialer := &mockDialer{}
dialer := &mockDialerStruct{}
c := setupClient()
c.conn = dialer
Convey("When Connect is called and launching the connection throws an error", func() {
isDisposed = true
connect = errors.New("ERROR")
dialer.isDisposed = true
dialer.connect = errors.New("ERROR")
err := c.Connect()
Convey("Then the error should be returned", func() {
So(err, ShouldNotBeNil)
Expand Down
4 changes: 1 addition & 3 deletions go.mod
@@ -1,12 +1,10 @@
module github.com/northwesternmutual/grammes

require (
git.nmlv.nml.com/trams/grammes v1.0.3
github.com/google/uuid v1.1.0
github.com/gorilla/websocket v1.4.0
github.com/jtolds/gls v4.2.1+incompatible // indirect
github.com/smartystreets/assertions v0.0.0-20190116191733-b6c0e53d7304 // indirect
github.com/smartystreets/goconvey v0.0.0-20181108003508-044398e4856c
go.uber.org/atomic v1.3.2 // indirect
go.uber.org/multierr v1.1.0 // indirect
go.uber.org/zap v1.9.1
)
2 changes: 2 additions & 0 deletions go.sum
@@ -1,3 +1,5 @@
git.nmlv.nml.com/trams/grammes v1.0.3 h1:MOCph7Igj6uUdtD8yiNzvXHYhL1+EVs1H30dkFp6VYM=
git.nmlv.nml.com/trams/grammes v1.0.3/go.mod h1:Eaybx/U9tj98QixLGpW1yLuMCXLPKzcUsrh7Q/woVC0=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/uuid v1.1.0 h1:Jf4mxPC/ziBnoPIdpQdPJ9OeiomAUHLvxmPRSPH9m4s=
Expand Down

0 comments on commit 626691a

Please sign in to comment.