fix(udpscope): bound the reconstructed timeline against the wall clock
Round 3 of the Task 4 review. Three defects, all in FrameDecoder rule 3. The resync backstop was one-directional. `predicted` is never below lastEmittedEnd + dt, so rejecting a correction that would step backwards meant only a LAGGING chain could ever be pulled back; a chain running fast drifted ahead without bound. Two hosts' crystals differ by tens of ppm, so a declared SamplingRate is always slightly wrong in one direction or the other and this is certain on a long session. A leading timeline cannot be corrected in one burst without going backwards -- lastEmittedEnd is by definition past arrival -- so the excess is bled off by drawing each burst 10 % narrower until the timeline is back inside the threshold. A repeated packet counter was treated as a normal packet. The C client de-duplicates fragments only, so an unfragmented update reaching a host that joined the group on two interfaces was emitted twice, doubling the values and advancing the timeline by a burst that never existed. The samplingRate == 0 path differenced two HrtRateFit::toSeconds() results. toSeconds() divides an absolute tick count -- ~1e11 on a producer that has been up a day -- by a rate refitted on every packet, so its few-parts-in-1e4 wobble arrives multiplied by the whole elapsed epoch: tens of milliseconds of jitter on a value whose consecutive difference is a few milliseconds. Raw ticks are differenced instead, anchored on the first usable packet so the wobble applies only to the interval since attach. The existing hrt-gap test could not have caught the last one: its 10 ms producer period made the expected answer exactly kDefaultDt, so a decoder that derived nothing passed. It now uses 25 ms. Four tests added, all sabotage-proven. The plan is updated to match. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.6
parent
7102412a9f
commit
3270284cfe
@@ -269,12 +269,101 @@ TEST(FrameDecoder, AccumulatedScalarNeverStepsBackwardsWhenResyncing) {
|
||||
dec.beginFrame(f);
|
||||
ASSERT_TRUE(dec.timestamps(f, 0, ts));
|
||||
|
||||
/* Arrival (500.000) is behind our timeline, so there is nothing to spread
|
||||
* into; the burst is drawn narrower instead, which starts to bleed the lead
|
||||
* off while still moving strictly forwards. */
|
||||
EXPECT_NEAR(ts[0], 500.0909, 1e-9);
|
||||
EXPECT_GT(ts[0], prevEnd) << "resync stepped backwards over the previous burst";
|
||||
for (size_t i = 1; i < ts.size(); i++) {
|
||||
EXPECT_GT(ts[i], ts[i - 1]);
|
||||
}
|
||||
}
|
||||
|
||||
// When the chain has to be abandoned but arrival lies just ahead of where the
|
||||
// last burst ended, the correction is made by COMPRESSING this one burst rather
|
||||
// than by stepping back. Rejecting the correction instead would be one-directional
|
||||
// — `predicted` is never below lastEmittedEnd + dt — and a timeline running fast
|
||||
// could then never be pulled back.
|
||||
TEST(FrameDecoder, AccumulatedScalarCompressesOneBurstRatherThanStepBack) {
|
||||
FrameDecoder dec;
|
||||
dec.setSignals({accSignal()});
|
||||
std::vector<double> ts;
|
||||
primeTenBursts(dec, ts, /*withCounter=*/true);
|
||||
|
||||
/* A counter gap far beyond any real outage: the prediction is unusable, and
|
||||
* the arrival anchor (500.086) sits behind the previous burst end. */
|
||||
FrameBuilder fb;
|
||||
fb.addSignal(std::vector<double>(10, 1.0));
|
||||
const FrameView& f = fb.build(0, 500.095, 10, 900011u);
|
||||
dec.beginFrame(f);
|
||||
ASSERT_TRUE(dec.timestamps(f, 0, ts));
|
||||
|
||||
EXPECT_GT(ts[0], 500.090) << "compressed burst must still start after the last one";
|
||||
EXPECT_NEAR(ts[9], 500.095, 1e-9) << "and end exactly on arrival";
|
||||
EXPECT_NEAR(ts[1] - ts[0], 0.0005, 1e-9) << "spread over the available room";
|
||||
}
|
||||
|
||||
// The whole point of compressing: a declared SamplingRate is a hand-written
|
||||
// config value, and even a correct one is measured against the producer host's
|
||||
// crystal, not ours. Tens of ppm of difference is certain over a long session,
|
||||
// so the reconstructed timeline WILL run away from the wall clock. It has to be
|
||||
// pulled back, and it has to stay monotonic while that happens.
|
||||
TEST(FrameDecoder, AccumulatedScalarDoesNotDriftAwayFromTheWallClockForever) {
|
||||
FrameDecoder dec;
|
||||
dec.setSignals({accSignal()}); /* declares 1 kHz */
|
||||
|
||||
/* The producer really runs 1 % fast: 10 samples take 9.9 ms of wall time,
|
||||
* so a chain stepping the declared 10 ms per packet gains 0.1 ms every
|
||||
* packet. This is the direction re-anchoring alone cannot fix: arrival is
|
||||
* always BEHIND the chain, so anchoring on it would step backwards and is
|
||||
* refused. Only compression pulls the timeline back. */
|
||||
double worstLead = 0.0;
|
||||
double lastEnd = 0.0;
|
||||
for (int p = 0; p < 20000; p++) {
|
||||
FrameBuilder fb;
|
||||
fb.addSignal(std::vector<double>(10, 1.0));
|
||||
const double arrival = 500.0 + p * 0.0099;
|
||||
const FrameView& f =
|
||||
fb.build(0, arrival, 10, static_cast<uint32_t>(p + 1));
|
||||
dec.beginFrame(f);
|
||||
std::vector<double> ts;
|
||||
ASSERT_TRUE(dec.timestamps(f, 0, ts));
|
||||
|
||||
for (size_t i = 0; i < ts.size(); i++) {
|
||||
ASSERT_GT(ts[i], lastEnd) << "timeline went backwards at packet " << p;
|
||||
lastEnd = ts[i];
|
||||
}
|
||||
worstLead = std::max(worstLead, ts[9] - arrival);
|
||||
}
|
||||
|
||||
/* Unchecked, 20000 packets at 0.1 ms each would put the trace 2 s ahead. */
|
||||
EXPECT_LT(worstLead, 0.6) << "timeline drifted " << worstLead << " s ahead";
|
||||
}
|
||||
|
||||
// The C client de-duplicates fragments but not whole unfragmented updates, so a
|
||||
// host subscribed on two interfaces sees each datagram twice. Emitting the
|
||||
// repeat would double the values and advance time by a burst that never was.
|
||||
TEST(FrameDecoder, AccumulatedScalarDropsADuplicatedDatagram) {
|
||||
FrameDecoder dec;
|
||||
dec.setSignals({accSignal()});
|
||||
std::vector<double> ts;
|
||||
primeTenBursts(dec, ts, /*withCounter=*/true);
|
||||
const double endBefore = ts[9];
|
||||
|
||||
FrameBuilder fb;
|
||||
fb.addSignal(std::vector<double>(10, 1.0));
|
||||
const FrameView& dup = fb.build(0, 500.1001, 10, 10u); /* counter 10 again */
|
||||
dec.beginFrame(dup);
|
||||
EXPECT_FALSE(dec.timestamps(dup, 0, ts));
|
||||
|
||||
/* And the drop must not have disturbed the chain: the genuine next packet
|
||||
* still lands one period after burst 10 ended. */
|
||||
const FrameView& next = fb.build(0, 500.109, 10, 11u);
|
||||
dec.beginFrame(next);
|
||||
ASSERT_TRUE(dec.timestamps(next, 0, ts));
|
||||
EXPECT_NEAR(ts[0], endBefore + 0.001, 1e-9);
|
||||
}
|
||||
|
||||
TEST(FrameDecoder, AccumulatedScalarDerivesDtFromTheHrtGapWhenNoRateIsDeclared) {
|
||||
FrameDecoder dec;
|
||||
SignalMeta m;
|
||||
@@ -288,15 +377,17 @@ TEST(FrameDecoder, AccumulatedScalarDerivesDtFromTheHrtGapWhenNoRateIsDeclared)
|
||||
for (int p = 0; p < 40; p++) {
|
||||
FrameBuilder fb;
|
||||
fb.addSignal(std::vector<double>(10, 1.0));
|
||||
const double producerSec = 100.0 + p * 0.010; /* 10 ms per packet */
|
||||
/* Zero-mean arrival jitter, so the rate fit still converges but any
|
||||
* single arrival GAP is wrong. Without it, uniform arrivals make
|
||||
* packetBurst and the hrt path return the same number and the test
|
||||
* cannot tell which branch produced it. The last packet's gap is
|
||||
* 7 ms, which arrival-spanning would render as a 0.7 ms period. */
|
||||
/* 25 ms per packet, deliberately NOT 10: at 10 the expected 1 ms period
|
||||
* equals kDefaultDt, so a decoder that never derived anything and just
|
||||
* returned the default would pass a test named for the derivation. */
|
||||
const double producerSec = 100.0 + p * 0.025;
|
||||
/* Zero-mean arrival jitter, so the rate fit still converges but no
|
||||
* single arrival GAP is right. Without it, uniform arrivals make
|
||||
* packetBurst and the hrt path return the same number by construction
|
||||
* and the test cannot tell which branch answered. */
|
||||
const double jitter[4] = {0.0, 0.003, 0.0, -0.003};
|
||||
const FrameView& f = fb.build(static_cast<uint64_t>(producerSec * ticks),
|
||||
700.0 + p * 0.010 + jitter[p % 4], 10,
|
||||
700.0 + p * 0.025 + jitter[p % 4], 10,
|
||||
static_cast<uint32_t>(p + 1));
|
||||
dec.beginFrame(f);
|
||||
std::vector<double> ts;
|
||||
@@ -304,9 +395,10 @@ TEST(FrameDecoder, AccumulatedScalarDerivesDtFromTheHrtGapWhenNoRateIsDeclared)
|
||||
}
|
||||
|
||||
ASSERT_EQ(last.size(), 10u);
|
||||
/* 10 ms of producer time across 10 samples is a 1 ms period, whatever the
|
||||
* datagrams did on the way over. */
|
||||
EXPECT_NEAR(last[1] - last[0], 0.001, 1e-5);
|
||||
/* 25 ms of producer time across 10 samples is a 2.5 ms period, whatever the
|
||||
* datagrams did on the way over. Arrival-spanning the last gap (22 ms)
|
||||
* would give 2.2 ms; defaulting would give 1 ms. */
|
||||
EXPECT_NEAR(last[1] - last[0], 0.0025, 2e-5);
|
||||
}
|
||||
|
||||
// A PACKET burst has no per-element time at all. Elements span
|
||||
|
||||
Reference in New Issue
Block a user