-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathclient_test.go
40 lines (34 loc) · 873 Bytes
/
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
package ctrader
import (
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
"github.com/diegobernardes/ctrader/openapi"
)
type mockClient struct {
mock.Mock
clientTransport
t *testing.T
count atomic.Int64
}
func (m *mockClient) send(payload []byte) error {
var msg openapi.ProtoMessage
require.NoError(m.t, proto.Unmarshal(payload, &msg))
require.Equal(m.t, msg.GetPayloadType(), uint32(openapi.ProtoPayloadType_HEARTBEAT_EVENT))
m.count.Add(1)
return nil
}
func TestClientKeepAlive(t *testing.T) {
t.Parallel()
mc := mockClient{t: t}
c := Client{transport: &mc}
c.keepalive()
time.Sleep(21 * time.Second)
require.Equal(t, int64(2), mc.count.Load())
c.stopSignal.Store(true)
time.Sleep(11 * time.Second)
require.Equal(t, int64(2), mc.count.Load())
}