migrate from pkg/store to docker/libkv
Signed-off-by: Alexandre Beslic <abronan@docker.com>
Alexandre Beslic
8 years ago
3 | 3 | "sync" |
4 | 4 | |
5 | 5 | log "github.com/Sirupsen/logrus" |
6 | "github.com/docker/swarm/pkg/store" | |
6 | "github.com/docker/libkv/store" | |
7 | 7 | ) |
8 | 8 | |
9 | 9 | // Candidate runs the leader election algorithm asynchronously |
2 | 2 | import ( |
3 | 3 | "testing" |
4 | 4 | |
5 | kv "github.com/docker/swarm/pkg/store" | |
5 | libkvmock "github.com/docker/libkv/store/mock" | |
6 | 6 | "github.com/stretchr/testify/assert" |
7 | 7 | "github.com/stretchr/testify/mock" |
8 | 8 | ) |
9 | 9 | |
10 | 10 | func TestCandidate(t *testing.T) { |
11 | store, err := kv.NewStore("mock", []string{}, nil) | |
11 | kv, err := libkvmock.New([]string{}, nil) | |
12 | 12 | assert.NoError(t, err) |
13 | assert.NotNil(t, kv) | |
13 | 14 | |
14 | mockStore := store.(*kv.Mock) | |
15 | mockLock := &kv.MockLock{} | |
15 | mockStore := kv.(*libkvmock.Mock) | |
16 | mockLock := &libkvmock.Lock{} | |
16 | 17 | mockStore.On("NewLock", "test_key", mock.Anything).Return(mockLock, nil) |
17 | 18 | |
18 | 19 | // Lock and unlock always succeeds. |
21 | 22 | mockLock.On("Lock").Return(mockLostCh, nil) |
22 | 23 | mockLock.On("Unlock").Return(nil) |
23 | 24 | |
24 | candidate := NewCandidate(store, "test_key", "test_node") | |
25 | candidate := NewCandidate(kv, "test_key", "test_node") | |
25 | 26 | candidate.RunForElection() |
26 | 27 | electedCh := candidate.ElectedCh() |
27 | 28 |
0 | 0 | package leadership |
1 | 1 | |
2 | import "github.com/docker/swarm/pkg/store" | |
2 | import "github.com/docker/libkv/store" | |
3 | 3 | |
4 | 4 | // Follower can folow an election in real-time and push notifications whenever |
5 | 5 | // there is a change in leadership. |
2 | 2 | import ( |
3 | 3 | "testing" |
4 | 4 | |
5 | kv "github.com/docker/swarm/pkg/store" | |
5 | "github.com/docker/libkv/store" | |
6 | libkvmock "github.com/docker/libkv/store/mock" | |
6 | 7 | "github.com/stretchr/testify/assert" |
7 | 8 | "github.com/stretchr/testify/mock" |
8 | 9 | ) |
9 | 10 | |
10 | 11 | func TestFollower(t *testing.T) { |
11 | store, err := kv.NewStore("mock", []string{}, nil) | |
12 | kv, err := libkvmock.New([]string{}, nil) | |
12 | 13 | assert.NoError(t, err) |
14 | assert.NotNil(t, kv) | |
13 | 15 | |
14 | mockStore := store.(*kv.Mock) | |
16 | mockStore := kv.(*libkvmock.Mock) | |
15 | 17 | |
16 | kvCh := make(chan *kv.KVPair) | |
17 | var mockKVCh <-chan *kv.KVPair = kvCh | |
18 | kvCh := make(chan *store.KVPair) | |
19 | var mockKVCh <-chan *store.KVPair = kvCh | |
18 | 20 | mockStore.On("Watch", "test_key", mock.Anything).Return(mockKVCh, nil) |
19 | 21 | |
20 | follower := NewFollower(store, "test_key") | |
22 | follower := NewFollower(kv, "test_key") | |
21 | 23 | follower.FollowElection() |
22 | 24 | leaderCh := follower.LeaderCh() |
23 | 25 | |
24 | 26 | // Simulate leader updates |
25 | 27 | go func() { |
26 | kvCh <- &kv.KVPair{Key: "test_key", Value: []byte("leader1")} | |
27 | kvCh <- &kv.KVPair{Key: "test_key", Value: []byte("leader1")} | |
28 | kvCh <- &kv.KVPair{Key: "test_key", Value: []byte("leader2")} | |
29 | kvCh <- &kv.KVPair{Key: "test_key", Value: []byte("leader1")} | |
28 | kvCh <- &store.KVPair{Key: "test_key", Value: []byte("leader1")} | |
29 | kvCh <- &store.KVPair{Key: "test_key", Value: []byte("leader1")} | |
30 | kvCh <- &store.KVPair{Key: "test_key", Value: []byte("leader2")} | |
31 | kvCh <- &store.KVPair{Key: "test_key", Value: []byte("leader1")} | |
30 | 32 | }() |
31 | 33 | |
32 | 34 | // We shouldn't see duplicate events. |