File: util_test.go

package info (click to toggle)
golang-github-hashicorp-raft 0.0~git20160317.0.3359516-1~bpo8+1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 428 kB
  • sloc: makefile: 16
file content (187 lines) | stat: -rw-r--r-- 3,646 bytes parent folder | download | duplicates (3)
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package raft

import (
	"reflect"
	"regexp"
	"testing"
	"time"
)

func TestRandomTimeout(t *testing.T) {
	start := time.Now()
	timeout := randomTimeout(time.Millisecond)

	select {
	case <-timeout:
		diff := time.Now().Sub(start)
		if diff < time.Millisecond {
			t.Fatalf("fired early")
		}
	case <-time.After(3 * time.Millisecond):
		t.Fatalf("timeout")
	}
}

func TestNewSeed(t *testing.T) {
	vals := make(map[int64]bool)
	for i := 0; i < 1000; i++ {
		seed := newSeed()
		if _, exists := vals[seed]; exists {
			t.Fatal("newSeed() return a value it'd previously returned")
		}
		vals[seed] = true
	}
}

func TestRandomTimeout_NoTime(t *testing.T) {
	timeout := randomTimeout(0)
	if timeout != nil {
		t.Fatalf("expected nil channel")
	}
}

func TestMin(t *testing.T) {
	if min(1, 1) != 1 {
		t.Fatalf("bad min")
	}
	if min(2, 1) != 1 {
		t.Fatalf("bad min")
	}
	if min(1, 2) != 1 {
		t.Fatalf("bad min")
	}
}

func TestMax(t *testing.T) {
	if max(1, 1) != 1 {
		t.Fatalf("bad max")
	}
	if max(2, 1) != 2 {
		t.Fatalf("bad max")
	}
	if max(1, 2) != 2 {
		t.Fatalf("bad max")
	}
}

func TestGenerateUUID(t *testing.T) {
	prev := generateUUID()
	for i := 0; i < 100; i++ {
		id := generateUUID()
		if prev == id {
			t.Fatalf("Should get a new ID!")
		}

		matched, err := regexp.MatchString(
			`[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}`, id)
		if !matched || err != nil {
			t.Fatalf("expected match %s %v %s", id, matched, err)
		}
	}
}

func TestAsyncNotify(t *testing.T) {
	chs := []chan struct{}{
		make(chan struct{}),
		make(chan struct{}, 1),
		make(chan struct{}, 2),
	}

	// Should not block!
	asyncNotify(chs)
	asyncNotify(chs)
	asyncNotify(chs)

	// Try to read
	select {
	case <-chs[0]:
		t.Fatalf("should not have message!")
	default:
	}
	select {
	case <-chs[1]:
	default:
		t.Fatalf("should have message!")
	}
	select {
	case <-chs[2]:
	default:
		t.Fatalf("should have message!")
	}
	select {
	case <-chs[2]:
	default:
		t.Fatalf("should have message!")
	}
}

func TestExcludePeer(t *testing.T) {
	peers := []string{NewInmemAddr(), NewInmemAddr(), NewInmemAddr()}
	peer := peers[2]

	after := ExcludePeer(peers, peer)
	if len(after) != 2 {
		t.Fatalf("Bad length")
	}
	if after[0] == peer || after[1] == peer {
		t.Fatalf("should not contain peer")
	}
}

func TestPeerContained(t *testing.T) {
	peers := []string{NewInmemAddr(), NewInmemAddr(), NewInmemAddr()}

	if !PeerContained(peers, peers[2]) {
		t.Fatalf("Expect contained")
	}
	if PeerContained(peers, NewInmemAddr()) {
		t.Fatalf("unexpected contained")
	}
}

func TestAddUniquePeer(t *testing.T) {
	peers := []string{NewInmemAddr(), NewInmemAddr(), NewInmemAddr()}
	after := AddUniquePeer(peers, peers[2])
	if !reflect.DeepEqual(after, peers) {
		t.Fatalf("unexpected append")
	}
	after = AddUniquePeer(peers, NewInmemAddr())
	if len(after) != 4 {
		t.Fatalf("expected append")
	}
}

func TestEncodeDecodePeers(t *testing.T) {
	peers := []string{NewInmemAddr(), NewInmemAddr(), NewInmemAddr()}
	_, trans := NewInmemTransport()

	// Try to encode/decode
	buf := encodePeers(peers, trans)
	decoded := decodePeers(buf, trans)

	if !reflect.DeepEqual(peers, decoded) {
		t.Fatalf("mismatch %v %v", peers, decoded)
	}
}

func TestBackoff(t *testing.T) {
	b := backoff(10*time.Millisecond, 1, 8)
	if b != 10*time.Millisecond {
		t.Fatalf("bad: %v", b)
	}

	b = backoff(20*time.Millisecond, 2, 8)
	if b != 20*time.Millisecond {
		t.Fatalf("bad: %v", b)
	}

	b = backoff(10*time.Millisecond, 8, 8)
	if b != 640*time.Millisecond {
		t.Fatalf("bad: %v", b)
	}

	b = backoff(10*time.Millisecond, 9, 8)
	if b != 640*time.Millisecond {
		t.Fatalf("bad: %v", b)
	}
}