-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstore_etcd_test.go
64 lines (54 loc) · 1.74 KB
/
store_etcd_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
package prophet
import (
"context"
"fmt"
"math"
"strings"
"testing"
"time"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"go.etcd.io/etcd/clientv3"
)
func TestAlreadyBootstrapped(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
stopC, port, err := startTestSingleEtcd(t)
if err != nil {
assert.FailNowf(t, "start embed etcd failed", "error: %+v", err)
}
defer close(stopC)
client, err := clientv3.New(clientv3.Config{
Endpoints: strings.Split(fmt.Sprintf("http://127.0.0.1:%d", port), ","),
DialTimeout: DefaultTimeout,
})
assert.Nil(t, err, "create etcd client failed")
defer client.Close()
e, err := NewElector(client)
assert.Nil(t, err, "TestAlreadyBootstrapped failed")
defer e.Stop(math.MaxUint64)
go e.ElectionLoop(context.Background(), math.MaxUint64, "node1", func() {}, func() {})
time.Sleep(time.Millisecond * 200)
store := newEtcdStore(client, newTestAdapter(ctrl), "node1", e)
yes, err := store.AlreadyBootstrapped()
assert.NoError(t, err, "TestAlreadyBootstrapped failed")
assert.False(t, yes, "TestAlreadyBootstrapped failed")
var reses []Resource
for i := 0; i < 100; i++ {
res := newTestResource()
res.SetID(uint64(i + 1))
reses = append(reses, res)
}
yes, err = store.PutBootstrapped(newTestContainer(), reses...)
assert.NoError(t, err, "TestAlreadyBootstrapped failed")
assert.True(t, yes, "TestAlreadyBootstrapped failed")
c := 0
err = store.LoadResources(8, func(res Resource) {
c++
})
assert.NoError(t, err, "TestAlreadyBootstrapped failed")
assert.Equal(t, 100, c, "TestAlreadyBootstrapped failed")
yes, err = store.AlreadyBootstrapped()
assert.NoError(t, err, "TestAlreadyBootstrapped failed")
assert.True(t, yes, "TestAlreadyBootstrapped failed")
}