forked from kgateway-dev/kgateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleader_test.go
87 lines (64 loc) · 2.13 KB
/
leader_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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package leaderelector_test
import (
"context"
"sync/atomic"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/kgateway-dev/kgateway/pkg/bootstrap/leaderelector"
)
var _ = Describe("Leader Startup Action", func() {
var (
ctx context.Context
cancel context.CancelFunc
electedChan chan struct{}
identity leaderelector.Identity
startupAction *leaderelector.LeaderStartupAction
startupOps uint64
)
BeforeEach(func() {
ctx, cancel = context.WithCancel(context.Background())
electedChan = make(chan struct{})
identity = leaderelector.NewIdentity(electedChan)
startupAction = leaderelector.NewLeaderStartupAction(identity)
startupAction.SetAction(func() error {
atomic.AddUint64(&startupOps, 1)
return nil
})
atomic.StoreUint64(&startupOps, 0)
})
AfterEach(func() {
cancel()
})
It("StartupAction executed once, if elected", func() {
startupAction.WatchElectionResults(ctx)
// StartupAction not executed, if not elected
Consistently(func(g Gomega) {
g.Expect(atomic.LoadUint64(&startupOps)).To(Equal(uint64(0)))
}, "1s", ".1s").ShouldNot(HaveOccurred())
// signal election
close(electedChan)
Eventually(func(g Gomega) {
g.Expect(atomic.LoadUint64(&startupOps)).To(Equal(uint64(1)))
}, "1s", ".1s").ShouldNot(HaveOccurred())
Consistently(func(g Gomega) {
g.Expect(atomic.LoadUint64(&startupOps)).To(Equal(uint64(1)))
}, "1s", ".1s").ShouldNot(HaveOccurred())
})
It("StartupAction not executed after context cancelled", func() {
startupAction.WatchElectionResults(ctx)
// StartupAction not executed, if not elected
Consistently(func(g Gomega) {
g.Expect(atomic.LoadUint64(&startupOps)).To(Equal(uint64(0)))
}, "1s", ".1s").ShouldNot(HaveOccurred())
// Cancelling the context should stop the election watch
cancel()
Consistently(func(g Gomega) {
g.Expect(atomic.LoadUint64(&startupOps)).To(Equal(uint64(0)))
}, "1s", ".1s").ShouldNot(HaveOccurred())
// signal election
close(electedChan)
Consistently(func(g Gomega) {
g.Expect(atomic.LoadUint64(&startupOps)).To(Equal(uint64(0)))
}, "1s", ".1s").ShouldNot(HaveOccurred())
})
})