-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpacket_queue_test.go
116 lines (92 loc) · 2.99 KB
/
packet_queue_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
package sio
import (
"testing"
"time"
"github.com/karagenc/socket.io-go/engine.io/parser"
"github.com/karagenc/socket.io-go/internal/utils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestPacketQueue(t *testing.T) {
t.Run("add, get, and reset", func(t *testing.T) {
pq := newPacketQueue()
pq.add(mustCreateEIOPacket(parser.PacketTypeMessage, false, nil))
pq.add(mustCreateEIOPacket(parser.PacketTypePing, false, nil))
packets := pq.get()
require.Equal(t, 2, len(packets))
require.Equal(t, 0, len(pq.packets))
pq.add(mustCreateEIOPacket(parser.PacketTypeMessage, false, nil))
pq.add(mustCreateEIOPacket(parser.PacketTypePing, false, nil))
pq.reset()
require.Equal(t, 0, len(pq.packets))
})
t.Run("poll", func(t *testing.T) {
pq := newPacketQueue()
go func() {
pq.add(
mustCreateEIOPacket(parser.PacketTypeMessage, false, nil),
mustCreateEIOPacket(parser.PacketTypePing, false, nil),
)
}()
packets, ok, closed := pq.poll()
require.False(t, closed)
require.True(t, ok)
require.Equal(t, 2, len(packets))
// This time, synchronously add packets.
pq.add(
mustCreateEIOPacket(parser.PacketTypeMessage, false, nil),
mustCreateEIOPacket(parser.PacketTypePing, false, nil),
)
packets, ok, closed = pq.poll()
require.False(t, closed)
require.True(t, ok)
require.Equal(t, 2, len(packets))
pq.reset()
packets, ok, closed = pq.poll()
require.False(t, closed)
require.False(t, ok)
require.True(t, packets == nil)
// `alreadyDrained` should be true.
timedout := pq.waitForDrain(10000 * time.Second /* Forever */)
require.False(t, timedout)
pq.close()
packets, ok, closed = pq.poll()
require.True(t, closed)
require.False(t, ok)
require.True(t, packets == nil)
})
t.Run("waitForDrain", func(t *testing.T) {
pq := newPacketQueue()
// Artificially add packet to make `alreadyDrained` false.
pq.packets = append(pq.packets, mustCreateEIOPacket(parser.PacketTypeMessage, false, nil))
timedout := pq.waitForDrain(1 * time.Millisecond)
require.True(t, timedout)
})
t.Run("pollAndSend", func(t *testing.T) {
pq := newPacketQueue()
tw := utils.NewTestWaiter(1)
socket := utils.NewTestSocket("s1")
socket.SendFunc = func(packets ...*parser.Packet) {
assert.Equal(t, parser.PacketTypeMessage, packets[0].Type)
assert.Equal(t, parser.PacketTypePing, packets[1].Type)
assert.Equal(t, parser.PacketTypePong, packets[2].Type)
assert.Equal(t, parser.PacketTypeNoop, packets[3].Type)
tw.Done()
}
go pq.pollAndSend(socket)
pq.add(
mustCreateEIOPacket(parser.PacketTypeMessage, false, nil),
mustCreateEIOPacket(parser.PacketTypePing, false, nil),
mustCreateEIOPacket(parser.PacketTypePong, false, nil),
mustCreateEIOPacket(parser.PacketTypeNoop, false, nil),
)
tw.Wait()
})
}
func mustCreateEIOPacket(typ parser.PacketType, isBinary bool, data []byte) *parser.Packet {
packet, err := parser.NewPacket(typ, isBinary, data)
if err != nil {
panic(err)
}
return packet
}