-
Notifications
You must be signed in to change notification settings - Fork 5
/
cluster_test.go
129 lines (115 loc) · 2.45 KB
/
cluster_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
117
118
119
120
121
122
123
124
125
126
127
128
129
package drax
import (
"fmt"
"io/ioutil"
"net"
"os"
"strconv"
"testing"
"time"
"github.com/docker/go-connections/sockets"
)
var clusterDialers = make(map[string]func(network, addr string) (net.Conn, error))
func TestNewCluster(t *testing.T) {
nodes, err := newTestCluster(3, "test")
if err != nil {
t.Fatal(err)
}
defer cleanupNodes(nodes)
timeout := time.After(10 * time.Second)
for _, n := range nodes {
select {
case <-timeout:
t.Fatal("timeout waiting for peers")
default:
}
peers, err := n.peers.Peers()
if err != nil {
t.Fatal(err)
}
if len(peers) == 2 {
break
}
}
}
func TestKVSingleNode(t *testing.T) {
nodes, err := newTestCluster(1, "test")
if err != nil {
t.Fatal(err)
}
defer cleanupNodes(nodes)
n := nodes[0]
kv := n.KVStore()
err = kv.Put("hello", []byte("world"), nil)
if err != nil {
t.Fatal(err)
}
kvPair, err := kv.Get("hello")
if string(kvPair.Value) != "world" {
t.Fatalf("exepcted value to be `world`, got %s", string(kvPair.Value))
}
}
func TestKVMultiNode(t *testing.T) {
nodes, err := newTestCluster(3, "test")
if err != nil {
t.Fatal(err)
}
defer cleanupNodes(nodes)
if err := nodes[0].KVStore().Put("hello", []byte("world"), nil); err != nil {
t.Fatal(err)
}
timeout := time.After(10 * time.Second)
ticker := time.NewTicker(1 * time.Second)
for _, n := range nodes {
for range ticker.C {
select {
case <-timeout:
t.Fatalf("timeout waiting for k/v pair on node %s", n.addr)
default:
}
kv, err := n.KVStore().Get("hello")
if err != nil {
continue
}
if string(kv.Value) == "world" {
break
}
}
}
}
func newTestCluster(size int, prefixAddr string) ([]*Cluster, error) {
var nodes []*Cluster
for i := 0; i < size; i++ {
addr := prefixAddr + strconv.Itoa(i)
home, err := ioutil.TempDir("", addr)
if err != nil {
return nil, err
}
l := sockets.NewInmemSocket(addr, 1)
clusterDialers[addr] = l.Dial
dialer := func(addr string, timeout time.Duration) (net.Conn, error) {
h, ok := clusterDialers[addr]
if !ok {
return nil, fmt.Errorf("unreachable")
}
return h("inmem", addr)
}
var peer string
if i > 0 {
peer = nodes[i-1].addr
}
c, err := New(l, dialer, home, addr, peer, nil)
if err != nil {
return nil, err
}
nodes = append(nodes, c)
}
return nodes, nil
}
func cleanupNodes(nodes []*Cluster) {
for _, n := range nodes {
os.RemoveAll(n.home)
n.Shutdown()
delete(clusterDialers, n.addr)
}
}