-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathctrl_test.go
More file actions
65 lines (56 loc) · 1.12 KB
/
Copy pathctrl_test.go
File metadata and controls
65 lines (56 loc) · 1.12 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
package chief
import (
"os"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
clientv3 "go.etcd.io/etcd/client/v3"
)
type testWorker struct {
m sync.Mutex
gotLeadership int
lostLeadership int
}
func (t *testWorker) Start() {
t.m.Lock()
defer t.m.Unlock()
t.gotLeadership++
}
func (t *testWorker) Stop() {
t.m.Lock()
defer t.m.Unlock()
t.lostLeadership++
}
func TestNewController(t *testing.T) {
etcdAddr, ok := os.LookupEnv("ETCD_ADDR")
require.True(t, ok)
cfg := clientv3.Config{Endpoints: []string{etcdAddr}}
cli, err := clientv3.New(cfg)
require.NoError(t, err)
defer func() {
if err := cli.Close(); err != nil {
t.Error(err)
}
}()
ctrl := NewController(cli, "test")
worker := &testWorker{}
ctrl.Register(worker)
start := time.Now()
for {
worker.m.Lock()
x := worker.gotLeadership
worker.m.Unlock()
if x > 0 {
break
}
require.True(t, time.Now().Before(start.Add(30*time.Second)))
time.Sleep(200 * time.Millisecond)
}
ctrl.Close()
worker.m.Lock()
x := worker.gotLeadership
worker.m.Unlock()
assert.Equal(t, 1, x)
}