-
Notifications
You must be signed in to change notification settings - Fork 18
/
rolodex_client_test.go
62 lines (48 loc) · 1.25 KB
/
rolodex_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
61
62
package meshboi
import (
"bytes"
"net"
"testing"
"time"
"inet.af/netaddr"
)
func TestNetworkCallback(t *testing.T) {
var nmap NetworkMap
called := 0
callback := func(member NetworkMap) {
called += 1
nmap = member
}
client, server := net.Pipe()
rolloClient := NewRolodexClient("testNet", client, time.Second, callback)
go rolloClient.Run()
defer rolloClient.Stop()
server.Write([]byte(`{ "addresses": ["192.168.4.1:2000"], "your_index": 0 }`))
time.Sleep(time.Millisecond)
if len(nmap.Addresses) != 1 {
t.Fatalf("expected 1 address but got %v", len(nmap.Addresses))
}
if called != 1 {
t.Fatalf("Called more than once")
}
if nmap.Addresses[0] != netaddr.MustParseIPPort("192.168.4.1:2000") {
t.Fatalf("Wrong ip address back")
}
if nmap.YourIndex != 0 {
t.Fatalf("Wrong index back")
}
}
func TestClientSendsHeartBeat(t *testing.T) {
callback := func(member NetworkMap) {
}
client, server := net.Pipe()
rolloClient := NewRolodexClient("testNet", client, time.Millisecond, callback)
go rolloClient.Run()
defer rolloClient.Stop()
time.Sleep(time.Millisecond)
b := make([]byte, 1000)
n, _ := server.Read(b)
if !bytes.Contains(b[:n], []byte("testNet")) {
t.Fatalf("Didn't contain the network name %v", string(b[:n]))
}
}