@@ -1931,8 +1931,10 @@ var _ = Describe("manger.Manager", func() {
1931
1931
1932
1932
It ("should run warmup runnables before leader election is won" , func () {
1933
1933
By ("Creating channels to track execution order" )
1934
- warmupCalled := make (chan struct {})
1935
- leaderElectionRunnableCalled := make (chan struct {})
1934
+ warmupCalled := atomic.Bool {}
1935
+ leaderElectionRunnableStarted := atomic.Bool {}
1936
+ // warmupRunnable's WarmupFunc will block until this channel is closed
1937
+ warmupRunnableWarmupBlockingChan := make (chan struct {})
1936
1938
1937
1939
By ("Creating a manager with leader election enabled" )
1938
1940
m , err := New (cfg , Options {
@@ -1946,31 +1948,29 @@ var _ = Describe("manger.Manager", func() {
1946
1948
})
1947
1949
Expect (err ).NotTo (HaveOccurred ())
1948
1950
1949
- // Override onStoppedLeading to prevent os.Exit
1950
- cm := m .(* controllerManager )
1951
- cm .onStoppedLeading = func () {}
1952
-
1953
1951
By ("Creating a runnable that implements WarmupRunnable interface" )
1954
1952
// Create a warmup runnable
1955
1953
warmupRunnable := warmupRunnableFunc {
1956
1954
StartFunc : func (ctx context.Context ) error {
1957
1955
// This is the main runnable that will be executed after leader election
1956
+ // Block forever
1958
1957
<- ctx .Done ()
1959
1958
return nil
1960
1959
},
1961
1960
WarmupFunc : func (ctx context.Context ) error {
1962
1961
// This should be called during startup before leader election
1963
- close (warmupCalled )
1962
+ warmupCalled .Store (true )
1963
+ <- warmupRunnableWarmupBlockingChan
1964
1964
return nil
1965
1965
},
1966
+ didWarmupFinish : make (chan bool ),
1966
1967
}
1967
1968
Expect (m .Add (warmupRunnable )).To (Succeed ())
1968
1969
1969
1970
By ("Creating a runnable that requires leader election" )
1970
1971
leaderElectionRunnable := leaderElectionRunnableFunc {
1971
1972
StartFunc : func (ctx context.Context ) error {
1972
- // This will only be called after leader election is won
1973
- close (leaderElectionRunnableCalled )
1973
+ leaderElectionRunnableStarted .Store (true )
1974
1974
<- ctx .Done ()
1975
1975
return nil
1976
1976
},
@@ -1985,17 +1985,21 @@ var _ = Describe("manger.Manager", func() {
1985
1985
1986
1986
go func () {
1987
1987
defer GinkgoRecover ()
1988
- Expect (m .Start (ctx )).NotTo ( HaveOccurred ())
1988
+ Expect (m .Start (ctx )).To ( Succeed ())
1989
1989
}()
1990
1990
1991
1991
By ("Waiting for the warmup runnable to be called" )
1992
- <- warmupCalled
1992
+ Eventually (warmupCalled .Load ).Should (BeTrue ())
1993
+ Expect (leaderElectionRunnableStarted .Load ()).To (BeFalse ())
1994
+
1995
+ By ("Closing the channel to unblock the warmup runnable" )
1996
+ close (warmupRunnableWarmupBlockingChan )
1993
1997
1994
1998
By ("Waiting for leader election to be won" )
1995
1999
<- m .Elected ()
1996
2000
1997
2001
By ("Verifying the leader election runnable is called after election" )
1998
- <- leaderElectionRunnableCalled
2002
+ Eventually ( leaderElectionRunnableStarted . Load ). Should ( BeTrue ())
1999
2003
})
2000
2004
})
2001
2005
0 commit comments