Codebase list golang-github-docker-leadership / 6962ad8
migrate from pkg/store to docker/libkv Signed-off-by: Alexandre Beslic <abronan@docker.com> Alexandre Beslic 8 years ago
4 changed file(s) with 20 addition(s) and 17 deletion(s). Raw diff Collapse all Expand all
33 "sync"
44
55 log "github.com/Sirupsen/logrus"
6 "github.com/docker/swarm/pkg/store"
6 "github.com/docker/libkv/store"
77 )
88
99 // Candidate runs the leader election algorithm asynchronously
22 import (
33 "testing"
44
5 kv "github.com/docker/swarm/pkg/store"
5 libkvmock "github.com/docker/libkv/store/mock"
66 "github.com/stretchr/testify/assert"
77 "github.com/stretchr/testify/mock"
88 )
99
1010 func TestCandidate(t *testing.T) {
11 store, err := kv.NewStore("mock", []string{}, nil)
11 kv, err := libkvmock.New([]string{}, nil)
1212 assert.NoError(t, err)
13 assert.NotNil(t, kv)
1314
14 mockStore := store.(*kv.Mock)
15 mockLock := &kv.MockLock{}
15 mockStore := kv.(*libkvmock.Mock)
16 mockLock := &libkvmock.Lock{}
1617 mockStore.On("NewLock", "test_key", mock.Anything).Return(mockLock, nil)
1718
1819 // Lock and unlock always succeeds.
2122 mockLock.On("Lock").Return(mockLostCh, nil)
2223 mockLock.On("Unlock").Return(nil)
2324
24 candidate := NewCandidate(store, "test_key", "test_node")
25 candidate := NewCandidate(kv, "test_key", "test_node")
2526 candidate.RunForElection()
2627 electedCh := candidate.ElectedCh()
2728
00 package leadership
11
2 import "github.com/docker/swarm/pkg/store"
2 import "github.com/docker/libkv/store"
33
44 // Follower can folow an election in real-time and push notifications whenever
55 // there is a change in leadership.
22 import (
33 "testing"
44
5 kv "github.com/docker/swarm/pkg/store"
5 "github.com/docker/libkv/store"
6 libkvmock "github.com/docker/libkv/store/mock"
67 "github.com/stretchr/testify/assert"
78 "github.com/stretchr/testify/mock"
89 )
910
1011 func TestFollower(t *testing.T) {
11 store, err := kv.NewStore("mock", []string{}, nil)
12 kv, err := libkvmock.New([]string{}, nil)
1213 assert.NoError(t, err)
14 assert.NotNil(t, kv)
1315
14 mockStore := store.(*kv.Mock)
16 mockStore := kv.(*libkvmock.Mock)
1517
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
1820 mockStore.On("Watch", "test_key", mock.Anything).Return(mockKVCh, nil)
1921
20 follower := NewFollower(store, "test_key")
22 follower := NewFollower(kv, "test_key")
2123 follower.FollowElection()
2224 leaderCh := follower.LeaderCh()
2325
2426 // Simulate leader updates
2527 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")}
3032 }()
3133
3234 // We shouldn't see duplicate events.