@@ -1452,13 +1452,15 @@ func TestScheduler_WithDistributed(t *testing.T) {
1452
1452
tests := []struct {
1453
1453
name string
1454
1454
count int
1455
+ runCount int
1455
1456
schedulerOpts []SchedulerOption
1456
1457
jobOpts []JobOption
1457
1458
assertions func (* testing.T )
1458
1459
}{
1459
1460
{
1460
1461
"3 schedulers with elector" ,
1461
1462
3 ,
1463
+ 1 ,
1462
1464
[]SchedulerOption {
1463
1465
WithDistributedElector (& testElector {notLeader : notLeader }),
1464
1466
},
@@ -1482,6 +1484,7 @@ func TestScheduler_WithDistributed(t *testing.T) {
1482
1484
{
1483
1485
"3 schedulers with locker" ,
1484
1486
3 ,
1487
+ 1 ,
1485
1488
[]SchedulerOption {
1486
1489
WithDistributedLocker (& testLocker {notLocked : notLocked }),
1487
1490
},
@@ -1499,11 +1502,14 @@ func TestScheduler_WithDistributed(t *testing.T) {
1499
1502
default :
1500
1503
}
1501
1504
}
1505
+
1506
+ assert .Equal (t , 2 , notLockedCount )
1502
1507
},
1503
1508
},
1504
1509
{
1505
1510
"3 schedulers and job with Distributed locker" ,
1506
1511
3 ,
1512
+ 1 ,
1507
1513
nil ,
1508
1514
[]JobOption {
1509
1515
WithDistributedJobLocker (& testLocker {notLocked : notLocked }),
@@ -1521,6 +1527,35 @@ func TestScheduler_WithDistributed(t *testing.T) {
1521
1527
default :
1522
1528
}
1523
1529
}
1530
+
1531
+ assert .Equal (t , 2 , notLockedCount )
1532
+ },
1533
+ },
1534
+ {
1535
+ "3 schedulers and job with disabled Distributed locker" ,
1536
+ 3 ,
1537
+ 3 ,
1538
+ []SchedulerOption {
1539
+ WithDistributedLocker (& testLocker {notLocked : notLocked }),
1540
+ },
1541
+ []JobOption {
1542
+ WithDisabledDistributedJobLocker (true ),
1543
+ },
1544
+ func (_ * testing.T ) {
1545
+ timeout := time .Now ().Add (1 * time .Second )
1546
+ var notLockedCount int
1547
+ for {
1548
+ if time .Now ().After (timeout ) {
1549
+ break
1550
+ }
1551
+ select {
1552
+ case <- notLocked :
1553
+ notLockedCount ++
1554
+ default :
1555
+ }
1556
+ }
1557
+
1558
+ assert .Equal (t , 0 , notLockedCount )
1524
1559
},
1525
1560
},
1526
1561
}
@@ -1531,6 +1566,11 @@ func TestScheduler_WithDistributed(t *testing.T) {
1531
1566
ctx , cancel := context .WithCancel (context .Background ())
1532
1567
schedulersDone := make (chan struct {}, tt .count )
1533
1568
1569
+ var (
1570
+ runCount int
1571
+ doneCount int
1572
+ )
1573
+
1534
1574
for i := tt .count ; i > 0 ; i -- {
1535
1575
s := newTestScheduler (t ,
1536
1576
tt .schedulerOpts ... ,
@@ -1539,6 +1579,7 @@ func TestScheduler_WithDistributed(t *testing.T) {
1539
1579
WithStartAt (
1540
1580
WithStartImmediately (),
1541
1581
),
1582
+ WithLimitedRuns (1 ),
1542
1583
}
1543
1584
jobOpts = append (jobOpts , tt .jobOpts ... )
1544
1585
@@ -1565,31 +1606,39 @@ func TestScheduler_WithDistributed(t *testing.T) {
1565
1606
}()
1566
1607
}
1567
1608
1568
- var runCount int
1569
- select {
1570
- case <- jobsRan :
1571
- cancel ()
1572
- runCount ++
1573
- case <- time .After (time .Second ):
1574
- cancel ()
1575
- t .Error ("timed out waiting for job to run" )
1609
+ RunCountLoop:
1610
+ for {
1611
+ select {
1612
+ case <- jobsRan :
1613
+ runCount ++
1614
+ if runCount >= tt .runCount {
1615
+ break RunCountLoop
1616
+ }
1617
+ case <- time .After (time .Second ):
1618
+ t .Error ("timed out waiting for job to run" )
1619
+ break RunCountLoop
1620
+ }
1576
1621
}
1577
1622
1578
- var doneCount int
1579
- timeout := time .Now ().Add (3 * time .Second )
1580
- for doneCount < tt .count && time .Now ().After (timeout ) {
1623
+ cancel ()
1624
+ assert .Equal (t , tt .runCount , runCount )
1625
+
1626
+ DoneCountLoop:
1627
+ for {
1581
1628
select {
1582
1629
case <- schedulersDone :
1583
1630
doneCount ++
1584
- default :
1631
+ if doneCount >= tt .count {
1632
+ break DoneCountLoop
1633
+ }
1634
+ case <- time .After (3 * time .Second ):
1635
+ t .Error ("timed out waiting for schedulers to shutdown" )
1636
+ break DoneCountLoop
1585
1637
}
1586
1638
}
1587
- close (jobsRan )
1588
- for range jobsRan {
1589
- runCount ++
1590
- }
1591
1639
1592
- assert .Equal (t , 1 , runCount )
1640
+ assert .Equal (t , tt .count , doneCount )
1641
+
1593
1642
time .Sleep (time .Second )
1594
1643
tt .assertions (t )
1595
1644
})
0 commit comments