secsi: T3 / T4 enforcement moved into the FSM

The SECS-I Protocol FSM now enforces T3 (reply timeout) and T4
(inter-block timeout) directly, instead of leaving them as
upper-layer hooks.

T3: on complete_send, if the block we just acked had W=1, record its
system_bytes in awaiting_reply_sys_ and emit ActionStartTimer{T3}.
deliver_recv cancels T3 when a block arrives whose system_bytes
match the outstanding request.  EventTimeout{T3} aborts the FSM with
"T3 reply timeout".

T4: deliver_recv emits ActionStartTimer{T4} whenever the delivered
block has end_block=false.  The next block's deliver_recv cancels
the timer; EventTimeout{T4} aborts with "T4 inter-block timeout".

abort() now also cancels T3/T4 and clears the tracking state.

Test changes:
  - Old "T3/T4 are FSM-level no-ops" test → REPLACED by four new
    tests: T3 arm+expire, T3 arm+matching-reply cancels, T4
    arm+expire, T4 arm+next-block cancels.
  - Two new observer accessors on Protocol (awaiting_reply,
    awaiting_next_block) so the tests can assert tracking state
    without poking internals.

COMPLIANCE.md §1a: T3 + T4 rows go .

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-06-09 11:52:43 +02:00
parent 77197b9c1e
commit d4d1a411d7
4 changed files with 217 additions and 11 deletions
+150 -8
View File
@@ -97,19 +97,161 @@ TEST_CASE("SECS-I T2: timeout in RecvBlock aborts (mid-block stall)") {
CHECK(has_abort(out));
}
TEST_CASE("SECS-I T3 / T4: FSM-level no-ops (upper layer enforces)") {
// The protocol docs in protocol.hpp note T3 (reply) and T4 (inter-block)
// are driven from the higher SECS-II / message layer. If a future
// commit adds in-FSM handling for either, this test will break and
// the documentation should be updated alongside.
TEST_CASE("SECS-I T3: fires after a W=1 send is acked, expiry aborts") {
// Send a primary (w_bit=true), peer EOTs, peer ACKs the block. The
// FSM should now be awaiting a reply on those system_bytes. Firing
// T3 aborts the transaction.
Block primary;
primary.header.stream = 1;
primary.header.function = 1;
primary.header.w_bit = true;
primary.header.block_number = 1;
primary.header.end_block = true;
primary.header.system_bytes = 0xCAFEBABE;
Protocol p(Role::Master);
std::vector<Action> out;
p.on_event(EventSend{primary}, out);
out.clear();
p.on_event(EventByte{kEOT}, out); // peer clears us
out.clear();
p.on_event(EventByte{kACK}, out); // peer ACKs the block
// FSM should be back to Idle, awaiting a reply with T3 armed.
CHECK(p.state() == Protocol::State::Idle);
CHECK(p.awaiting_reply());
// The T3 ActionStartTimer should be present in `out`.
bool t3_armed = std::any_of(out.begin(), out.end(), [](const Action& a) {
auto* t = std::get_if<ActionStartTimer>(&a);
return t && t->which == Timer::T3;
});
CHECK(t3_armed);
// Now fire T3.
out.clear();
p.on_event(EventTimeout{Timer::T3}, out);
CHECK(p.state() == Protocol::State::Idle);
CHECK(out.empty());
CHECK_FALSE(p.awaiting_reply());
CHECK(has_abort(out));
bool t3_mentioned = std::any_of(out.begin(), out.end(), [](const Action& a) {
auto* err = std::get_if<ActionRaiseError>(&a);
return err && err->reason.find("T3") != std::string::npos;
});
CHECK(t3_mentioned);
}
TEST_CASE("SECS-I T3: cancels when matching reply arrives") {
// Same setup as above, but instead of T3 expiring we receive a reply
// block whose system_bytes match.
Block primary;
primary.header.stream = 1;
primary.header.function = 1;
primary.header.w_bit = true;
primary.header.end_block = true;
primary.header.block_number = 1;
primary.header.system_bytes = 0xCAFEBABE;
Protocol p(Role::Master);
std::vector<Action> out;
p.on_event(EventSend{primary}, out);
p.on_event(EventByte{kEOT}, out);
out.clear();
p.on_event(EventByte{kACK}, out);
REQUIRE(p.awaiting_reply());
out.clear();
// Peer sends ENQ for the reply.
p.on_event(EventByte{kENQ}, out);
out.clear();
// Then the reply block (header + body + checksum).
Block reply;
reply.header.stream = 1;
reply.header.function = 2;
reply.header.end_block = true;
reply.header.block_number = 1;
reply.header.system_bytes = 0xCAFEBABE;
auto reply_bytes = reply.encode();
for (auto byte : reply_bytes) {
p.on_event(EventByte{byte}, out);
}
// T3 should have been cancelled in deliver_recv.
CHECK_FALSE(p.awaiting_reply());
bool t3_cancelled = std::any_of(out.begin(), out.end(), [](const Action& a) {
auto* t = std::get_if<ActionCancelTimer>(&a);
return t && t->which == Timer::T3;
});
CHECK(t3_cancelled);
}
TEST_CASE("SECS-I T4: fires after a non-final block, expiry aborts") {
// Receive a block whose end_block bit is false (i.e. multi-block
// continuation expected); T4 should be armed, and firing it aborts.
Block mid;
mid.header.stream = 7;
mid.header.function = 3;
mid.header.block_number = 1;
mid.header.end_block = false;
mid.header.system_bytes = 0xDEAD;
auto bytes = mid.encode();
Protocol p(Role::Master);
std::vector<Action> out;
p.on_event(EventByte{kENQ}, out);
out.clear();
for (auto byte : bytes) {
p.on_event(EventByte{byte}, out);
}
CHECK(p.awaiting_next_block());
bool t4_armed = std::any_of(out.begin(), out.end(), [](const Action& a) {
auto* t = std::get_if<ActionStartTimer>(&a);
return t && t->which == Timer::T4;
});
CHECK(t4_armed);
out.clear();
p.on_event(EventTimeout{Timer::T4}, out);
CHECK(p.state() == Protocol::State::Idle);
CHECK(out.empty());
CHECK_FALSE(p.awaiting_next_block());
CHECK(has_abort(out));
bool t4_mentioned = std::any_of(out.begin(), out.end(), [](const Action& a) {
auto* err = std::get_if<ActionRaiseError>(&a);
return err && err->reason.find("T4") != std::string::npos;
});
CHECK(t4_mentioned);
}
TEST_CASE("SECS-I T4: cancels when the next block arrives") {
Block mid;
mid.header.stream = 7;
mid.header.function = 3;
mid.header.block_number = 1;
mid.header.end_block = false;
mid.header.system_bytes = 0xDEAD;
auto bytes1 = mid.encode();
Block final_block;
final_block.header.stream = 7;
final_block.header.function = 3;
final_block.header.block_number = 2;
final_block.header.end_block = true;
final_block.header.system_bytes = 0xDEAD;
auto bytes2 = final_block.encode();
Protocol p(Role::Master);
std::vector<Action> out;
p.on_event(EventByte{kENQ}, out);
for (auto b : bytes1) p.on_event(EventByte{b}, out);
REQUIRE(p.awaiting_next_block());
// Second block. Peer ENQs again (each block is its own send cycle).
out.clear();
p.on_event(EventByte{kENQ}, out);
out.clear();
for (auto b : bytes2) p.on_event(EventByte{b}, out);
CHECK_FALSE(p.awaiting_next_block());
bool t4_cancelled = std::any_of(out.begin(), out.end(), [](const Action& a) {
auto* t = std::get_if<ActionCancelTimer>(&a);
return t && t->which == Timer::T4;
});
CHECK(t4_cancelled);
}
TEST_CASE("SECS-I T2: in send state retries (existing) AND in recv state aborts (new)") {