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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
// Copyright (c) The nextest Contributors
// SPDX-License-Identifier: MIT OR Apache-2.0
//! The controller for the test runner.
//!
//! This module interfaces with the external world and the test executor. It
//! receives events from the executor and from other inputs (e.g. signal and
//! input handling), and sends events to the reporter.
use super::{RunUnitRequest, RunnerTaskState, ShutdownRequest};
use crate::{
config::{
elements::{FlakyResult, MaxFail, TerminateMode},
scripts::{ScriptId, SetupScriptConfig},
},
input::{InputEvent, InputHandler},
list::{OwnedTestInstanceId, TestInstance, TestInstanceId, TestInstanceIdKey, TestList},
output_spec::LiveSpec,
reporter::events::{
CancelReason, ChildExecutionOutputDescription, ExecuteStatus, ExecutionResultDescription,
ExecutionStatuses, FailureDescription, FinalRunStats, InfoResponse, ReporterEvent,
RunFinishedStats, RunStats, StressIndex, StressProgress, StressRunStats, TestEvent,
TestEventKind, TestsNotSeen,
},
runner::{ExecutorEvent, RunUnitQuery, SignalRequest, StressCondition, StressCount},
signal::{
JobControlEvent, ShutdownEvent, ShutdownSignalEvent, SignalEvent, SignalHandler,
SignalInfoEvent,
},
time::StopwatchStart,
};
use chrono::Local;
use debug_ignore::DebugIgnore;
use futures::future::{Fuse, FusedFuture};
use nextest_metadata::MismatchReason;
use quick_junit::ReportUuid;
use std::{
collections::{BTreeMap, BTreeSet},
env, mem,
pin::Pin,
time::Duration,
};
use tokio::{
sync::{
mpsc::{UnboundedReceiver, UnboundedSender, unbounded_channel},
oneshot,
},
time::MissedTickBehavior,
};
use tracing::debug;
/// Context for the dispatcher.
///
/// This struct is responsible for coordinating events from the outside world
/// and communicating with the executor.
#[derive(Clone)]
#[derive_where::derive_where(Debug)]
pub(super) struct DispatcherContext<'a, F> {
callback: DebugIgnore<F>,
run_id: ReportUuid,
profile_name: String,
cli_args: Vec<String>,
stopwatch: StopwatchStart,
run_stats: RunStats,
max_fail: MaxFail,
global_timeout: Duration,
running_setup_script: Option<ContextSetupScript<'a>>,
running_tests: BTreeMap<TestInstanceId<'a>, ContextTestInstance<'a>>,
signal_count: Option<SignalCount>,
stress_cx: DispatcherStressContext,
tick_interval: Duration,
rerun_cx: DispatcherRerunContext,
#[cfg(test)]
disable_signal_3_times_panic: bool,
}
impl<'a, F> DispatcherContext<'a, F>
where
F: FnMut(ReporterEvent<'a>) + Send,
{
#[expect(clippy::too_many_arguments)]
pub(super) fn new(
callback: F,
run_id: ReportUuid,
profile_name: &str,
cli_args: Vec<String>,
initial_run_count: usize,
max_fail: MaxFail,
global_timeout: Duration,
stress_condition: Option<StressCondition>,
expected_outstanding: Option<BTreeSet<OwnedTestInstanceId>>,
) -> Self {
// Tick every 50ms by default.
let tick_interval_ms = env::var("NEXTEST_PROGRESS_TICK_INTERVAL_MS")
.ok()
.and_then(|interval| interval.parse::<u64>().ok())
.unwrap_or(50);
Self {
callback: DebugIgnore(callback),
run_id,
stopwatch: crate::time::stopwatch(),
profile_name: profile_name.to_owned(),
cli_args,
run_stats: RunStats {
initial_run_count,
..RunStats::default()
},
max_fail,
global_timeout,
running_setup_script: None,
running_tests: BTreeMap::new(),
signal_count: None,
stress_cx: DispatcherStressContext::new(stress_condition),
tick_interval: Duration::from_millis(tick_interval_ms),
rerun_cx: DispatcherRerunContext::new(expected_outstanding),
#[cfg(test)]
disable_signal_3_times_panic: false,
}
}
/// Runs the dispatcher to completion, until `resp_rx` is closed.
///
/// `executor_rx` is the main communication channel between the dispatcher
/// and the executor. It receives events, but some of those events also
/// include senders for the dispatcher to communicate back to the executor.
///
/// This is expected to be spawned as a task via [`async_scoped`].
pub(super) async fn run(
&mut self,
mut executor_rx: UnboundedReceiver<ExecutorEvent<'a>>,
signal_handler: &mut SignalHandler,
input_handler: &mut InputHandler,
mut report_cancel_rx: Pin<&mut Fuse<oneshot::Receiver<()>>>,
) -> RunnerTaskState {
let mut signals_done = false;
let mut inputs_done = false;
// For stress tests, this function is called for each sub-run -- in
// other words, we reinitialize the global timeout for each sub-run.
let mut global_timeout_sleep =
std::pin::pin!(crate::time::pausable_sleep(self.global_timeout));
// This is the interval at which tick events are sent to the reporter.
let mut tick_interval = tokio::time::interval(self.tick_interval);
tick_interval.set_missed_tick_behavior(MissedTickBehavior::Skip);
loop {
let internal_event = tokio::select! {
_ = &mut global_timeout_sleep => {
InternalEvent::GlobalTimeout
},
_ = tick_interval.tick() => {
InternalEvent::Tick
},
internal_event = executor_rx.recv() => {
match internal_event {
Some(event) => InternalEvent::Executor(event),
None => {
// All runs have been completed.
break RunnerTaskState::finished_no_children();
}
}
},
internal_event = signal_handler.recv(), if !signals_done => {
match internal_event {
Some(event) => InternalEvent::Signal(event),
None => {
signals_done = true;
continue;
}
}
},
internal_event = input_handler.recv(), if !inputs_done => {
match internal_event {
Some(event) => InternalEvent::Input(event),
None => {
inputs_done = true;
continue;
}
}
}
res = &mut report_cancel_rx, if !report_cancel_rx.as_ref().is_terminated() => {
match res {
Ok(()) => {
InternalEvent::ReportCancel
}
Err(_) => {
// In normal operation, the sender is kept alive
// until the end of the run, so this should never
// fail. However there are circumstances around
// shutdown where it may be possible that the sender
// isn't kept alive. In those cases, we just ignore
// the error and carry on.
debug!(
"report_cancel_rx was dropped early: \
shutdown ordering issue?",
);
continue;
}
}
}
};
match self.handle_event(internal_event) {
#[cfg(unix)]
HandleEventResponse::JobControl(JobControlEvent::Stop) => {
// This is in reality bounded by the number of tests
// currently running.
let (status_tx, mut status_rx) = unbounded_channel();
self.broadcast_request(RunUnitRequest::Signal(SignalRequest::Stop(status_tx)));
debug!(
remaining = status_rx.sender_strong_count(),
"stopping tests"
);
// There's a possibility of a race condition between a test
// exiting and sending the message to the receiver. For that
// reason, don't wait more than 100ms on children to stop.
let mut sleep = std::pin::pin!(tokio::time::sleep(Duration::from_millis(100)));
loop {
tokio::select! {
res = status_rx.recv() => {
debug!(
res = ?res,
remaining = status_rx.sender_strong_count(),
"test stopped",
);
if res.is_none() {
// No remaining message in the channel's
// buffer.
break;
}
}
_ = &mut sleep => {
debug!(
remaining = status_rx.sender_strong_count(),
"timeout waiting for tests to stop, ignoring",
);
break;
}
};
}
// Restore the terminal state.
input_handler.suspend();
// Pause the global timeout while suspended.
global_timeout_sleep.as_mut().pause();
// Also pause the stress stopwatch while suspended.
self.stress_cx.pause_stopwatch();
// Now stop nextest itself.
super::os::raise_stop();
}
#[cfg(unix)]
HandleEventResponse::JobControl(JobControlEvent::Continue) => {
// Nextest has been resumed. Resume the input handler, as well as all the tests.
input_handler.resume();
// Resume the global timeout.
global_timeout_sleep.as_mut().resume();
// Also resume the stress stopwatch.
self.stress_cx.resume_stopwatch();
self.broadcast_request(RunUnitRequest::Signal(SignalRequest::Continue));
}
HandleEventResponse::Info(_) => {
// In reality, this is bounded by the number of
// tests running at the same time.
let (sender, mut receiver) = unbounded_channel();
let total = self
.broadcast_request(RunUnitRequest::Query(RunUnitQuery::GetInfo(sender)));
let mut index = 0;
self.info_started(total);
debug!(expected = total, "waiting for info responses");
loop {
// Don't wait too long for tasks to respond, to avoid a
// hung unit task.
let sleep = tokio::time::sleep(Duration::from_millis(100));
tokio::select! {
res = receiver.recv() => {
if let Some(info) = res {
debug!(
index,
expected = total,
remaining = total.saturating_sub(index + 1),
sender_strong_count = receiver.sender_strong_count(),
"received info response",
);
self.info_response(
index,
total,
info,
);
index += 1;
} else {
// All senders have been dropped.
break;
}
}
_ = sleep => {
debug!(
remaining = total.saturating_sub(index + 1),
sender_strong_count = receiver.sender_strong_count(),
"timeout waiting for tests to stop, ignoring",
);
break;
}
};
}
self.info_finished(total.saturating_sub(index + 1));
}
HandleEventResponse::Cancel(cancel) => {
// A cancellation notice was received.
match cancel {
// Some of the branches here don't do anything, but are specified
// for readability.
CancelEvent::Report => {
// An error was produced by the reporter, and cancellation has
// begun.
self.broadcast_request(RunUnitRequest::OtherCancel);
}
CancelEvent::TestFailure => {
// A test failure has caused cancellation to begin.
self.broadcast_request(RunUnitRequest::OtherCancel);
}
CancelEvent::GlobalTimeout => {
// The global timeout has expired, causing cancellation to begin.
self.broadcast_request(RunUnitRequest::Signal(
SignalRequest::Shutdown(ShutdownRequest::Once(
ShutdownEvent::TERMINATE,
)),
));
}
CancelEvent::Signal(req) => {
// A signal has caused cancellation to begin. Let all the child
// processes know about the signal, and continue to handle
// events.
//
// Ignore errors here: if there are no receivers to cancel, so
// be it. Also note the ordering here: cancelled_ref is set
// *before* this is sent.
self.broadcast_request(RunUnitRequest::Signal(
SignalRequest::Shutdown(req),
));
}
}
}
HandleEventResponse::None => {}
}
}
}
pub(super) fn run_started(&mut self, test_list: &'a TestList, test_threads: usize) {
let (stress_count, stress_infinite, stress_duration_nanos) =
match self.stress_cx.condition() {
Some(StressCondition::Count(StressCount::Count { count })) => {
(Some(count.get()), false, None)
}
Some(StressCondition::Count(StressCount::Infinite)) => (None, true, None),
Some(StressCondition::Duration(duration)) => {
(None, false, Some(duration.as_nanos() as u64))
}
None => (None, false, None),
};
crate::fire_usdt!(UsdtRunStart {
run_id: self.run_id,
profile_name: self.profile_name.clone(),
total_tests: test_list.test_count(),
filter_count: test_list.run_count(),
test_threads,
stress_count,
stress_infinite,
stress_duration_nanos,
});
self.basic_callback(TestEventKind::RunStarted {
test_list,
run_id: self.run_id,
profile_name: self.profile_name.clone(),
cli_args: self.cli_args.clone(),
stress_condition: self.stress_cx.condition(),
})
}
pub(super) fn stress_sub_run_started(&mut self, progress: StressProgress) {
// Reset run stats since we're starting over. Do this here rather than
// in stress_sub_run_finished because we sometimes fetch run_stats after
// stress_sub_run_finished and want it to be accurate until the next
// sub-run starts.
let sub_stats = self.run_stats;
self.run_stats = RunStats {
initial_run_count: sub_stats.initial_run_count,
..Default::default()
};
// Fire the USDT probe for stress sub-run start.
let (stress_current, stress_total) = match &progress {
StressProgress::Count {
total,
completed,
elapsed: _,
} => {
let total = match total {
StressCount::Count { count } => Some(count.get()),
StressCount::Infinite => None,
};
(*completed, total)
}
StressProgress::Time {
total: _,
elapsed: _,
completed,
} => (*completed, None),
};
crate::fire_usdt!(UsdtStressSubRunStart {
stress_sub_run_id: progress.unique_id(self.run_id),
run_id: self.run_id,
profile_name: self.profile_name.clone(),
stress_current,
stress_total,
elapsed_nanos: self.stopwatch.snapshot().active.as_nanos() as u64,
});
self.basic_callback(TestEventKind::StressSubRunStarted { progress })
}
pub(super) fn stress_sub_run_finished(&mut self) {
let sub_elapsed = self
.stress_cx
.mark_completed(self.run_stats.summarize_final());
let progress = self
.stress_progress()
.expect("stress_sub_run_finished called in non-stress test context");
// Fire the USDT probe for stress sub-run done.
let (stress_current, stress_total) = match &progress {
StressProgress::Count {
total,
completed,
elapsed: _,
} => {
let total = match total {
StressCount::Count { count } => Some(count.get()),
StressCount::Infinite => None,
};
(*completed - 1, total)
}
StressProgress::Time {
total: _,
elapsed: _,
completed,
} => (*completed - 1, None),
};
crate::fire_usdt!(UsdtStressSubRunDone {
stress_sub_run_id: progress.unique_id(self.run_id),
run_id: self.run_id,
profile_name: self.profile_name.clone(),
stress_current,
stress_total,
elapsed_nanos: self.stopwatch.snapshot().active.as_nanos() as u64,
sub_run_duration_nanos: sub_elapsed.as_nanos() as u64,
total_tests: self.run_stats.initial_run_count,
passed: self.run_stats.passed,
failed: self.run_stats.failed_count(),
skipped: self.run_stats.skipped,
});
self.basic_callback(TestEventKind::StressSubRunFinished {
progress,
sub_elapsed,
sub_stats: self.run_stats,
})
}
pub(super) fn stress_index(&self) -> Option<StressIndex> {
self.stress_cx.stress_index()
}
pub(super) fn stress_progress(&self) -> Option<StressProgress> {
self.stress_cx.progress(self.stopwatch.snapshot().active)
}
/// Returns the reason for cancellation, or `None` if the run is not cancelled.
pub(super) fn cancel_reason(&self) -> Option<CancelReason> {
self.run_stats.cancel_reason
}
#[inline]
fn basic_callback(&mut self, kind: TestEventKind<'a>) {
let snapshot = self.stopwatch.snapshot();
let event = TestEvent {
// We'd previously add up snapshot.start_time + snapshot.active +
// paused, but that isn't resilient to clock changes. Instead, use
// `Local::now()` time (which isn't necessarily monotonic) along
// with snapshot.active (which is almost always monotonic).
timestamp: Local::now().fixed_offset(),
elapsed: snapshot.active,
kind,
};
(self.callback)(ReporterEvent::Test(Box::new(event)))
}
#[inline]
fn callback_none_response(&mut self, kind: TestEventKind<'a>) -> HandleEventResponse {
self.basic_callback(kind);
HandleEventResponse::None
}
fn handle_event(&mut self, event: InternalEvent<'a>) -> HandleEventResponse {
match event {
InternalEvent::Tick => {
(self.callback)(ReporterEvent::Tick);
HandleEventResponse::None
}
InternalEvent::Executor(ExecutorEvent::SetupScriptStarted {
stress_index,
script_id,
config,
program,
index,
total,
req_rx_tx,
}) => {
if self.run_stats.cancel_reason.is_some() {
// The run has been cancelled: don't start any new units.
return HandleEventResponse::None;
}
let (req_tx, req_rx) = unbounded_channel();
// Write the status line before unblocking the executor (same
// reasoning as the Started case below).
self.basic_callback(TestEventKind::SetupScriptStarted {
stress_index,
index,
total,
script_id: script_id.clone(),
program,
args: config.command.args.clone(),
no_capture: config.no_capture(),
});
match req_rx_tx.send(req_rx) {
Ok(_) => {}
Err(_) => {
// The test task died?
debug!(?script_id, "test task died, ignoring");
return HandleEventResponse::None;
}
}
self.new_setup_script(script_id, config, index, total, req_tx);
HandleEventResponse::None
}
InternalEvent::Executor(ExecutorEvent::SetupScriptSlow {
stress_index,
script_id,
config,
program,
elapsed,
will_terminate,
}) => {
// Fire the USDT probe for setup script slow.
crate::fire_usdt!(UsdtSetupScriptSlow {
id: script_id.unique_id(self.run_id, stress_index.map(|s| s.current)),
run_id: self.run_id,
script_id: script_id.to_string(),
program: program.clone(),
args: config.command.args.clone(),
elapsed_nanos: elapsed.as_nanos() as u64,
will_terminate: will_terminate.is_some(),
stress_current: stress_index.map(|s| s.current),
stress_total: stress_index.and_then(|s| s.total_get()),
});
self.callback_none_response(TestEventKind::SetupScriptSlow {
stress_index,
script_id,
program,
args: config.command.args.clone(),
elapsed,
will_terminate: will_terminate.is_some(),
})
}
InternalEvent::Executor(ExecutorEvent::SetupScriptFinished {
stress_index,
script_id,
config,
program,
index,
total,
status,
}) => {
self.finish_setup_script();
self.run_stats.on_setup_script_finished(&status);
// Fire the setup-script-done probe, extracting the exit code
// from the result if available.
let exit_code = match &status.result {
ExecutionResultDescription::Fail {
failure: FailureDescription::ExitCode { code },
..
} => Some(*code),
_ => None,
};
// Extract stdout and stderr lengths from the output.
let (stdout_len, stderr_len) = match &status.output {
ChildExecutionOutputDescription::Output { output, .. } => {
output.stdout_stderr_len()
}
ChildExecutionOutputDescription::StartError(_) => (None, None),
};
crate::fire_usdt!(UsdtSetupScriptDone {
id: script_id.unique_id(self.run_id, stress_index.map(|s| s.current)),
run_id: self.run_id,
script_id: script_id.to_string(),
program: program.clone(),
args: config.command.args.clone(),
result: status.result.as_static_str(),
exit_code,
duration_nanos: status.time_taken.as_nanos() as u64,
stress_current: stress_index.map(|s| s.current),
stress_total: stress_index.and_then(|s| s.total_get()),
stdout_len,
stderr_len,
});
// Setup scripts failing always cause the entire test run to be cancelled
// (--no-fail-fast is ignored).
let fail_cancel = !status.result.is_success();
self.basic_callback(TestEventKind::SetupScriptFinished {
stress_index,
index,
total,
script_id,
program,
args: config.command.args.clone(),
no_capture: config.no_capture(),
junit_store_success_output: config.junit.store_success_output,
junit_store_failure_output: config.junit.store_failure_output,
run_status: status,
});
if fail_cancel {
self.begin_cancel(CancelReason::SetupScriptFailure, CancelEvent::TestFailure)
} else {
HandleEventResponse::None
}
}
InternalEvent::Executor(ExecutorEvent::Started {
stress_index,
test_instance,
slot_assignment,
command_line,
req_rx_tx,
flaky_result,
}) => {
if self.run_stats.cancel_reason.is_some() {
// The run has been cancelled: don't start any new units.
return HandleEventResponse::None;
}
let (req_tx, req_rx) = unbounded_channel();
// Write the status line before unblocking the executor. The
// executor spawns the child process immediately after receiving
// this ack, and in no-capture / interceptor mode the child
// inherits stderr. Writing the status line first prevents
// interleaving between the status line and the child's output.
//
// Use len() + 1 because new_test hasn't been called yet (it
// must happen after the successful send to avoid orphaned
// state on failure).
self.basic_callback(TestEventKind::TestStarted {
stress_index,
test_instance: test_instance.id(),
slot_assignment,
current_stats: self.run_stats,
running: self.running_tests.len() + 1,
command_line,
});
match req_rx_tx.send(req_rx) {
Ok(_) => {}
Err(_) => {
// The test task died?
debug!(test = ?test_instance.id(), "test task died, ignoring");
return HandleEventResponse::None;
}
}
self.new_test(test_instance, req_tx, flaky_result);
HandleEventResponse::None
}
InternalEvent::Executor(ExecutorEvent::Slow {
stress_index,
test_instance,
retry_data,
elapsed,
will_terminate,
}) => {
// Fire the test-slow probe.
crate::fire_usdt!(UsdtTestAttemptSlow {
attempt_id: test_instance.id().attempt_id(
self.run_id,
stress_index.map(|s| s.current),
retry_data.attempt,
),
run_id: self.run_id,
binary_id: test_instance.suite_info.binary_id.clone(),
test_name: test_instance.name.to_owned(),
attempt: retry_data.attempt,
total_attempts: retry_data.total_attempts,
elapsed_nanos: elapsed.as_nanos() as u64,
will_terminate: will_terminate.is_some(),
stress_current: stress_index.map(|s| s.current),
stress_total: stress_index.and_then(|s| s.total_get()),
});
self.callback_none_response(TestEventKind::TestSlow {
stress_index,
test_instance: test_instance.id(),
retry_data,
elapsed,
will_terminate: will_terminate.is_some(),
})
}
InternalEvent::Executor(ExecutorEvent::AttemptFailedWillRetry {
stress_index,
test_instance,
failure_output,
run_status,
delay_before_next_attempt,
}) => {
let instance = self.existing_test(test_instance.id());
instance.attempt_failed_will_retry(run_status.clone());
self.callback_none_response(TestEventKind::TestAttemptFailedWillRetry {
stress_index,
test_instance: test_instance.id(),
failure_output,
run_status,
delay_before_next_attempt,
running: self.running_tests.len(),
})
}
InternalEvent::Executor(ExecutorEvent::RetryStarted {
stress_index,
test_instance,
slot_assignment,
retry_data,
command_line,
tx,
}) => {
if self.run_stats.cancel_reason.is_some() {
// The run has been cancelled: don't send a message over the tx and don't start
// any new units.
return HandleEventResponse::None;
}
// Write the status line before unblocking the executor (same
// reasoning as the Started case above).
self.basic_callback(TestEventKind::TestRetryStarted {
stress_index,
test_instance: test_instance.id(),
slot_assignment,
retry_data,
running: self.running_tests.len(),
command_line,
});
match tx.send(()) {
Ok(_) => {}
Err(_) => {
// The test task died?
debug!(test = ?test_instance.id(), "test task died, ignoring");
}
}
HandleEventResponse::None
}
InternalEvent::Executor(ExecutorEvent::Finished {
stress_index,
test_instance,
success_output,
failure_output,
junit_store_success_output,
junit_store_failure_output,
junit_flaky_fail_status,
last_run_status,
}) => {
let run_statuses = self.finish_test(test_instance.id(), last_run_status);
self.run_stats.on_test_finished(&run_statuses);
// Check if this run should be cancelled because of a failure.
// is_exceeded returns Some(terminate_mode) if max-fail is exceeded.
let terminate_mode = self.max_fail.is_exceeded(self.run_stats.failed_count());
self.basic_callback(TestEventKind::TestFinished {
stress_index,
test_instance: test_instance.id(),
success_output,
failure_output,
junit_store_success_output,
junit_store_failure_output,
junit_flaky_fail_status,
run_statuses,
current_stats: self.run_stats,
running: self.running(),
});
if let Some(terminate_mode) = terminate_mode {
// A test failed: start cancellation if required.
// Check if we should terminate immediately or wait for running tests.
match terminate_mode {
TerminateMode::Immediate => {
// Terminate running tests immediately.
self.broadcast_request(RunUnitRequest::Signal(
SignalRequest::Shutdown(ShutdownRequest::Once(
ShutdownEvent::TestFailureImmediate,
)),
));
self.begin_cancel(
CancelReason::TestFailureImmediate,
CancelEvent::Signal(ShutdownRequest::Once(
ShutdownEvent::TestFailureImmediate,
)),
)
}
TerminateMode::Wait => {
self.begin_cancel(CancelReason::TestFailure, CancelEvent::TestFailure)
}
}
} else {
HandleEventResponse::None
}
}
InternalEvent::Executor(ExecutorEvent::Skipped {
stress_index,
test_instance,
reason,
}) => {
// If the mismatch reason is that this test isn't a benchmark,
// we don't display it in the skip counts (but still keep track
// of it internally).
if !matches!(reason, MismatchReason::NotBenchmark) {
self.run_stats.skipped += 1;
}
self.callback_none_response(TestEventKind::TestSkipped {
stress_index,
test_instance: test_instance.id(),
reason,
})
}
InternalEvent::Signal(event) => self.handle_signal_event(event),
InternalEvent::GlobalTimeout => {
self.begin_cancel(CancelReason::GlobalTimeout, CancelEvent::GlobalTimeout)
}
InternalEvent::Input(InputEvent::Info) => {
// Print current statistics.
HandleEventResponse::Info(InfoEvent::Input)
}
InternalEvent::Input(InputEvent::Enter) => {
self.callback_none_response(TestEventKind::InputEnter {
current_stats: self.run_stats,
running: self.running(),
})
}
InternalEvent::ReportCancel => {
self.begin_cancel(CancelReason::ReportError, CancelEvent::Report)
}
}
}
fn new_setup_script(
&mut self,
id: ScriptId,
config: &'a SetupScriptConfig,
index: usize,
total: usize,
req_tx: UnboundedSender<RunUnitRequest<'a>>,
) {
let prev = self.running_setup_script.replace(ContextSetupScript {
id,
config,
index,
total,
req_tx,
});
debug_assert!(
prev.is_none(),
"new setup script expected, but already exists: {prev:?}",
);
}
fn finish_setup_script(&mut self) {
let prev = self.running_setup_script.take();
debug_assert!(
prev.is_some(),
"existing setup script expected, but already exists: {prev:?}",
);
}
fn new_test(
&mut self,
instance: TestInstance<'a>,
req_tx: UnboundedSender<RunUnitRequest<'a>>,
flaky_result: FlakyResult,
) {
// Track this test as seen for rerun tracking.
self.rerun_cx.mark_seen(instance.id());
let prev = self.running_tests.insert(
instance.id(),
ContextTestInstance {
instance,
past_attempts: Vec::new(),
req_tx,
flaky_result,
},
);
if let Some(prev) = prev {
panic!("new test instance expected, but already exists: {prev:?}");
}
}
fn existing_test(&mut self, key: TestInstanceId<'a>) -> &mut ContextTestInstance<'a> {
self.running_tests
.get_mut(&key)
.expect("existing test instance expected but not found")
}
fn finish_test(
&mut self,
key: TestInstanceId<'a>,
last_run_status: ExecuteStatus<LiveSpec>,
) -> ExecutionStatuses<LiveSpec> {
self.running_tests
.remove(&key)
.unwrap_or_else(|| {
panic!(
"existing test instance {key:?} expected, \
but not found"
)
})
.finish(last_run_status)
}
fn setup_scripts_running(&self) -> usize {
if self.running_setup_script.is_some() {
1
} else {
0
}
}
fn running(&self) -> usize {
self.running_tests.len()
}
/// Returns the number of units the request was broadcast to.
fn broadcast_request(&self, req: RunUnitRequest<'a>) -> usize {
let mut count = 0;
if let Some(setup_script) = &self.running_setup_script {
if setup_script.req_tx.send(req.clone()).is_err() {
// The most likely reason for this error is that the setup
// script has been marked as closed but we haven't processed the
// exit event yet.
debug!(?setup_script.id, "failed to send request to setup script (likely closed)");
} else {
count += 1;
}
}
for (key, instance) in &self.running_tests {
if instance.req_tx.send(req.clone()).is_err() {
// The most likely reason for this error is that the test
// instance has been marked as closed but we haven't processed
// the exit event yet.
debug!(
?key,
"failed to send request to test instance (likely closed)"
);
} else {
count += 1;
}
}
count
}
fn handle_signal_event(&mut self, event: SignalEvent) -> HandleEventResponse {
match event {
SignalEvent::Shutdown(event) => {
// TestFailureImmediate doesn't participate in signal count escalation.
// It can only happen once and doesn't escalate to Twice on repetition.
let req = match event {
ShutdownEvent::TestFailureImmediate => ShutdownRequest::Once(event),
ShutdownEvent::Signal(_) => {
let signal_count = self.increment_signal_count();
signal_count.to_request(event)
}
};
let cancel_reason = event_to_cancel_reason(event);
self.begin_cancel(cancel_reason, CancelEvent::Signal(req))
}
#[cfg(unix)]
SignalEvent::JobControl(JobControlEvent::Stop) => {
// Debounce stop signals.
if !self.stopwatch.is_paused() {
self.basic_callback(TestEventKind::RunPaused {
setup_scripts_running: self.setup_scripts_running(),
running: self.running(),
});
self.stopwatch.pause();
HandleEventResponse::JobControl(JobControlEvent::Stop)
} else {
HandleEventResponse::None
}
}
#[cfg(unix)]
SignalEvent::JobControl(JobControlEvent::Continue) => {
// Debounce continue signals.
if self.stopwatch.is_paused() {
self.stopwatch.resume();
self.basic_callback(TestEventKind::RunContinued {
setup_scripts_running: self.setup_scripts_running(),
running: self.running(),
});
HandleEventResponse::JobControl(JobControlEvent::Continue)
} else {
HandleEventResponse::None
}
}
SignalEvent::Info(event) => HandleEventResponse::Info(InfoEvent::Signal(event)),
}
}
fn info_started(&mut self, total: usize) {
self.basic_callback(TestEventKind::InfoStarted {
// Due to a race between units exiting and the info request being
// broadcast, we rely on the info event's receiver count to
// determine how many responses we're expecting. We expect every
// unit that gets a request to return a response.
total,
run_stats: self.run_stats,
});
}
fn info_response(&mut self, index: usize, total: usize, response: InfoResponse<'a>) {
self.basic_callback(TestEventKind::InfoResponse {
index,
total,
response,
});
}
fn info_finished(&mut self, missing: usize) {
self.basic_callback(TestEventKind::InfoFinished { missing });
}
fn increment_signal_count(&mut self) -> SignalCount {
let new_count = match self.signal_count {
None => SignalCount::Once,
Some(SignalCount::Once) => SignalCount::Twice,
Some(SignalCount::Twice) => {
// The process was signaled 3 times. Time to panic.
#[cfg(test)]
{
if self.disable_signal_3_times_panic {
SignalCount::Twice
} else {
// TODO: a panic here won't currently lead to other
// tasks being cancelled. This should be fixed.
panic!("Signaled 3 times, exiting immediately");
}
}
#[cfg(not(test))]
panic!("Signaled 3 times, exiting immediately");
}
};
self.signal_count = Some(new_count);
new_count
}
/// Begin cancellation of a test run. Report it if the current cancel state
/// is less than the required one.
///
/// Returns the corresponding `HandleEventResponse`.
fn begin_cancel(&mut self, reason: CancelReason, event: CancelEvent) -> HandleEventResponse {
// TODO: combine reason and event? The Twice block ignoring the event
// seems to indicate a data modeling issue.
if event == CancelEvent::Signal(ShutdownRequest::Twice) {
// Forcibly kill child processes in the case of a second shutdown
// signal.
self.run_stats.cancel_reason = Some(CancelReason::SecondSignal);
self.basic_callback(TestEventKind::RunBeginKill {
setup_scripts_running: self.setup_scripts_running(),
current_stats: self.run_stats,
running: self.running(),
});
HandleEventResponse::Cancel(event)
} else if self.run_stats.cancel_reason < Some(reason) {
self.run_stats.cancel_reason = Some(reason);
self.basic_callback(TestEventKind::RunBeginCancel {
setup_scripts_running: self.setup_scripts_running(),
current_stats: self.run_stats,
running: self.running(),
});
HandleEventResponse::Cancel(event)
} else {
HandleEventResponse::None
}
}
pub(super) fn run_finished(&mut self) {
let stopwatch_end = self.stopwatch.snapshot();
let stress_stats = self.stress_cx.run_stats(self.run_stats.summarize_final());
let (stress_completed, stress_success, stress_failed) = match &stress_stats {
Some(stats) => (
Some(stats.completed.current),
Some(stats.success_count),
Some(stats.failed_count),
),
None => (None, None, None),
};
crate::fire_usdt!(UsdtRunDone {
run_id: self.run_id,
profile_name: self.profile_name.clone(),
total_tests: self.run_stats.initial_run_count,
passed: self.run_stats.passed,
failed: self.run_stats.failed_count(),
skipped: self.run_stats.skipped,
duration_nanos: stopwatch_end.active.as_nanos() as u64,
paused_nanos: stopwatch_end.paused.as_nanos() as u64,
stress_completed,
stress_success,
stress_failed,
});
let rerun_cx = mem::replace(&mut self.rerun_cx, DispatcherRerunContext::InitialRun);
let tests_not_seen = rerun_cx.into_tests_not_seen();
self.basic_callback(TestEventKind::RunFinished {
start_time: stopwatch_end.start_time.fixed_offset(),
run_id: self.run_id,
elapsed: stopwatch_end.active,
run_stats: stress_stats.map_or_else(
|| RunFinishedStats::Single(self.run_stats),
RunFinishedStats::Stress,
),
outstanding_not_seen: tests_not_seen,
});
}
pub(super) fn run_stats(&self) -> RunStats {
self.run_stats
}
}
#[derive(Clone, Debug)]
enum DispatcherStressContext {
None,
Stress {
condition: StressCondition,
sub_stopwatch: StopwatchStart,
completed: u32,
failed: u32,
cancelled: bool,
},
}
/// Context for rerun tracking.
///
/// This enum tracks whether the current run is an initial run or a rerun, and
/// maintains the necessary state for computing which expected tests were not
/// seen during a rerun.
#[derive(Clone, Debug)]
enum DispatcherRerunContext {
/// This is an initial run, not a rerun. No tracking needed.
InitialRun,
/// This is a rerun of a previous run.
///
/// Contains the set of tests expected to run. As tests are seen, they are
/// removed from this set. At the end of the run, any remaining tests are
/// reported as "not seen".
Rerun(BTreeSet<OwnedTestInstanceId>),
}
impl DispatcherRerunContext {
fn new(expected_outstanding: Option<BTreeSet<OwnedTestInstanceId>>) -> Self {
match expected_outstanding {
Some(expected) => Self::Rerun(expected),
None => Self::InitialRun,
}
}
/// Marks a test as seen during this run.
fn mark_seen(&mut self, id: TestInstanceId<'_>) {
if let Self::Rerun(expected) = self {
expected.remove(&id as &dyn TestInstanceIdKey);
}
}
/// Returns tests not seen, if this is a rerun and some tests were not seen.
///
/// Returns `None` if this is an initial run.
fn into_tests_not_seen(self) -> Option<TestsNotSeen> {
let Self::Rerun(not_seen) = self else {
return None;
};
let total_not_seen = not_seen.len();
const MAX_DISPLAY: usize = 8;
let sample: Vec<_> = not_seen.into_iter().take(MAX_DISPLAY).collect();
Some(TestsNotSeen {
not_seen: sample,
total_not_seen,
})
}
}
impl DispatcherStressContext {
fn new(condition: Option<StressCondition>) -> Self {
if let Some(condition) = condition {
Self::Stress {
condition,
sub_stopwatch: crate::time::stopwatch(),
completed: 0,
failed: 0,
cancelled: false,
}
} else {
Self::None
}
}
fn condition(&self) -> Option<StressCondition> {
match self {
Self::None => None,
Self::Stress { condition, .. } => Some(condition.clone()),
}
}
fn progress(&self, total_elapsed: Duration) -> Option<StressProgress> {
match self {
Self::None => None,
Self::Stress {
condition,
sub_stopwatch: _,
completed,
failed: _,
cancelled: _,
} => match condition {
StressCondition::Count(total) => Some(StressProgress::Count {
total: *total,
elapsed: total_elapsed,
completed: *completed,
}),
StressCondition::Duration(total) => Some(StressProgress::Time {
total: *total,
elapsed: total_elapsed,
completed: *completed,
}),
},
}
}
#[inline]
fn stress_index(&self) -> Option<StressIndex> {
match self {
Self::None => None,
Self::Stress {
condition,
completed,
..
} => {
// The index starts from 0 so it is the same as the number of
// completed runs.
let current = *completed;
let total = match condition {
StressCondition::Count(StressCount::Count { count }) => Some(*count),
StressCondition::Count(StressCount::Infinite)
| StressCondition::Duration(_) => None,
};
Some(StressIndex { current, total })
}
}
}
fn mark_completed(&mut self, summary: FinalRunStats) -> Duration {
match self {
Self::None => {
panic!("mark_completed called in a non-stress test context");
}
Self::Stress {
condition: _,
sub_stopwatch,
completed,
failed,
cancelled,
} => {
*completed += 1;
match summary {
FinalRunStats::Success => {}
FinalRunStats::NoTestsRun => {
// TODO: We should figure out whether to terminate the
// test run based on this.
}
FinalRunStats::Failed { .. } => {
*failed += 1;
}
FinalRunStats::Cancelled { .. } => {
// In this case, we don't add to the failed count. The
// displayer will take care of displaying the
// cancellation message properly.
*cancelled = true;
}
}
let duration = sub_stopwatch.snapshot().active;
*sub_stopwatch = crate::time::stopwatch();
duration
}
}
}
fn run_stats(&self, last_final_stats: FinalRunStats) -> Option<StressRunStats> {
match self {
Self::None => None,
Self::Stress {
condition: _,
sub_stopwatch: _,
completed,
failed,
cancelled,
} => {
let mut success_count = completed.saturating_sub(*failed);
// If the run is cancelled, there's one less success than we
// thought above.
if *cancelled {
success_count = success_count.saturating_sub(1);
}
Some(StressRunStats {
completed: self.stress_index().expect("we're in the Self::Stress case"),
success_count,
failed_count: *failed,
last_final_stats,
})
}
}
}
#[cfg(unix)]
fn pause_stopwatch(&mut self) {
match self {
Self::None => {}
Self::Stress { sub_stopwatch, .. } => {
sub_stopwatch.pause();
}
}
}
#[cfg(unix)]
fn resume_stopwatch(&mut self) {
match self {
Self::None => {}
Self::Stress { sub_stopwatch, .. } => {
sub_stopwatch.resume();
}
}
}
}
fn event_to_cancel_reason(event: ShutdownEvent) -> CancelReason {
match event {
ShutdownEvent::Signal(sig) => match sig {
#[cfg(unix)]
ShutdownSignalEvent::Hangup | ShutdownSignalEvent::Term | ShutdownSignalEvent::Quit => {
CancelReason::Signal
}
ShutdownSignalEvent::Interrupt => CancelReason::Interrupt,
},
ShutdownEvent::TestFailureImmediate => CancelReason::TestFailureImmediate,
}
}
#[derive(Clone, Debug)]
struct ContextSetupScript<'a> {
id: ScriptId,
// Store these details primarily for debugging.
#[expect(dead_code)]
config: &'a SetupScriptConfig,
#[expect(dead_code)]
index: usize,
#[expect(dead_code)]
total: usize,
req_tx: UnboundedSender<RunUnitRequest<'a>>,
}
#[derive(Clone, Debug)]
struct ContextTestInstance<'a> {
// Store the instance primarily for debugging.
#[expect(dead_code)]
instance: TestInstance<'a>,
past_attempts: Vec<ExecuteStatus<LiveSpec>>,
req_tx: UnboundedSender<RunUnitRequest<'a>>,
flaky_result: FlakyResult,
}
impl ContextTestInstance<'_> {
fn attempt_failed_will_retry(&mut self, run_status: ExecuteStatus<LiveSpec>) {
self.past_attempts.push(run_status);
}
fn finish(self, last_run_status: ExecuteStatus<LiveSpec>) -> ExecutionStatuses<LiveSpec> {
let mut attempts = self.past_attempts;
attempts.push(last_run_status);
ExecutionStatuses::new(attempts, self.flaky_result)
}
}
// Almost all events are executor events, which is much larger than the others,
// so it doesn't make sense to optimize for the rare signal and input events.
#[expect(clippy::large_enum_variant)]
#[derive(Debug)]
enum InternalEvent<'a> {
Tick,
Executor(ExecutorEvent<'a>),
Signal(SignalEvent),
Input(InputEvent),
ReportCancel,
GlobalTimeout,
}
/// The return result of `handle_event`.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[must_use = "this enum should not be dropped on the floor"]
enum HandleEventResponse {
/// Stop or continue the run.
#[cfg_attr(not(unix), expect(dead_code))]
JobControl(JobControlEvent),
/// Request information from running units.
Info(InfoEvent),
/// Cancel the run.
Cancel(CancelEvent),
/// No response.
///
/// We use `None` here rather than `Option` because we've found that
/// `Option` enables using `?`, which can lead to incorrect results.
None,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum InfoEvent {
Signal(SignalInfoEvent),
Input,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum CancelEvent {
Report,
TestFailure,
GlobalTimeout,
Signal(ShutdownRequest),
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
enum SignalCount {
Once,
Twice,
}
impl SignalCount {
fn to_request(self, event: ShutdownEvent) -> ShutdownRequest {
match self {
Self::Once => ShutdownRequest::Once(event),
Self::Twice => ShutdownRequest::Twice,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::sync::Mutex;
#[test]
fn begin_cancel_report_signal_interrupt() {
// TODO: also test TestFinished and SetupScriptFinished events.
let events = Mutex::new(Vec::new());
let mut cx = DispatcherContext::new(
|event| match event {
ReporterEvent::Test(event) => {
events.lock().unwrap().push(event);
}
ReporterEvent::Tick => {
// Ignore tick events here.
}
},
ReportUuid::new_v4(),
"default",
vec![],
0,
MaxFail::All,
crate::time::far_future_duration(),
None, // stress_condition
None, // expected_outstanding
);
cx.disable_signal_3_times_panic = true;
// Begin cancellation with a report error.
let response = cx.handle_event(InternalEvent::ReportCancel);
assert_eq!(
response,
HandleEventResponse::Cancel(CancelEvent::Report),
"expected report"
);
{
let mut events = events.lock().unwrap();
assert_eq!(events.len(), 1, "expected 1 event");
let event = events.pop().unwrap();
let TestEventKind::RunBeginCancel {
setup_scripts_running,
current_stats,
running,
} = event.kind
else {
panic!("expected RunBeginCancel event, found {:?}", event.kind);
};
assert_eq!(setup_scripts_running, 0, "expected 0 setup scripts running");
assert_eq!(running, 0, "expected 0 tests running");
assert_eq!(
current_stats.cancel_reason,
Some(CancelReason::ReportError),
"expected report error"
);
}
// Send another report error, ensuring it's ignored.
let response = cx.handle_event(InternalEvent::ReportCancel);
assert_noop(response, &events);
// Save a copy before TestFailureImmediate for later tests.
let cx_before_test_failure = cx.clone();
// Test TestFailureImmediate after ReportCancel - should upgrade.
let response = cx.handle_event(InternalEvent::Signal(SignalEvent::Shutdown(
ShutdownEvent::TestFailureImmediate,
)));
assert_eq!(
response,
HandleEventResponse::Cancel(CancelEvent::Signal(ShutdownRequest::Once(
ShutdownEvent::TestFailureImmediate
))),
"expected TestFailureImmediate"
);
{
let mut events = events.lock().unwrap();
assert_eq!(events.len(), 1, "expected 1 event");
let event = events.pop().unwrap();
let TestEventKind::RunBeginCancel {
setup_scripts_running,
current_stats,
running,
} = event.kind
else {
panic!("expected RunBeginCancel event, found {:?}", event.kind);
};
assert_eq!(setup_scripts_running, 0, "expected 0 setup scripts running");
assert_eq!(running, 0, "expected 0 tests running");
assert_eq!(
current_stats.cancel_reason,
Some(CancelReason::TestFailureImmediate),
"expected test failure immediate"
);
}
// Send another TestFailureImmediate, ensuring it's ignored (no escalation like signals).
let response = cx.handle_event(InternalEvent::Signal(SignalEvent::Shutdown(
ShutdownEvent::TestFailureImmediate,
)));
assert_noop(response, &events);
// Send a report error after TestFailureImmediate, ensuring it's ignored.
let response = cx.handle_event(InternalEvent::ReportCancel);
assert_noop(response, &events);
// The rules:
// * Any one signal will cause that signal.
// * Any two signals received will cause a SIGKILL.
// * After a signal is received, any less-important cancel-worthy events
// are ignored.
// * TestFailureImmediate acts like a signal but doesn't escalate on repetition.
//
// Interestingly, this state machine appears to function on Windows too
// (though of course the only variant is an Interrupt so this only runs
// one iteration.) Should it be different? No compelling reason to be
// yet.
for sig1 in ShutdownSignalEvent::ALL_VARIANTS {
for sig2 in ShutdownSignalEvent::ALL_VARIANTS {
eprintln!("** testing {sig1:?} -> {sig2:?}");
// Separate test for each signal to avoid mixing up state.
let mut cx = cx.clone();
// First signal.
let response = cx.handle_event(InternalEvent::Signal(SignalEvent::Shutdown(
ShutdownEvent::Signal(*sig1),
)));
assert_eq!(
response,
HandleEventResponse::Cancel(CancelEvent::Signal(ShutdownRequest::Once(
ShutdownEvent::Signal(*sig1)
))),
"expected Once"
);
{
let mut events = events.lock().unwrap();
assert_eq!(events.len(), 1, "expected 1 event");
let event = events.pop().unwrap();
let TestEventKind::RunBeginCancel {
setup_scripts_running,
current_stats,
running,
} = event.kind
else {
panic!("expected RunBeginCancel event, found {:?}", event.kind);
};
assert_eq!(setup_scripts_running, 0, "expected 0 setup scripts running");
assert_eq!(running, 0, "expected 0 tests running");
assert_eq!(
current_stats.cancel_reason,
Some(event_to_cancel_reason(ShutdownEvent::Signal(*sig1))),
"expected signal"
);
}
// Another report error, ensuring it's ignored.
let response = cx.handle_event(InternalEvent::ReportCancel);
assert_noop(response, &events);
// Second signal.
let response = cx.handle_event(InternalEvent::Signal(SignalEvent::Shutdown(
ShutdownEvent::Signal(*sig2),
)));
assert_eq!(
response,
HandleEventResponse::Cancel(CancelEvent::Signal(ShutdownRequest::Twice)),
"expected kill"
);
{
let mut events = events.lock().unwrap();
assert_eq!(events.len(), 1, "expected 1 events");
let event = events.pop().unwrap();
let TestEventKind::RunBeginKill {
setup_scripts_running,
current_stats,
running,
} = event.kind
else {
panic!("expected RunBeginKill event, found {:?}", event.kind);
};
assert_eq!(setup_scripts_running, 0, "expected 0 setup scripts running");
assert_eq!(running, 0, "expected 0 tests running");
assert_eq!(
current_stats.cancel_reason,
Some(CancelReason::SecondSignal),
"expected second signal"
);
}
// Another report error, ensuring it's ignored.
let response = cx.handle_event(InternalEvent::ReportCancel);
assert_noop(response, &events);
// TestFailureImmediate after signal should be ignored (signal is more severe).
let response = cx.handle_event(InternalEvent::Signal(SignalEvent::Shutdown(
ShutdownEvent::TestFailureImmediate,
)));
assert_noop(response, &events);
}
}
// Test that signals upgrade from TestFailureImmediate.
for sig in ShutdownSignalEvent::ALL_VARIANTS {
eprintln!("** testing TestFailureImmediate -> {sig:?}");
// Separate test for each signal to avoid mixing up state.
// Clone from before TestFailureImmediate was sent.
let mut cx = cx_before_test_failure.clone();
// First, send TestFailureImmediate.
let response = cx.handle_event(InternalEvent::Signal(SignalEvent::Shutdown(
ShutdownEvent::TestFailureImmediate,
)));
assert_eq!(
response,
HandleEventResponse::Cancel(CancelEvent::Signal(ShutdownRequest::Once(
ShutdownEvent::TestFailureImmediate
))),
"expected TestFailureImmediate"
);
{
let mut events = events.lock().unwrap();
assert_eq!(events.len(), 1, "expected 1 event");
let event = events.pop().unwrap();
let TestEventKind::RunBeginCancel {
setup_scripts_running,
current_stats,
running,
} = event.kind
else {
panic!("expected RunBeginCancel event, found {:?}", event.kind);
};
assert_eq!(setup_scripts_running, 0, "expected 0 setup scripts running");
assert_eq!(running, 0, "expected 0 tests running");
assert_eq!(
current_stats.cancel_reason,
Some(CancelReason::TestFailureImmediate),
"expected test failure immediate"
);
}
// Now send a signal - should upgrade.
let response = cx.handle_event(InternalEvent::Signal(SignalEvent::Shutdown(
ShutdownEvent::Signal(*sig),
)));
assert_eq!(
response,
HandleEventResponse::Cancel(CancelEvent::Signal(ShutdownRequest::Once(
ShutdownEvent::Signal(*sig)
))),
"expected signal upgrade"
);
{
let mut events = events.lock().unwrap();
assert_eq!(events.len(), 1, "expected 1 event");
let event = events.pop().unwrap();
let TestEventKind::RunBeginCancel {
setup_scripts_running,
current_stats,
running,
} = event.kind
else {
panic!("expected RunBeginCancel event, found {:?}", event.kind);
};
assert_eq!(setup_scripts_running, 0, "expected 0 setup scripts running");
assert_eq!(running, 0, "expected 0 tests running");
assert_eq!(
current_stats.cancel_reason,
Some(event_to_cancel_reason(ShutdownEvent::Signal(*sig))),
"expected signal cancel reason"
);
}
// A second signal should cause a kill.
let response = cx.handle_event(InternalEvent::Signal(SignalEvent::Shutdown(
ShutdownEvent::Signal(*sig),
)));
assert_eq!(
response,
HandleEventResponse::Cancel(CancelEvent::Signal(ShutdownRequest::Twice)),
"expected kill"
);
{
let mut events = events.lock().unwrap();
assert_eq!(events.len(), 1, "expected 1 event");
let event = events.pop().unwrap();
let TestEventKind::RunBeginKill {
setup_scripts_running,
current_stats,
running,
} = event.kind
else {
panic!("expected RunBeginKill event, found {:?}", event.kind);
};
assert_eq!(setup_scripts_running, 0, "expected 0 setup scripts running");
assert_eq!(running, 0, "expected 0 tests running");
assert_eq!(
current_stats.cancel_reason,
Some(CancelReason::SecondSignal),
"expected second signal"
);
}
}
}
#[track_caller]
fn assert_noop(response: HandleEventResponse, events: &Mutex<Vec<Box<TestEvent<'_>>>>) {
assert_eq!(response, HandleEventResponse::None, "expected no response");
assert_eq!(events.lock().unwrap().len(), 0, "expected no new events");
}
}