@@ -23,6 +23,9 @@ import (
23
23
"testing"
24
24
"time"
25
25
26
+ "strings"
27
+
28
+ "github.com/spf13/viper"
26
29
"github.com/stretchr/testify/assert"
27
30
"github.com/stretchr/testify/mock"
28
31
)
@@ -33,11 +36,11 @@ const (
33
36
)
34
37
35
38
func init () {
36
- startupGracePeriod = time . Millisecond * 500
37
- membershipSampleInterval = time .Millisecond * 100
38
- leaderAliveThreshold = time .Millisecond * 500
39
- leadershipDeclarationInterval = leaderAliveThreshold / 2
40
- leaderElectionDuration = time .Millisecond * 500
39
+
40
+ SetStartupGracePeriod ( time .Millisecond * 500 )
41
+ SetMembershipSampleInterval ( time .Millisecond * 100 )
42
+ SetLeaderAliveThreshold ( time . Millisecond * 500 )
43
+ SetLeaderElectionDuration ( time .Millisecond * 500 )
41
44
}
42
45
43
46
type msg struct {
@@ -186,7 +189,7 @@ func TestInitPeersAtSameTime(t *testing.T) {
186
189
// Scenario: Peers are spawned at the same time
187
190
// expected outcome: the peer that has the lowest ID is the leader
188
191
peers := createPeers (0 , 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 , 0 )
189
- time .Sleep (startupGracePeriod + leaderElectionDuration )
192
+ time .Sleep (getStartupGracePeriod () + getLeaderElectionDuration () )
190
193
leaders := waitForLeaderElection (t , peers )
191
194
isP0leader := peers [len (peers )- 1 ].IsLeader ()
192
195
assert .True (t , isP0leader , "p0 isn't a leader. Leaders are: %v" , leaders )
@@ -198,7 +201,7 @@ func TestInitPeersStartAtIntervals(t *testing.T) {
198
201
t .Parallel ()
199
202
// Scenario: Peers are spawned one by one in a slow rate
200
203
// expected outcome: the first peer is the leader although its ID is lowest
201
- peers := createPeers (startupGracePeriod + leadershipDeclarationInterval , 3 , 2 , 1 , 0 )
204
+ peers := createPeers (getStartupGracePeriod () + getLeadershipDeclarationInterval () , 3 , 2 , 1 , 0 )
202
205
waitForLeaderElection (t , peers )
203
206
assert .True (t , peers [0 ].IsLeader ())
204
207
}
@@ -226,9 +229,9 @@ func TestStop(t *testing.T) {
226
229
for _ , p := range peers {
227
230
p .Stop ()
228
231
}
229
- time .Sleep (leaderAliveThreshold )
232
+ time .Sleep (getLeaderAliveThreshold () )
230
233
gossipCounterAfterStop := atomic .LoadInt32 (& gossipCounter )
231
- time .Sleep (leaderAliveThreshold * 5 )
234
+ time .Sleep (getLeaderAliveThreshold () * 5 )
232
235
assert .Equal (t , gossipCounterAfterStop , atomic .LoadInt32 (& gossipCounter ))
233
236
}
234
237
@@ -265,7 +268,7 @@ func TestConvergence(t *testing.T) {
265
268
p .On ("Peers" ).Return (allPeerIds )
266
269
}
267
270
268
- time .Sleep (leaderAliveThreshold * 5 )
271
+ time .Sleep (getLeaderAliveThreshold () * 5 )
269
272
finalLeaders := waitForLeaderElection (t , combinedPeers )
270
273
assert .Len (t , finalLeaders , 1 , "Combined peer group was suppose to have 1 leader exactly" )
271
274
assert .Equal (t , leaders1 [0 ], finalLeaders [0 ], "Combined peer group has different leader than expected:" )
@@ -288,12 +291,12 @@ func TestLeadershipTakeover(t *testing.T) {
288
291
// Scenario: Peers spawn one by one in descending order.
289
292
// After a while, the leader peer stops.
290
293
// expected outcome: the peer that takes over is the peer with lowest ID
291
- peers := createPeers (startupGracePeriod + leadershipDeclarationInterval , 5 , 4 , 3 , 2 )
294
+ peers := createPeers (getStartupGracePeriod () + getLeadershipDeclarationInterval () , 5 , 4 , 3 , 2 )
292
295
leaders := waitForLeaderElection (t , peers )
293
296
assert .Len (t , leaders , 1 , "Only 1 leader should have been elected" )
294
297
assert .Equal (t , "p5" , leaders [0 ])
295
298
peers [0 ].Stop ()
296
- time .Sleep (leadershipDeclarationInterval + leaderAliveThreshold * 3 )
299
+ time .Sleep (getLeadershipDeclarationInterval () + getLeaderAliveThreshold () * 3 )
297
300
leaders = waitForLeaderElection (t , peers [1 :])
298
301
assert .Len (t , leaders , 1 , "Only 1 leader should have been elected" )
299
302
assert .Equal (t , "p2" , leaders [0 ])
@@ -316,7 +319,7 @@ func TestPartition(t *testing.T) {
316
319
p .On ("Peers" ).Return ([]Peer {})
317
320
p .On ("Gossip" , mock .Anything )
318
321
}
319
- time .Sleep (leadershipDeclarationInterval + leaderAliveThreshold * 2 )
322
+ time .Sleep (getLeadershipDeclarationInterval () + getLeaderAliveThreshold () * 2 )
320
323
leaders = waitForMultipleLeadersElection (t , peers , 6 )
321
324
assert .Len (t , leaders , 6 )
322
325
for _ , p := range peers {
@@ -329,7 +332,7 @@ func TestPartition(t *testing.T) {
329
332
p .callbackInvoked = false
330
333
p .sharedLock .Unlock ()
331
334
}
332
- time .Sleep (leadershipDeclarationInterval + leaderAliveThreshold * 2 )
335
+ time .Sleep (getLeadershipDeclarationInterval () + getLeaderAliveThreshold () * 2 )
333
336
leaders = waitForLeaderElection (t , peers )
334
337
assert .Len (t , leaders , 1 , "Only 1 leader should have been elected" )
335
338
assert .Equal (t , "p0" , leaders [0 ])
@@ -343,3 +346,41 @@ func TestPartition(t *testing.T) {
343
346
}
344
347
345
348
}
349
+
350
+ func TestConfigFromFile (t * testing.T ) {
351
+ preStartupGracePeriod := getStartupGracePeriod ()
352
+ preMembershipSampleInterval := getMembershipSampleInterval ()
353
+ preLeaderAliveThreshold := getLeaderAliveThreshold ()
354
+ preLeaderElectionDuration := getLeaderElectionDuration ()
355
+
356
+ // Recover the config values in order to avoid impacting other tests
357
+ defer func () {
358
+ SetStartupGracePeriod (preStartupGracePeriod )
359
+ SetMembershipSampleInterval (preMembershipSampleInterval )
360
+ SetLeaderAliveThreshold (preLeaderAliveThreshold )
361
+ SetLeaderElectionDuration (preLeaderElectionDuration )
362
+ }()
363
+
364
+ // Verify if using default values when config is missing
365
+ viper .Reset ()
366
+ assert .Equal (t , time .Second * 15 , getStartupGracePeriod ())
367
+ assert .Equal (t , time .Second , getMembershipSampleInterval ())
368
+ assert .Equal (t , time .Second * 10 , getLeaderAliveThreshold ())
369
+ assert .Equal (t , time .Second * 5 , getLeaderElectionDuration ())
370
+ assert .Equal (t , getLeaderAliveThreshold ()/ 2 , getLeadershipDeclarationInterval ())
371
+
372
+ //Verify reading the values from config file
373
+ viper .Reset ()
374
+ viper .SetConfigName ("core" )
375
+ viper .SetEnvPrefix ("CORE" )
376
+ viper .AddConfigPath ("./../../peer" )
377
+ viper .SetEnvKeyReplacer (strings .NewReplacer ("." , "_" ))
378
+ viper .AutomaticEnv ()
379
+ err := viper .ReadInConfig ()
380
+ assert .NoError (t , err )
381
+ assert .Equal (t , time .Second * 15 , getStartupGracePeriod ())
382
+ assert .Equal (t , time .Second , getMembershipSampleInterval ())
383
+ assert .Equal (t , time .Second * 10 , getLeaderAliveThreshold ())
384
+ assert .Equal (t , time .Second * 5 , getLeaderElectionDuration ())
385
+ assert .Equal (t , getLeaderAliveThreshold ()/ 2 , getLeadershipDeclarationInterval ())
386
+ }
0 commit comments