Files
OrcaSlicer/tests/slic3rutils/test_creality_print.cpp
T
Kris Austin 2054242884 Fix Creality print host hanging on a printer that stops answering (#16772)
ws_connect(), query_boxes_info() and start_print() set expires_after()
deadlines, then made synchronous Beast calls. Beast only applies those
deadlines to async operations, so none of them fired. A printer that
accepted the websocket and then stopped answering blocked the caller
forever. That froze the GUI thread when the Creality send dialog opened
or the sidebar synced filaments, and left uploads stuck after the file
arrived.

Run every websocket call as an async operation on the local io_context,
so the deadlines apply. run_until_done() and the per-address connect
loop move from MoonrakerPrinterAgent.cpp into AsioUtils.hpp, and both
files share them. Each resolved address gets its own connect deadline,
and start_print() gets a reply deadline for its commands. A start-print
timeout shows the translated timeout message that HTTP timeouts already
use, now shared through PrintHost::timed_out_message(), and other
websocket errors drop Boost's source location.

The websocket port and deadlines move into a protected ws_settings
member, so the tests can use a local port and short deadlines. The new
tests run CrealityPrint against a fake printer that goes silent before
or after the handshake.
2026-10-24 22:01:15 -05:00

322 lines
11 KiB
C++

#include <catch2/catch_message.hpp>
#include <catch2/catch_test_macros.hpp>
#include <catch2/generators/catch_generators.hpp>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <cstddef>
#include <cstdlib>
#include <fstream>
#include <future>
#include <iostream>
#include <memory>
#include <mutex>
#include <string>
#include <thread>
#include <tuple>
#include <utility>
#include <vector>
#include <boost/asio/buffer.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/asio/ip/address.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/beast/core/buffers_to_string.hpp>
#include <boost/beast/core/error.hpp>
#include <boost/beast/core/flat_buffer.hpp>
#include <boost/beast/http/message.hpp>
#include <boost/beast/http/read.hpp>
#include <boost/beast/http/status.hpp>
#include <boost/beast/http/string_body.hpp>
#include <boost/beast/http/write.hpp>
#include <boost/beast/websocket/rfc6455.hpp>
#include <boost/beast/websocket/stream.hpp>
#include <nlohmann/json.hpp>
#include <wx/string.h>
#include "libslic3r/Config.hpp"
#include "libslic3r/PrintConfig.hpp"
#include "slic3r/Utils/CrealityPrint.hpp"
#include "slic3r/Utils/Http.hpp"
#include "slic3r/Utils/PrintHost.hpp"
#include "test_utils.hpp"
using namespace Slic3r;
namespace {
namespace beast = boost::beast;
namespace http = beast::http;
namespace websocket = beast::websocket;
namespace net = boost::asio;
using tcp = net::ip::tcp;
using namespace std::chrono_literals;
enum class Behavior {
SilentBeforeHandshake, // Never answers the websocket upgrade
Silent, // Completes the websocket upgrade, then never reads or sends
Answers, // Sends a heartbeat, and the CFS slots once the heartbeat is acknowledged
};
struct Message
{
bool text;
std::string payload;
};
// Serves the printer's HTTP API and its websocket on one port.
class FakeCrealityPrinter
{
public:
FakeCrealityPrinter(Behavior behavior, std::string model) : m_behavior(behavior), m_model(std::move(model))
{
m_accept_thread = std::thread([this] { accept_loop(); });
}
~FakeCrealityPrinter()
{
release();
m_stopping = true;
// Wakes the blocking accept.
tcp::socket wake{m_accept_ioc};
beast::error_code ec;
wake.connect(m_acceptor.local_endpoint(), ec);
m_accept_thread.join();
for (std::thread& connection : m_connections)
connection.join();
}
std::string port() const { return std::to_string(m_acceptor.local_endpoint().port()); }
std::string base_url() const { return "http://127.0.0.1:" + port(); }
std::vector<Message> messages()
{
std::lock_guard<std::mutex> lock(m_mutex);
return m_messages;
}
// Hangs up every connection that is waiting.
void release()
{
std::lock_guard<std::mutex> lock(m_mutex);
m_released = true;
m_changed.notify_all();
}
private:
void accept_loop()
{
for (;;) {
auto ioc = std::make_unique<net::io_context>();
tcp::socket socket{*ioc};
beast::error_code ec;
m_acceptor.accept(socket, ec);
if (ec || m_stopping)
return;
m_connections.emplace_back([this, ioc = std::move(ioc), socket = std::move(socket)]() mutable { serve(std::move(socket)); });
}
}
void serve(tcp::socket socket)
{
beast::flat_buffer buffer;
http::request<http::string_body> request;
beast::error_code ec;
http::read(socket, buffer, request, ec);
if (ec)
return;
if (!websocket::is_upgrade(request)) {
serve_http(socket, request);
return;
}
if (m_behavior == Behavior::SilentBeforeHandshake) {
wait_for_release();
return;
}
websocket::stream<tcp::socket> ws{std::move(socket)};
ws.accept(request, ec);
if (ec)
return;
if (m_behavior == Behavior::Silent) {
wait_for_release();
return;
}
ws.text(true);
// Reading also answers the client's close frame.
for (bool first = true; !ec; first = false) {
beast::flat_buffer message;
ws.read(message, ec);
if (ec)
break;
const std::string payload = beast::buffers_to_string(message.data());
{
std::lock_guard<std::mutex> lock(m_mutex);
m_messages.push_back({ws.got_text(), payload});
}
if (first)
ws.write(net::buffer(std::string(R"({"ModeCode":"heart_beat"})")), ec);
else if (payload == "ok")
ws.write(net::buffer(std::string(R"({"boxsInfo":{"materialBoxs":[]}})")), ec);
}
}
void serve_http(tcp::socket& socket, const http::request<http::string_body>& request)
{
http::response<http::string_body> response{http::status::ok, request.version()};
response.keep_alive(false);
// The upload only needs a success status.
response.body() = request.target() == "/info" ? nlohmann::json{{"model", m_model}}.dump() : "{}";
response.prepare_payload();
beast::error_code ec;
http::write(socket, response, ec);
}
void wait_for_release()
{
std::unique_lock<std::mutex> lock(m_mutex);
m_changed.wait(lock, [this] { return m_released; });
}
const Behavior m_behavior;
const std::string m_model;
net::io_context m_accept_ioc;
tcp::acceptor m_acceptor{m_accept_ioc, {net::ip::make_address("127.0.0.1"), 0}};
std::atomic<bool> m_stopping{false};
std::thread m_accept_thread;
std::vector<std::thread> m_connections;
std::mutex m_mutex;
std::condition_variable m_changed;
std::vector<Message> m_messages;
bool m_released = false;
};
class TestHost : public CrealityPrint
{
public:
using CrealityPrint::CrealityPrint;
using CrealityPrint::ws_settings;
};
std::unique_ptr<TestHost> make_host(const FakeCrealityPrinter& printer, std::chrono::milliseconds deadline)
{
DynamicPrintConfig config;
config.set_key_value("print_host", new ConfigOptionString(printer.base_url()));
config.set_key_value("print_host_webui", new ConfigOptionString(""));
config.set_key_value("printhost_cafile", new ConfigOptionString(""));
config.set_key_value("printhost_port", new ConfigOptionString(""));
config.set_key_value("printhost_apikey", new ConfigOptionString(""));
config.set_key_value("printhost_ssl_ignore_revoke", new ConfigOptionBool(false));
auto host = std::make_unique<TestHost>(&config);
host->ws_settings.port = printer.port();
host->ws_settings.connect = deadline;
host->ws_settings.reply = deadline;
host->ws_settings.close = deadline;
return host;
}
// Runs `call`, giving it `limit` to return before the printer hangs up on it.
template<class Call> bool finishes_within(FakeCrealityPrinter& printer, std::chrono::milliseconds limit, Call&& call)
{
auto done = std::async(std::launch::async, std::forward<Call>(call));
const bool in_time = done.wait_for(limit) == std::future_status::ready;
printer.release();
// Exits instead of returning, since the future's destructor would wait forever for a call the hang-up cannot unblock.
if (done.wait_for(10s) != std::future_status::ready) {
std::cerr << "CrealityPrint call still running 10 s after the printer hung up" << std::endl;
std::_Exit(EXIT_FAILURE);
}
return in_time;
}
bool upload_and_print(const CrealityPrint& host, wxString& error)
{
const ScopedTemporaryFile source(".gcode");
std::ofstream(source.string()) << "G28\n";
PrintHostUpload upload;
upload.source_path = source.path();
upload.upload_path = "test.gcode";
upload.post_action = PrintHostPostUploadAction::StartPrint;
return host.upload(upload, [](Http::Progress, bool&) {}, [&](wxString message) { error = message; }, [](wxString, wxString) {});
}
} // namespace
TEST_CASE("Querying the CFS slots gives a silent printer no more than its deadlines", "[CrealityPrint]")
{
const Behavior behavior = GENERATE(Behavior::SilentBeforeHandshake, Behavior::Silent);
CAPTURE(static_cast<int>(behavior));
FakeCrealityPrinter printer(behavior, "F008");
const auto host = make_host(printer, 200ms);
std::string reply;
CHECK(finishes_within(printer, 2s, [&] { reply = host->query_boxes_info(); }));
CHECK(reply.empty());
}
TEST_CASE("Querying the CFS slots acknowledges heartbeats in text frames", "[CrealityPrint]")
{
FakeCrealityPrinter printer(Behavior::Answers, "F008");
const auto host = make_host(printer, 5s);
CHECK(host->query_boxes_info() == R"({"boxsInfo":{"materialBoxs":[]}})");
const std::vector<Message> messages = printer.messages();
REQUIRE(messages.size() == 2);
CHECK(messages[0].text);
CHECK(nlohmann::json::parse(messages[0].payload) == nlohmann::json{{"method", "get"}, {"params", {{"boxsInfo", 1}}}});
CHECK(messages[1].text);
CHECK(messages[1].payload == "ok");
}
TEST_CASE("Starting a print gives a silent printer no more than its deadlines", "[CrealityPrint]")
{
// F008 is a multi-color model and F001 a plain one. Without a handshake the start command is never sent, so the upload fails.
const auto [behavior, model, started] = GENERATE(table<Behavior, std::string, bool>({
{Behavior::SilentBeforeHandshake, "F008", false},
{Behavior::Silent, "F008", true},
{Behavior::SilentBeforeHandshake, "F001", false},
{Behavior::Silent, "F001", true},
}));
CAPTURE(static_cast<int>(behavior), model);
FakeCrealityPrinter printer(behavior, model);
const auto host = make_host(printer, 200ms);
bool result = !started;
wxString error;
CHECK(finishes_within(printer, 3s, [&] { result = upload_and_print(*host, error); }));
CHECK(result == started);
CHECK(error.empty() == started);
CHECK(error.StartsWith("Connection timed out") == !started);
}
TEST_CASE("Starting a print sends each command with the G-code path in a text frame", "[CrealityPrint]")
{
// F008 is a multi-color model and F001 a plain one. K1-family models keep their G-code under /usr/data.
const auto [model, commands, gcode] = GENERATE(table<std::string, std::vector<std::string>, std::string>({
{"F008", {"colorMatch", "multiColorPrint"}, "/mnt/UDISK/printer_data/gcodes/test.gcode"},
{"F001", {"opGcodeFile"}, "printprt:/mnt/UDISK/printer_data/gcodes/test.gcode"},
{"K1C", {"colorMatch", "multiColorPrint"}, "/usr/data/printer_data/gcodes/test.gcode"},
}));
CAPTURE(model);
FakeCrealityPrinter printer(Behavior::Answers, model);
const auto host = make_host(printer, 5s);
wxString error;
CHECK(upload_and_print(*host, error));
CHECK(error.empty());
const std::vector<Message> messages = printer.messages();
REQUIRE(messages.size() == commands.size());
for (std::size_t i = 0; i < messages.size(); ++i) {
CAPTURE(i);
CHECK(messages[i].text);
const nlohmann::json params = nlohmann::json::parse(messages[i].payload).at("params");
REQUIRE(params.contains(commands[i]));
CHECK(params.at(commands[i]).dump().find(gcode) != std::string::npos);
}
}