-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
60 lines (49 loc) · 1.22 KB
/
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
package tt
import (
"context"
"testing"
"time"
"github.com/gregoryv/mq"
"github.com/gregoryv/tt/event"
)
func TestClient(t *testing.T) {
client := NewClient()
client.SetServer("tcp://localhost:1883")
ctx := context.Background()
ctx, _ = context.WithTimeout(ctx, time.Millisecond)
go client.Run(ctx)
for v := range client.Events() {
switch v := v.(type) {
case event.ClientUp:
_ = client.Send(ctx, mq.NewConnect())
case event.ClientConnect:
// do something once you are connected
p := mq.Pub(0, "gopher/happy", "yes")
err := client.Send(ctx, p)
if err != nil {
t.Fatal(err)
}
case *mq.Publish:
_ = v // do something the received packet
case event.ClientStop:
// do some clean up maybe
}
}
}
func Test_iDPool_nextTimeout(t *testing.T) {
pool := newIDPool(1) // 1 .. 5
ctx, _ := context.WithTimeout(context.Background(), time.Millisecond)
pool.next(ctx)
if v, _ := pool.next(ctx); v != 0 {
t.Error("expect 0 id when pool is cancelled", v)
}
}
func Test_iDPool_reuse(t *testing.T) {
pool := newIDPool(3)
if v := pool.reuse(99); v != 0 {
t.Error("pool.reuse accepted value > max")
}
if v := pool.reuse(2); v != 0 {
t.Error("pool.reuse accepted value that hasn't been used")
}
}