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
|
#include <chrono>
#include <QtTest>
#include "widgets/JttyTxLifecycle.hpp"
class TestJttyTxLifecycle final : public QObject
{
Q_OBJECT
private slots:
void pendingSessionRetainsBackendIdentity ()
{
JttyTxLifecycle lifecycle;
auto const epoch = TxAudioQueueEpoch {7};
QVERIFY (lifecycle.begin (JttyTxLifecycle::Backend::Tci, epoch));
QVERIFY (lifecycle.addPending (101, 201));
QCOMPARE (lifecycle.phase (), JttyTxLifecycle::Phase::AwaitingEnqueue);
QCOMPARE (lifecycle.backend (), JttyTxLifecycle::Backend::Tci);
QCOMPARE (lifecycle.epoch (), epoch);
QCOMPARE (lifecycle.pending ().size (), std::size_t {1});
QCOMPARE (lifecycle.pending ().front ().request_id, 201);
}
void stopSnapshotIsImmutable ()
{
JttyTxLifecycle lifecycle;
auto const epoch = lifecycle.begin (JttyTxLifecycle::Backend::Local);
QVERIFY (epoch.isValid ());
QVERIFY (lifecycle.addPending (1, 2));
TxAudioQueueProgress progress;
progress.epoch = epoch;
progress.queued_samples = 3600;
progress.served_samples = 1200;
progress.total_samples = 4800;
auto const accepted = lifecycle.accept (epoch, 1, progress);
QVERIFY (accepted.pending.has_value ());
QVERIFY (lifecycle.markTransmitting ());
auto const first = lifecycle.beginStop ();
QVERIFY (first.has_value ());
QCOMPARE (first->backend, JttyTxLifecycle::Backend::Local);
QCOMPARE (first->epoch, epoch);
QCOMPARE (first->progress.queued_samples, 3600);
QCOMPARE (first->progress.served_samples, 1200);
QCOMPARE (first->progress.total_samples, 4800);
QVERIFY (!lifecycle.begin (JttyTxLifecycle::Backend::Tci,
TxAudioQueueEpoch {99}));
QVERIFY (!lifecycle.addPending (3, 4));
QVERIFY (!lifecycle.accept (epoch, 3, 9600).pending.has_value ());
auto const repeated = lifecycle.beginStop ();
QVERIFY (repeated.has_value ());
QCOMPARE (repeated->backend, first->backend);
QCOMPARE (repeated->epoch, first->epoch);
QCOMPARE (repeated->progress.total_samples, first->progress.total_samples);
}
void acceptanceAdvancingTotalDiscardsDeferredDrain ()
{
JttyTxLifecycle lifecycle;
auto const epoch = lifecycle.begin (JttyTxLifecycle::Backend::Tci);
QVERIFY (lifecycle.addPending (1, 10));
QVERIFY (!lifecycle.observeDrain (epoch, 0).has_value ());
QVERIFY (lifecycle.deferredDrain ().has_value ());
auto const accepted = lifecycle.accept (epoch, 1, 2400);
QVERIFY (accepted.pending.has_value ());
QVERIFY (!accepted.drain.has_value ());
QVERIFY (!lifecycle.deferredDrain ().has_value ());
QCOMPARE (lifecycle.committedTotal (), 2400);
}
void finalFailureReleasesMatchingDeferredDrain ()
{
JttyTxLifecycle lifecycle;
auto const epoch = lifecycle.begin (JttyTxLifecycle::Backend::Tci);
QVERIFY (lifecycle.addPending (1, 10));
QVERIFY (lifecycle.addPending (2, 20));
QVERIFY (!lifecycle.observeDrain (epoch, 0).has_value ());
auto const first = lifecycle.fail (epoch, 1);
QVERIFY (first.pending.has_value ());
QVERIFY (!first.drain.has_value ());
auto const last = lifecycle.fail (epoch, 2);
QVERIFY (last.pending.has_value ());
QVERIFY (last.drain.has_value ());
QCOMPARE (last.drain->epoch, epoch);
QCOMPARE (last.drain->total, 0);
}
void rigCloseCanRejectEveryPendingRequestBeforeAcceptance ()
{
JttyTxLifecycle lifecycle;
auto const epoch = lifecycle.begin (JttyTxLifecycle::Backend::Tci);
QVERIFY (lifecycle.addPending (1, 11));
QVERIFY (lifecycle.addPending (2, 22));
auto const failed = lifecycle.failAll ();
QCOMPARE (failed.pending.size (), std::size_t {2});
QCOMPARE (failed.pending.at (0).request_id, 11);
QCOMPARE (failed.pending.at (1).request_id, 22);
QVERIFY (!lifecycle.hasPending ());
auto const stop = lifecycle.beginStop ();
QVERIFY (stop.has_value ());
QCOMPARE (stop->backend, JttyTxLifecycle::Backend::Tci);
QVERIFY (!lifecycle.accept (epoch, 1, 100).pending.has_value ());
}
void localStopCanRejectPendingAfterSnapshot ()
{
JttyTxLifecycle lifecycle;
auto const epoch = lifecycle.begin (JttyTxLifecycle::Backend::Tci);
QVERIFY (lifecycle.addPending (1, 11));
auto const stop = lifecycle.beginStop ();
QVERIFY (stop.has_value ());
QVERIFY (!lifecycle.fail (epoch, 1).pending.has_value ());
auto const failed = lifecycle.failAll ();
QCOMPARE (failed.pending.size (), std::size_t {1});
QCOMPARE (failed.pending.front ().request_id, 11);
QVERIFY (!failed.drain.has_value ());
}
void preacceptanceTimeoutIsConfigurableAndStartsWithFirstPending ()
{
using namespace std::chrono_literals;
JttyTxLifecycle lifecycle {25ms};
auto const epoch = lifecycle.begin (JttyTxLifecycle::Backend::Local);
auto const start = JttyTxLifecycle::TimePoint {1s};
QVERIFY (!lifecycle.preacceptanceDeadline ().has_value ());
QVERIFY (lifecycle.addPending (1, 1, start));
QCOMPARE (lifecycle.preacceptanceTimeout (), 25ms);
QCOMPARE (*lifecycle.preacceptanceDeadline (), start + 25ms);
QVERIFY (!lifecycle.preacceptanceTimedOut (start + 24ms));
QVERIFY (lifecycle.preacceptanceTimedOut (start + 25ms));
QVERIFY (lifecycle.addPending (2, 2, start + 20ms));
QCOMPARE (*lifecycle.preacceptanceDeadline (), start + 25ms);
QVERIFY (lifecycle.accept (epoch, 1, 10).pending.has_value ());
QVERIFY (lifecycle.preacceptanceDeadline ().has_value ());
QVERIFY (lifecycle.fail (epoch, 2).pending.has_value ());
QVERIFY (!lifecycle.preacceptanceDeadline ().has_value ());
}
void resetRequiresBackendStopRouting ()
{
JttyTxLifecycle lifecycle;
auto const epoch = lifecycle.begin (JttyTxLifecycle::Backend::Local);
QVERIFY (lifecycle.addPending (1, 1));
QVERIFY (lifecycle.accept (epoch, 1, 10).pending.has_value ());
auto const stop = lifecycle.beginStop ();
QVERIFY (stop.has_value ());
QVERIFY (!lifecycle.reset ());
QCOMPARE (lifecycle.phase (), JttyTxLifecycle::Phase::Stopping);
QCOMPARE (lifecycle.backend (), JttyTxLifecycle::Backend::Local);
auto wrong = *stop;
wrong.backend = JttyTxLifecycle::Backend::Tci;
QVERIFY (!lifecycle.markBackendStopRouted (wrong));
QVERIFY (!lifecycle.reset ());
QVERIFY (lifecycle.markBackendStopRouted (*stop));
QVERIFY (lifecycle.reset ());
QCOMPARE (lifecycle.phase (), JttyTxLifecycle::Phase::Idle);
QCOMPARE (lifecycle.backend (), JttyTxLifecycle::Backend::None);
QVERIFY (!lifecycle.accept (epoch, 1, 10).pending.has_value ());
auto const next = lifecycle.begin (JttyTxLifecycle::Backend::Tci);
QCOMPARE (next.value (), epoch.value () + 1);
}
};
QTEST_GUILESS_MAIN (TestJttyTxLifecycle)
#include "test_jtty_tx_lifecycle.moc"
|