Files
OrcaSlicer/tests/slic3rutils/test_printer_sequence_ids.cpp
Hanif Koh 7efe54a80a Number Printer Commands From One Counter That Starts at a Random Point
The device layer numbered its commands from a static int that started at 20000 and never wrapped.
After 10000 commands in one session it left the 20000-29999 band, and replies no longer counted as
the app's own: the printer's command error dialogs, the chamber temperature warnings and firmware
upgrade errors were dropped, and the comm log throttled the app's own replies. The counter was also a
plain int incremented from several places, not atomic.

PrinterSequenceIds is now the counter for device-layer commands: atomic, wrapping inside the band, and
started at a random point in it at each launch, so a printer that remembers recent ids does not take a
new launch's commands for repeats. MachineObject::m_sequence_id refers to it, so existing
`m_sequence_id++` call sites are unchanged. The unused second copy of the band in DevUtil is removed.

The Orca agent's connect requests keep their fixed ids in the band, and its print.gcode_file and
files.* requests keep their nanosecond-clock ids.

At the wrap, DevCtrlInfo's `>=` check on the AMS firmware switch can miss or early-accept a reply once
per 10000 commands; it is bounded by that request's 3 s timeout.
2026-11-30 12:57:06 +08:00

62 lines
2.0 KiB
C++

#include <catch2/catch_test_macros.hpp>
#include <catch2/generators/catch_generators.hpp>
#include <slic3r/GUI/DeviceManager.hpp>
#include <slic3r/Utils/PrinterSequenceIds.hpp>
#include <cstddef>
#include <cstdint>
#include <set>
#include <thread>
#include <vector>
using namespace Slic3r;
TEST_CASE("Request numbers count up by one from where they start", "[PrinterSequenceIds]")
{
PrinterSequenceIds ids(0);
CHECK(ids.next() == 20000);
CHECK(ids.next() == 20001);
CHECK(ids++ == 20002);
}
TEST_CASE("Request numbers wrap inside the band", "[PrinterSequenceIds]")
{
// 9998 is the band's second-to-last offset; a start past the band's size folds into the band.
const std::uint32_t start = GENERATE(9998u, 19998u);
PrinterSequenceIds ids(start);
CHECK(ids.next() == 29998);
CHECK(ids.next() == 29999);
CHECK(ids.next() == 20000);
}
TEST_CASE("Numbers drawn from several threads at once are all different and in the band", "[PrinterSequenceIds]")
{
constexpr int threads = 4;
constexpr int per_thread = 2000; // 8000 numbers, fewer than the band's 10000
PrinterSequenceIds ids(1234);
std::vector<std::vector<int>> drawn(threads);
std::vector<std::thread> workers;
for (int t = 0; t < threads; ++t)
workers.emplace_back([&ids, &drawn, t] {
for (int i = 0; i < per_thread; ++i)
drawn[t].push_back(ids.next());
});
for (std::thread& worker : workers)
worker.join();
std::set<int> unique;
for (const std::vector<int>& numbers : drawn)
unique.insert(numbers.begin(), numbers.end());
CHECK(unique.size() == static_cast<std::size_t>(threads * per_thread));
CHECK(PrinterSequenceIds::in_band(*unique.begin()));
CHECK(PrinterSequenceIds::in_band(*unique.rbegin()));
}
TEST_CASE("The device layer numbers its commands from the shared counter", "[PrinterSequenceIds]")
{
CHECK(&MachineObject::m_sequence_id == &PrinterSequenceIds::shared());
CHECK(PrinterSequenceIds::in_band(MachineObject::m_sequence_id++));
}