11using k8s . LeaderElection ;
22using Moq ;
33using System ;
4+ using System . Collections . Concurrent ;
45using System . Collections . Generic ;
56using System . Linq ;
67using System . Threading ;
@@ -84,8 +85,8 @@ public void SimpleLeaderElection()
8485 [ Fact ]
8586 public void LeaderElection ( )
8687 {
87- var electionHistory = new List < string > ( ) ;
88- var leadershipHistory = new List < string > ( ) ;
88+ var electionHistory = new ConcurrentQueue < string > ( ) ;
89+ var leadershipHistory = new ConcurrentQueue < string > ( ) ;
8990 var electionHistoryCountdown = new CountdownEvent ( 7 ) ;
9091
9192 var renewCountA = 3 ;
@@ -95,19 +96,19 @@ public void LeaderElection()
9596 {
9697 renewCountA -- ;
9798
98- electionHistory . Add ( "A creates record" ) ;
99- leadershipHistory . Add ( "A gets leadership" ) ;
99+ electionHistory . Enqueue ( "A creates record" ) ;
100+ leadershipHistory . Enqueue ( "A gets leadership" ) ;
100101 electionHistoryCountdown . Signal ( ) ;
101102 } ;
102103
103104 mockLockA . OnUpdate += ( _ ) =>
104105 {
105106 renewCountA -- ;
106- electionHistory . Add ( "A updates record" ) ;
107+ electionHistory . Enqueue ( "A updates record" ) ;
107108 electionHistoryCountdown . Signal ( ) ;
108109 } ;
109110
110- mockLockA . OnChange += ( _ ) => { leadershipHistory . Add ( "A gets leadership" ) ; } ;
111+ mockLockA . OnChange += ( _ ) => { leadershipHistory . Enqueue ( "A gets leadership" ) ; } ;
111112
112113 var leaderElectionConfigA = new LeaderElectionConfig ( mockLockA )
113114 {
@@ -123,19 +124,19 @@ public void LeaderElection()
123124 {
124125 renewCountB -- ;
125126
126- electionHistory . Add ( "B creates record" ) ;
127+ electionHistory . Enqueue ( "B creates record" ) ;
127128 electionHistoryCountdown . Signal ( ) ;
128- leadershipHistory . Add ( "B gets leadership" ) ;
129+ leadershipHistory . Enqueue ( "B gets leadership" ) ;
129130 } ;
130131
131132 mockLockB . OnUpdate += ( _ ) =>
132133 {
133134 renewCountB -- ;
134- electionHistory . Add ( "B updates record" ) ;
135+ electionHistory . Enqueue ( "B updates record" ) ;
135136 electionHistoryCountdown . Signal ( ) ;
136137 } ;
137138
138- mockLockB . OnChange += ( _ ) => { leadershipHistory . Add ( "B gets leadership" ) ; } ;
139+ mockLockB . OnChange += ( _ ) => { leadershipHistory . Enqueue ( "B gets leadership" ) ; } ;
139140
140141 var leaderElectionConfigB = new LeaderElectionConfig ( mockLockB )
141142 {
@@ -153,13 +154,13 @@ public void LeaderElection()
153154
154155 leaderElector . OnStartedLeading += ( ) =>
155156 {
156- leadershipHistory . Add ( "A starts leading" ) ;
157+ leadershipHistory . Enqueue ( "A starts leading" ) ;
157158 testLeaderElectionLatch . Signal ( ) ;
158159 } ;
159160
160161 leaderElector . OnStoppedLeading += ( ) =>
161162 {
162- leadershipHistory . Add ( "A stops leading" ) ;
163+ leadershipHistory . Enqueue ( "A stops leading" ) ;
163164 testLeaderElectionLatch . Signal ( ) ;
164165 lockAStopLeading . Set ( ) ;
165166 } ;
@@ -176,13 +177,13 @@ public void LeaderElection()
176177
177178 leaderElector . OnStartedLeading += ( ) =>
178179 {
179- leadershipHistory . Add ( "B starts leading" ) ;
180+ leadershipHistory . Enqueue ( "B starts leading" ) ;
180181 testLeaderElectionLatch . Signal ( ) ;
181182 } ;
182183
183184 leaderElector . OnStoppedLeading += ( ) =>
184185 {
185- leadershipHistory . Add ( "B stops leading" ) ;
186+ leadershipHistory . Enqueue ( "B stops leading" ) ;
186187 testLeaderElectionLatch . Signal ( ) ;
187188 } ;
188189
0 commit comments