Skip to content

Commit 894ec29

Browse files
authored
Added websocket related client config, fixed tests (#5)
This commit adds several websocket client configuration params, namely: - WithWriteBufferSize - WithReadBufferSize - WithHandshakeTimeout - WithCompression It adds relevant tests, fixes previous broken tests, some typos and warnings.
1 parent 1c0edcc commit 894ec29

File tree

13 files changed

+220
-86
lines changed

13 files changed

+220
-86
lines changed

configuration.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,34 @@ func WithRequestTimeout(interval time.Duration) ClientConfiguration {
110110
c.requestTimeout = interval
111111
}
112112
}
113+
114+
// WithWriteBufferSize sets the max write buffer size
115+
// for the websocket frame
116+
func WithWriteBufferSize(writeBufferSize int) ClientConfiguration {
117+
return func(c *Client) {
118+
c.conn.SetWriteBufferSize(writeBufferSize)
119+
}
120+
}
121+
122+
// WithReadBufferSize sets the max read buffer size
123+
// for the websocket frame
124+
func WithReadBufferSize(readBufferSize int) ClientConfiguration {
125+
return func(c *Client) {
126+
c.conn.SetReadBufferSize(readBufferSize)
127+
}
128+
}
129+
130+
// WithHandshakeTimeout sets the websocket handshake timeout
131+
func WithHandshakeTimeout(handshakeTimeout time.Duration) ClientConfiguration {
132+
return func(c *Client) {
133+
c.conn.SetHandshakeTimeout(handshakeTimeout)
134+
}
135+
}
136+
137+
// WithCompression sets the compression
138+
// flag for websocket connections
139+
func WithCompression(enableCompression bool) ClientConfiguration {
140+
return func(c *Client) {
141+
c.conn.SetCompression(enableCompression)
142+
}
143+
}

gremconnect/dialer.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ type Dialer interface {
5050
SetPingInterval(interval time.Duration)
5151
SetWritingWait(interval time.Duration)
5252
SetReadingWait(interval time.Duration)
53+
SetWriteBufferSize(writeBufferSize int)
54+
SetReadBufferSize(readBufferSize int)
55+
SetHandshakeTimeout(handshakeTimeout time.Duration)
56+
SetCompression(enableCompression bool)
5357
}
5458

5559
// NewWebSocketDialer returns a new WebSocket dialer to use when
@@ -58,12 +62,16 @@ type Dialer interface {
5862
// if they're not assigned by DialerConfig functions.
5963
func NewWebSocketDialer(address string) Dialer {
6064
return &WebSocket{
61-
timeout: 5 * time.Second,
62-
pingInterval: 60 * time.Second,
63-
writingWait: 15 * time.Second,
64-
readingWait: 15 * time.Second,
65-
connected: false,
66-
address: address,
67-
Quit: make(chan struct{}),
65+
timeout: 5 * time.Second,
66+
pingInterval: 60 * time.Second,
67+
writingWait: 15 * time.Second,
68+
readingWait: 15 * time.Second,
69+
handshakeTimeout: 5 * time.Second,
70+
writeBufferSize: 50 * 1024,
71+
readBufferSize: 50 * 1024,
72+
enableCompression: false,
73+
connected: false,
74+
address: address,
75+
Quit: make(chan struct{}),
6876
}
6977
}

gremconnect/websocket.go

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,20 @@ import (
3434
// to dial to the gremlin server and sustain
3535
// a stable connection by pinging it regularly.
3636
type WebSocket struct {
37-
address string
38-
conn *websocket.Conn
39-
auth *Auth
40-
disposed bool
41-
connected bool
42-
pingInterval time.Duration
43-
writingWait time.Duration
44-
readingWait time.Duration
45-
timeout time.Duration
46-
Quit chan struct{}
37+
address string
38+
conn *websocket.Conn
39+
auth *Auth
40+
disposed bool
41+
connected bool
42+
enableCompression bool
43+
pingInterval time.Duration
44+
writingWait time.Duration
45+
readingWait time.Duration
46+
timeout time.Duration
47+
handshakeTimeout time.Duration
48+
writeBufferSize int
49+
readBufferSize int
50+
Quit chan struct{}
4751

4852
sync.RWMutex
4953
}
@@ -54,9 +58,10 @@ type WebSocket struct {
5458
func (ws *WebSocket) Connect() error {
5559
var err error
5660
dialer := websocket.Dialer{
57-
WriteBufferSize: 1024 * 50, // Set up for large messages.
58-
ReadBufferSize: 1024 * 50, // Set up for large messages.
59-
HandshakeTimeout: 5 * time.Second,
61+
WriteBufferSize: ws.writeBufferSize,
62+
ReadBufferSize: ws.readBufferSize,
63+
HandshakeTimeout: ws.handshakeTimeout,
64+
EnableCompression: ws.enableCompression,
6065
}
6166

6267
// Check if the host address already has the proper
@@ -174,7 +179,7 @@ func (ws *WebSocket) Ping(errs chan error) {
174179
}
175180
}
176181

177-
// Configration functions
182+
// Configuration functions
178183

179184
// SetAuth will set the authentication to this user and pass
180185
func (ws *WebSocket) SetAuth(user, pass string) {
@@ -200,3 +205,19 @@ func (ws *WebSocket) SetWritingWait(interval time.Duration) {
200205
func (ws *WebSocket) SetReadingWait(interval time.Duration) {
201206
ws.readingWait = interval
202207
}
208+
209+
func (ws *WebSocket) SetWriteBufferSize(writeBufferSize int) {
210+
ws.writeBufferSize = writeBufferSize
211+
}
212+
213+
func (ws *WebSocket) SetReadBufferSize(readBufferSize int) {
214+
ws.readBufferSize = readBufferSize
215+
}
216+
217+
func (ws *WebSocket) SetHandshakeTimeout(handshakeTimeout time.Duration) {
218+
ws.handshakeTimeout = handshakeTimeout
219+
}
220+
221+
func (ws *WebSocket) SetCompression(enableCompression bool) {
222+
ws.enableCompression = enableCompression
223+
}

gremconnect/websocket_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,3 +343,55 @@ func TestSetReadingWait(t *testing.T) {
343343
})
344344
})
345345
}
346+
347+
func TestSetWriteBufferSize(t *testing.T) {
348+
Convey("Given a WebSocket and a write buffer size", t, func() {
349+
dialer := &WebSocket{}
350+
writeBufferSize := 512 * 1024
351+
Convey("And SetWriteBufferSize is called", func() {
352+
dialer.SetWriteBufferSize(writeBufferSize)
353+
Convey("Then the write buffer size should be set in the dialer", func() {
354+
So(dialer.writeBufferSize, ShouldEqual, writeBufferSize)
355+
})
356+
})
357+
})
358+
}
359+
360+
func TestSetReadBufferSize(t *testing.T) {
361+
Convey("Given a WebSocket and a read buffer size", t, func() {
362+
dialer := &WebSocket{}
363+
readBufferSize := 256 * 1024
364+
Convey("And SetReadBufferSize is called", func() {
365+
dialer.SetReadBufferSize(readBufferSize)
366+
Convey("Then the read buffer size should be set in the dialer", func() {
367+
So(dialer.readBufferSize, ShouldEqual, readBufferSize)
368+
})
369+
})
370+
})
371+
}
372+
373+
func TestHandshakeTimeout(t *testing.T) {
374+
Convey("Given a WebSocket and a handshake timeout", t, func() {
375+
dialer := &WebSocket{}
376+
handshakeTimeout := time.Second
377+
Convey("And SetHandshakeTimeout is called", func() {
378+
dialer.SetHandshakeTimeout(handshakeTimeout)
379+
Convey("Then the handshake timeout should be set in the dialer", func() {
380+
So(dialer.handshakeTimeout, ShouldEqual, handshakeTimeout)
381+
})
382+
})
383+
})
384+
}
385+
386+
func TestCompression(t *testing.T) {
387+
Convey("Given a WebSocket and a compression flag", t, func() {
388+
dialer := &WebSocket{}
389+
enableCompression := true
390+
Convey("And SetCompression is called", func() {
391+
dialer.SetCompression(enableCompression)
392+
Convey("Then the compression flag should be set in the dialer", func() {
393+
So(dialer.enableCompression, ShouldEqual, enableCompression)
394+
})
395+
})
396+
})
397+
}

manager/query_test.go

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,30 +24,35 @@ import (
2424
"testing"
2525
"time"
2626

27+
. "github.com/smartystreets/goconvey/convey"
28+
2729
"github.com/northwesternmutual/grammes/gremconnect"
2830
"github.com/northwesternmutual/grammes/logging"
29-
. "github.com/smartystreets/goconvey/convey"
3031
)
3132

3233
// MOCKDIALER
3334

3435
type mockDialer gremconnect.WebSocket
3536

36-
func (*mockDialer) Connect() error { return connect }
37-
func (*mockDialer) Close() error { return nil }
38-
func (*mockDialer) Write([]byte) error { return nil }
39-
func (m *mockDialer) Read() ([]byte, error) { return []byte(response), nil }
40-
func (*mockDialer) Ping(chan error) {}
41-
func (*mockDialer) IsConnected() bool { return isConnected }
42-
func (*mockDialer) IsDisposed() bool { return isDisposed }
43-
func (*mockDialer) Auth() (*gremconnect.Auth, error) { return &gremconnect.Auth{}, nil }
44-
func (*mockDialer) Address() string { return "" }
45-
func (m *mockDialer) GetQuit() chan struct{} { return make(chan struct{}) }
46-
func (*mockDialer) SetAuth(string, string) {}
47-
func (*mockDialer) SetTimeout(time.Duration) {}
48-
func (*mockDialer) SetPingInterval(time.Duration) {}
49-
func (*mockDialer) SetWritingWait(time.Duration) {}
50-
func (*mockDialer) SetReadingWait(time.Duration) {}
37+
func (*mockDialer) Connect() error { return connect }
38+
func (*mockDialer) Close() error { return nil }
39+
func (*mockDialer) Write([]byte) error { return nil }
40+
func (m *mockDialer) Read() ([]byte, error) { return []byte(response), nil }
41+
func (*mockDialer) Ping(chan error) {}
42+
func (*mockDialer) IsConnected() bool { return isConnected }
43+
func (*mockDialer) IsDisposed() bool { return isDisposed }
44+
func (*mockDialer) Auth() (*gremconnect.Auth, error) { return &gremconnect.Auth{}, nil }
45+
func (*mockDialer) Address() string { return "" }
46+
func (m *mockDialer) GetQuit() chan struct{} { return make(chan struct{}) }
47+
func (*mockDialer) SetAuth(string, string) {}
48+
func (*mockDialer) SetTimeout(time.Duration) {}
49+
func (*mockDialer) SetPingInterval(time.Duration) {}
50+
func (*mockDialer) SetWritingWait(time.Duration) {}
51+
func (*mockDialer) SetReadingWait(time.Duration) {}
52+
func (*mockDialer) SetWriteBufferSize(int) {}
53+
func (*mockDialer) SetReadBufferSize(int) {}
54+
func (*mockDialer) SetHandshakeTimeout(time.Duration) {}
55+
func (*mockDialer) SetCompression(bool) {}
5156

5257
func TestSetLoggerQM(t *testing.T) {
5358
Convey("Given a dialer, string executor and query manager", t, func() {

query/traversal/addproperty_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ package traversal
2323
import (
2424
"testing"
2525

26-
"github.com/northwesternmutual/grammes/query/cardinality"
2726
. "github.com/smartystreets/goconvey/convey"
27+
28+
"github.com/northwesternmutual/grammes/query/cardinality"
2829
)
2930

3031
func TestProperty(t *testing.T) {
@@ -45,7 +46,7 @@ func TestProperty(t *testing.T) {
4546
Convey("When 'Property' is called with object strings and ints", func() {
4647
result := g.Property(3, 4, 4)
4748
Convey("Then result should equal 'g.property(3,4,4)'", func() {
48-
So(result.String(), ShouldEqual, "g.property(3,4,4)")
49+
So(result.String(), ShouldEqual, "g.property(3l,4l,4l)")
4950
})
5051
})
5152

query/traversal/pagerank_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func TestPageRank(t *testing.T) {
3939
Convey("When 'PageRank' is called with one argument", func() {
4040
result := g.PageRank(1.234)
4141
Convey("Then result should equal 'g.pageRank(1.234)'", func() {
42-
So(result.String(), ShouldEqual, "g.pageRank(1.234)")
42+
So(result.String(), ShouldEqual, "g.pageRank(1.234f)")
4343
})
4444
})
4545

query/traversal/skip_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ package traversal
2323
import (
2424
"testing"
2525

26-
"github.com/northwesternmutual/grammes/query/scope"
2726
. "github.com/smartystreets/goconvey/convey"
27+
28+
"github.com/northwesternmutual/grammes/query/scope"
2829
)
2930

3031
func TestSkip(t *testing.T) {
@@ -40,7 +41,7 @@ func TestSkip(t *testing.T) {
4041
Convey("When 'Skip' is called with multiple extraFloat arguments", func() {
4142
result := g.Skip(scope.Local, 1.234)
4243
Convey("Then result should equal 'g.skip(local,1.234)'", func() {
43-
So(result.String(), ShouldEqual, "g.skip(local,1.234)")
44+
So(result.String(), ShouldEqual, "g.skip(local,1.234f)")
4445
})
4546
})
4647
})

query/traversal/timeLimit_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func TestTimeLimit(t *testing.T) {
3232
Convey("When 'Store' is called", func() {
3333
result := g.TimeLimit(1.234)
3434
Convey("Then result should equal 'g.timeLimit(1.234)'", func() {
35-
So(result.String(), ShouldEqual, "g.timeLimit(1.234)")
35+
So(result.String(), ShouldEqual, "g.timeLimit(1.234f)")
3636
})
3737
})
3838
})

query/traversal/util_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func TestAddStep(t *testing.T) {
6565
i := float64(1.234)
6666
g.AddStep("test", i)
6767
Convey("Then g should equal g.test(1.234)", func() {
68-
So(g.String(), ShouldEqual, "g.test(1.234)")
68+
So(g.String(), ShouldEqual, "g.test(1.234f)")
6969
})
7070
})
7171

@@ -81,7 +81,7 @@ func TestAddStep(t *testing.T) {
8181
s := `a 'test' with \ and \\ and nested {"key": "value", "key2": "val\"val"} and {'key': 'value', 'kay2': 'val\'val'}`
8282
g.AddStep("test", s)
8383
expected := `g.test('a \'test\' with \\ and \\\\ and nested {"key": "value", "key2": "val\\"val"} and {\'key\': \'value\', \'kay2\': \'val\\\'val\'}')`
84-
Convey("Then g should equal " + expected, func() {
84+
Convey("Then g should equal "+expected, func() {
8585
So(g.String(), ShouldEqual, expected)
8686
})
8787
})

0 commit comments

Comments
 (0)