STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
test_command_dedupe.cpp
Go to the documentation of this file.
1// A CalibrationCommand delivered twice (two subscriptions to its table) is handled once.
2#include <cstdio>
3#include <cstring>
4
6
8
9static int failures = 0;
10#define CHECK(cond, msg) \
11 do { \
12 if (!(cond)) { \
13 std::printf("FAIL: %s\n", msg); \
14 ++failures; \
15 } \
16 } while (0)
17
18static void make(uint8_t* out, uint64_t ts, uint8_t type, uint16_t sensor, float ref) {
19 std::memset(out, 0, CommandDedupe::kBytes);
20 std::memcpy(out, &ts, 8);
21 out[8] = type;
22 std::memcpy(out + 10, &sensor, 2);
23 std::memcpy(out + 12, &ref, 4);
24}
25
26int main() {
28 uint8_t a[16], b[16];
29
30 make(a, 1000, 5, 4206, 75.f);
31 CHECK(!d.seen(a), "a first command is handled");
32 CHECK(d.seen(a), "the same command arriving again (a second subscription) is dropped");
33
34 // Ten captures sent 120 ms apart, each delivered twice: ten handled, not twenty.
35 int handled = 0;
36 for (int i = 0; i < 10; ++i) {
37 make(b, 2000 + 120 * i, 5, 4202, 100.f);
38 for (int copy = 0; copy < 2; ++copy)
39 if (!d.seen(b))
40 ++handled;
41 }
42 CHECK(handled == 10, "ten captures delivered twice are handled ten times");
43
44 // The same capture repeated by the operator is a new command: a new timestamp.
45 make(a, 5000, 5, 4206, 75.f);
46 make(b, 5001, 5, 4206, 75.f);
47 CHECK(!d.seen(a) && !d.seen(b), "two captures a millisecond apart are both handled");
48
49 // Each field distinguishes a command.
50 uint8_t c[16];
51 make(c, 6000, 5, 4206, 75.f);
52 CHECK(!d.seen(c), "base");
53 make(c, 6000, 6, 4206, 75.f);
54 CHECK(!d.seen(c), "type differs");
55 make(c, 6000, 5, 4207, 75.f);
56 CHECK(!d.seen(c), "sensor differs");
57 make(c, 6000, 5, 4206, 76.f);
58 CHECK(!d.seen(c), "reference differs");
59
60 // Memory is bounded: a command older than kDepth others is forgotten.
62 make(a, 1, 5, 1, 0.f);
63 e.seen(a);
64 for (int i = 0; i < static_cast<int>(CommandDedupe::kDepth); ++i) {
65 make(b, 100 + i, 5, 1, 0.f);
66 e.seen(b);
67 }
68 CHECK(!e.seen(a), "only the last kDepth commands are remembered");
69
70 if (failures == 0)
71 std::printf("command_dedupe: all checks passed\n");
72 return failures == 0 ? 0 : 1;
73}
static constexpr std::size_t kBytes
static constexpr std::size_t kDepth
Drops a CalibrationCommand the service has already handled.
bool seen(const uint8_t *cmd)
true if this exact command was already seen (drop it); otherwise records it.
*brief create JSON pointer *sa reference_tokens const string_t const string_t & b
Definition json.hpp:13878
*brief create JSON pointer *sa reference_tokens const string_t & a
Definition json.hpp:13877
#define CHECK(cond, msg)
static int failures
static void make(uint8_t *out, uint64_t ts, uint8_t type, uint16_t sensor, float ref)
int main()