/
websocket_client_mock.go
104 lines (93 loc) · 3 KB
/
websocket_client_mock.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
package wsclient
import (
"context"
"fmt"
"net/http"
"sync"
"github.com/gbdevw/gowse/wscengine/wsadapters"
"github.com/stretchr/testify/mock"
)
/*****************************************************************************/
/* WEBSOCKET CLIENT MOCK */
/*****************************************************************************/
// Mock for WebsocketClientInterface. Each callback has its own mock that is used when a callback is called.
type WebsocketClientMock struct {
mock.Mock
}
// Mocked OnOpen method
func (mock *WebsocketClientMock) OnOpen(
ctx context.Context,
resp *http.Response,
conn wsadapters.WebsocketConnectionAdapterInterface,
readMutex *sync.Mutex,
exit context.CancelFunc,
restarting bool) error {
// Call mocked method with provided args and return predefined return value if any
args := mock.Called(ctx, resp, conn, readMutex, exit, restarting)
return args.Error(0)
}
// Mocked OnMessage method
func (mock *WebsocketClientMock) OnMessage(
ctx context.Context,
conn wsadapters.WebsocketConnectionAdapterInterface,
readMutex *sync.Mutex,
restart context.CancelFunc,
exit context.CancelFunc,
sessionId string,
msgType wsadapters.MessageType,
msg []byte) {
// Call mocked method with provided args and return predefined return value if any
mock.Called(ctx, conn, readMutex, restart, exit, sessionId, msgType, msg)
}
// Mocked OnReadError method
func (mock *WebsocketClientMock) OnReadError(
ctx context.Context,
conn wsadapters.WebsocketConnectionAdapterInterface,
readMutex *sync.Mutex,
restart context.CancelFunc,
exit context.CancelFunc,
err error) {
// Call mocked method with provided args and return predefined return value if any
mock.Called(ctx, conn, readMutex, restart, exit, err)
}
// Mocked OnClose method
func (mock *WebsocketClientMock) OnClose(
ctx context.Context,
conn wsadapters.WebsocketConnectionAdapterInterface,
readMutex *sync.Mutex,
closeMessage *CloseMessageDetails) *CloseMessageDetails {
// Call mocked method with provided args and return predefined return value if any
args := mock.Called(ctx, conn, readMutex, closeMessage)
if args.Get(0) != nil {
// Type assertion
msg, ok := args.Get(0).(*CloseMessageDetails)
if ok {
return msg
} else {
panic(fmt.Sprintf("mocked OnClose returned value is not nil or a *CloseMessageDetails. Got %T, %v", msg, msg))
}
} else {
return nil
}
}
// Mocked OnCloseError method
func (mock *WebsocketClientMock) OnCloseError(
ctx context.Context,
err error) {
// Call mocked method with provided args and return predefined return value if any
mock.Called(ctx, err)
}
func (mock *WebsocketClientMock) OnRestartError(
ctx context.Context,
exit context.CancelFunc,
err error,
retryCount int) {
// Call mocked method with provided args and return predefined return value if any
mock.Called(ctx, exit, err, retryCount)
}
// Factory for WebsocketClientMock
func NewWebsocketClientMock() *WebsocketClientMock {
return &WebsocketClientMock{
Mock: mock.Mock{},
}
}