-
Notifications
You must be signed in to change notification settings - Fork 3
/
uni_client_test.go
125 lines (116 loc) · 3.81 KB
/
uni_client_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package wsrpc
import (
"context"
"crypto/tls"
"errors"
"testing"
"time"
"github.com/gorilla/websocket"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/smartcontractkit/wsrpc/internal/message"
"github.com/smartcontractkit/wsrpc/mocks"
)
func testRespMsg(t *testing.T, payload []byte) []byte {
resp, err := message.NewResponse(
"1",
&message.Response{Payload: payload},
nil)
require.NoError(t, err)
respBytes, err := MarshalProtoMessage(resp)
require.NoError(t, err)
return respBytes
}
func TestUniClient(t *testing.T) {
t.Run("success", func(t *testing.T) {
conn := new(mocks.Conn)
lggr := new(mocks.Logger)
uc := UniClientConn{
conn: conn,
lggr: lggr,
}
resp := message.Response{}
conn.On("WriteMessage", mock.Anything, mock.Anything).Return(nil).Once()
conn.On("ReadMessage").Return(websocket.BinaryMessage, testRespMsg(t, []byte("hello")), nil).Once()
err := uc.Invoke(context.Background(), "Method", &message.Request{}, &resp)
require.NoError(t, err)
assert.Equal(t, "hello", string(resp.Payload))
conn.AssertExpectations(t)
})
t.Run("reconnect on write", func(t *testing.T) {
conn := new(mocks.Conn)
lggr := new(mocks.Logger)
uc := UniClientConn{
conn: conn,
lggr: lggr,
}
resp := message.Response{}
uc.connector = func(ctx context.Context, target string, tlsConfig *tls.Config) (Conn, error) {
return conn, nil
}
conn.On("WriteMessage", mock.Anything, mock.Anything).Return(errors.New("oh no")).Once()
// Should cause a reconnect, so we write again
lggr.On("Warnf", mock.Anything, mock.Anything)
conn.On("WriteMessage", mock.Anything, mock.Anything).Return(nil).Once()
conn.On("ReadMessage").Return(websocket.BinaryMessage, testRespMsg(t, []byte("hello")), nil)
err := uc.Invoke(context.Background(), "Method", &message.Request{}, &resp)
require.NoError(t, err)
assert.Equal(t, "hello", string(resp.Payload))
conn.AssertExpectations(t)
})
t.Run("reconnect on read", func(t *testing.T) {
conn := new(mocks.Conn)
lggr := new(mocks.Logger)
uc := UniClientConn{
conn: conn,
lggr: lggr,
}
resp := new(message.Response)
uc.connector = func(ctx context.Context, target string, tlsConfig *tls.Config) (Conn, error) {
return conn, nil
}
conn.On("WriteMessage", mock.Anything, mock.Anything).Return(nil).Once()
conn.On("ReadMessage").Return(websocket.BinaryMessage, nil, errors.New("oh no")).Once()
// Should cause a reconnect, so we write again
lggr.On("Warnf", mock.Anything, mock.Anything)
conn.On("WriteMessage", mock.Anything, mock.Anything).Return(nil).Once()
conn.On("ReadMessage").Return(websocket.BinaryMessage, testRespMsg(t, []byte("hello")), nil).Once()
err := uc.Invoke(context.Background(), "Method", &message.Request{}, resp)
require.NoError(t, err)
assert.Equal(t, "hello", string(resp.Payload))
conn.AssertExpectations(t)
})
t.Run("cancel", func(t *testing.T) {
conn := new(mocks.Conn)
lggr := new(mocks.Logger)
uc := UniClientConn{
conn: conn,
lggr: lggr,
}
resp := new(message.Response)
req := new(message.Request)
// Connection always errors, but we can cancel
uc.connector = func(ctx context.Context, target string, tlsConfig *tls.Config) (Conn, error) {
return conn, errors.New("oh no")
}
conn.On("WriteMessage", mock.Anything, mock.Anything).Return(errors.New("oh no"))
lggr.On("Warnf", mock.Anything, mock.Anything)
ctx, cancel := context.WithCancel(context.Background())
done := make(chan struct{})
var err error
go func() {
err = uc.Invoke(ctx, "Method", req, resp)
done <- struct{}{}
}()
cancel()
select {
case <-done:
case <-time.After(1 * time.Second):
t.Error("failed to cancel")
}
require.Error(t, err)
assert.Contains(t, "context canceled", err.Error())
conn.AssertExpectations(t)
})
}