-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathclients_test.go
More file actions
124 lines (106 loc) · 3.3 KB
/
Copy pathclients_test.go
File metadata and controls
124 lines (106 loc) · 3.3 KB
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
package clients_test
import (
"os"
"testing"
"github.com/jcelliott/lumber"
"github.com/nanopack/mist/clients"
"github.com/nanopack/mist/server"
)
var (
testAddr = "127.0.0.1:2445"
testTag = "hello"
testMsg = "world"
)
// TestMain
func TestMain(m *testing.M) {
lumber.Level(lumber.LvlInt("fatal"))
server.StartTCP(testAddr, nil)
os.Exit(m.Run())
}
// TestTCPClientConnect tests to ensure a client can connect to a running server
func TestTCPClientConnect(t *testing.T) {
client, err := clients.New(testAddr, "")
if err != nil {
t.Fatalf("Client failed to connect - %s", err.Error())
t.FailNow()
}
defer client.Close()
if err := client.Ping(); err != nil {
t.Fatalf("ping failed")
}
if msg := <-client.Messages(); msg.Data != "pong" {
t.Fatalf("Unexpected data: Expecting 'pong' got %s", msg.Data)
}
client.Ping()
}
// TestBadTCPClientConnect tests to ensure a client can connect to a running server
func TestBadTCPClientConnect(t *testing.T) {
client, err := clients.New("321321321", "")
if err == nil {
t.Fatalf("Client succeeded to connect")
}
client, err = clients.New(testAddr, "hil")
if err != nil {
t.Fatalf("Client failed to connect - %s", err.Error())
t.FailNow()
}
defer client.Close()
if err := client.Ping(); err != nil {
t.Fatalf("ping failed")
}
if msg := <-client.Messages(); msg.Data != "pong" {
t.Fatalf("Unexpected data: Expecting 'pong' got %s", msg.Data)
}
client.Ping()
}
// TestTCPClient tests to ensure a client can run all of its expected commands;
// we don't have to actually test any of the results of the commands since those
// are already tested in other tests (proxy_test and subscriptions_test in the
// core package)
func TestTCPClient(t *testing.T) {
client, err := clients.New(testAddr, "")
if err != nil {
t.Fatalf("failed to connect - %s", err.Error())
t.FailNow()
}
defer client.Close()
// subscribe should fail with no tags
if err := client.Subscribe([]string{}); err == nil {
t.Fatalf("Subscription succeeded with missing tags!")
}
// test ability to subscribe
if err := client.Subscribe([]string{"a"}); err != nil {
t.Fatalf("client subscriptions failed %s", err.Error())
}
// test ability to list (subscriptions)
if err := client.List(); err != nil {
t.Fatalf("listing subscriptions failed %s", err.Error())
}
if msg := <-client.Messages(); msg.Data == "\"a\"" {
t.Fatalf("Failed to 'list' - '%s' '%#v'", msg.Error, msg.Data)
}
// test publish
if err := client.Publish([]string{"a"}, "testpublish"); err != nil {
t.Fatalf("publishing failed %s", err.Error())
}
if err := client.Publish([]string{}, "nopublish"); err == nil {
t.Fatalf("publishing no tags succeeded %s", err.Error())
}
if err := client.Publish([]string{"a"}, ""); err == nil {
t.Fatalf("publishing no data succeeded %s", err.Error())
}
// test ability to unsubscribe
if err := client.Unsubscribe([]string{"a"}); err != nil {
t.Fatalf("client unsubscriptions failed %s", err.Error())
}
if err := client.Unsubscribe([]string{}); err == nil {
t.Fatalf("client unsubscriptions no tags succeeded %s", err.Error())
}
// test ability to list (no subscriptions)
if err := client.List(); err != nil {
t.Fatalf("listing subscriptions failed %s", err.Error())
}
if msg := <-client.Messages(); msg.Data != "" {
t.Fatalf("Failed to 'list' - %s", msg.Error)
}
}