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
|
#include <QtTest>
#include <QCoreApplication>
#include <QBuffer>
#include <QFile>
#include <QProcess>
#include <QVector>
#include <cstdio>
#include <memory>
#ifdef Q_OS_WIN
#include <fcntl.h>
#include <io.h>
#endif
#include "DecoderOutputFramer.hpp"
namespace
{
char const fixtureOption[] = "--decoder-output-fixture";
int runFixture (QStringList const& arguments)
{
if (arguments.size () < 3) return 2;
auto const payload = QByteArray::fromBase64 (arguments[2].toLatin1 ());
#ifdef Q_OS_WIN
if (-1 == _setmode (_fileno (stdout), _O_BINARY)) return 3;
#endif
QFile output;
if (!output.open (stdout, QIODevice::WriteOnly, QFileDevice::DontCloseHandle))
{
return 4;
}
if (output.write (payload) != payload.size () || !output.flush ()) return 5;
return arguments.contains ("--hold") ? QCoreApplication::exec () : 0;
}
}
class TestDecoderOutputFramer final : public QObject
{
Q_OBJECT
private Q_SLOTS:
void rejectedRecordDoesNotStopDrain ();
void staleFramesRemainAssociatedWithTheirGeneration ();
void completeStateWaitsForStdoutDrain ();
void recordTerminatorsArePreserved ();
void malformedFramesAreReported ();
void killedFrameCanBeReplaced ();
};
void TestDecoderOutputFramer::rejectedRecordDoesNotStopDrain ()
{
QByteArray const output {
"<DecodeStarted> gen=7\n"
"record rejected by policy\n"
"<DecodeFinished> 2 17 123456 gen=7\n"};
DecoderOutputFramer framer;
int started {0};
int records {0};
int finished {0};
DecoderOutputFramer::EventHandler handler {
[&] (DecoderOutputFramer::Event const& event) {
if (DecoderOutputFramer::EventType::Started == event.type) ++started;
if (DecoderOutputFramer::EventType::Record == event.type)
{
++records;
return;
}
if (DecoderOutputFramer::EventType::Finished == event.type) ++finished;
}};
QProcess process;
connect (&process, &QProcess::readyReadStandardOutput, this, [&] {
framer.drain (process, handler);
});
process.start (QCoreApplication::applicationFilePath (),
{fixtureOption, QString::fromLatin1 (output.toBase64 ())});
QVERIFY (process.waitForStarted ());
QVERIFY (process.waitForFinished ());
framer.drain (process, handler);
QCOMPARE (started, 1);
QCOMPARE (records, 1);
QCOMPARE (finished, 1);
QCOMPARE (framer.currentGeneration (), qint32 {0});
}
void TestDecoderOutputFramer::staleFramesRemainAssociatedWithTheirGeneration ()
{
QByteArray const output {
"<DecodeStarted> gen=7\n"
"stale record\n"
"<DecodeFinished> 0 0 0 gen=7\n"
"<DecodeStarted> gen=8\n"
"current record\n"
"<DecodeFinished> 0 1 0 gen=8\n"};
DecoderOutputFramer framer;
QVector<qint32> recordGenerations;
QVector<QByteArray> acceptedRecords;
QVector<qint32> finishedGenerations;
qint32 const activeGeneration {8};
DecoderOutputFramer::EventHandler handler {
[&] (DecoderOutputFramer::Event const& event) {
if (DecoderOutputFramer::EventType::Record == event.type)
{
recordGenerations.append (event.generation);
if (activeGeneration == event.generation)
{
acceptedRecords.append (event.rawLine);
}
}
else if (DecoderOutputFramer::EventType::Finished == event.type)
{
finishedGenerations.append (event.generation);
}
}};
QProcess process;
process.start (QCoreApplication::applicationFilePath (),
{fixtureOption, QString::fromLatin1 (output.toBase64 ())});
QVERIFY (process.waitForStarted ());
QVERIFY (process.waitForFinished ());
framer.drain (process, handler);
QCOMPARE (recordGenerations, QVector<qint32> ({7, 8}));
QCOMPARE (acceptedRecords, QVector<QByteArray> ({"current record\n"}));
QCOMPARE (finishedGenerations, QVector<qint32> ({7, 8}));
}
void TestDecoderOutputFramer::completeStateWaitsForStdoutDrain ()
{
std::unique_ptr<shared_dec_data_t> shared {new shared_dec_data_t};
std::unique_ptr<dec_data_t> payload {new dec_data_t {}};
DecoderIpc::initialize (*shared);
QVERIFY (DecoderIpc::publish (*shared, *payload, false, 21));
qint32 generation {0};
QVERIFY (DecoderIpc::claim (*shared, generation));
QVERIFY (DecoderIpc::finish (*shared, generation));
QByteArray output {
"<DecodeStarted> gen=21\n"
"buffered record\n"
"<DecodeFinished> 0 1 0 gen=21\n"};
QBuffer buffer {&output};
QVERIFY (buffer.open (QIODevice::ReadOnly));
QCOMPARE (DecoderIpc::state (*shared), qint32 {DECODER_IPC_COMPLETE});
DecoderOutputFramer framer;
int records {0};
bool consumed {false};
framer.drain (buffer, [&] (DecoderOutputFramer::Event const& event) {
if (DecoderOutputFramer::EventType::Record == event.type) ++records;
if (DecoderOutputFramer::EventType::Finished == event.type)
{
consumed = DecoderIpc::consume (*shared, event.generation);
}
});
QCOMPARE (records, 1);
QVERIFY (consumed);
QCOMPARE (DecoderIpc::state (*shared), qint32 {DECODER_IPC_IDLE});
}
void TestDecoderOutputFramer::recordTerminatorsArePreserved ()
{
QByteArray const output {
"<DecodeStarted> gen=9\r\n"
"first record\r\n"
"second record\n"
"<DecodeFinished> 0 2 0 gen=9\r\n"};
DecoderOutputFramer framer;
QVector<QByteArray> records;
QProcess process;
process.start (QCoreApplication::applicationFilePath (),
{fixtureOption, QString::fromLatin1 (output.toBase64 ())});
QVERIFY (process.waitForStarted ());
QVERIFY (process.waitForFinished ());
framer.drain (process, [&] (DecoderOutputFramer::Event const& event) {
if (DecoderOutputFramer::EventType::Record == event.type)
{
records.append (event.rawLine);
}
});
QCOMPARE (records, QVector<QByteArray> ({"first record\r\n",
"second record\n"}));
}
void TestDecoderOutputFramer::malformedFramesAreReported ()
{
QByteArray output {
"unframed record\n"
"<DecodeStarted> gen=0\n"
"<DecodeStarted> gen=14\n"
"<DecodeFinished> 0 0 0 gen=15\n"
"<DecodeFinished> 0 0 0 gen=14\n"};
QBuffer buffer {&output};
QVERIFY (buffer.open (QIODevice::ReadOnly));
DecoderOutputFramer framer;
QVector<DecoderOutputFramer::EventType> eventTypes;
framer.drain (buffer, [&] (DecoderOutputFramer::Event const& event) {
eventTypes.append (event.type);
});
QCOMPARE (eventTypes, QVector<DecoderOutputFramer::EventType> ({
DecoderOutputFramer::EventType::Malformed,
DecoderOutputFramer::EventType::Malformed,
DecoderOutputFramer::EventType::Started,
DecoderOutputFramer::EventType::Malformed,
DecoderOutputFramer::EventType::Finished}));
QCOMPARE (framer.currentGeneration (), qint32 {0});
}
void TestDecoderOutputFramer::killedFrameCanBeReplaced ()
{
DecoderOutputFramer framer;
QVector<DecoderOutputFramer::Event> events;
DecoderOutputFramer::EventHandler handler {
[&] (DecoderOutputFramer::Event const& event) {events.append (event);}};
QProcess abandoned;
connect (&abandoned, &QProcess::readyReadStandardOutput, this, [&] {
framer.drain (abandoned, handler);
});
QByteArray const abandonedOutput {
"<DecodeStarted> gen=11\n"
"abandoned record\n"};
abandoned.start (QCoreApplication::applicationFilePath (),
{fixtureOption,
QString::fromLatin1 (abandonedOutput.toBase64 ()),
"--hold"});
QVERIFY (abandoned.waitForStarted ());
QVERIFY (abandoned.waitForReadyRead ());
framer.drain (abandoned, handler);
QCOMPARE (framer.currentGeneration (), qint32 {11});
abandoned.kill ();
QTRY_COMPARE_WITH_TIMEOUT (abandoned.state (), QProcess::NotRunning, 2000);
framer.reset ();
events.clear ();
QByteArray const replacementOutput {
"<DecodeStarted> gen=12\n"
"replacement record\n"
"<DecodeFinished> 0 1 0 gen=12\n"};
QProcess replacement;
connect (&replacement, &QProcess::readyReadStandardOutput, this, [&] {
framer.drain (replacement, handler);
});
replacement.start (QCoreApplication::applicationFilePath (),
{fixtureOption,
QString::fromLatin1 (replacementOutput.toBase64 ())});
QVERIFY (replacement.waitForStarted ());
QVERIFY (replacement.waitForFinished ());
framer.drain (replacement, handler);
QCOMPARE (events.size (), 3);
QCOMPARE (events[0].type, DecoderOutputFramer::EventType::Started);
QCOMPARE (events[0].generation, qint32 {12});
QCOMPARE (events[1].type, DecoderOutputFramer::EventType::Record);
QCOMPARE (events[1].rawLine, QByteArray {"replacement record\n"});
QCOMPARE (events[2].type, DecoderOutputFramer::EventType::Finished);
QCOMPARE (events[2].generation, qint32 {12});
}
int main (int argc, char ** argv)
{
QCoreApplication application {argc, argv};
auto const arguments = application.arguments ();
if (arguments.contains (fixtureOption)) return runFixture (arguments);
TestDecoderOutputFramer test;
return QTest::qExec (&test, argc, argv);
}
#include "test_decoder_output_framer.moc"
|