12#include "gtest/gtest.h"
30struct RecordingLogger :
public Logger {
31 RecordingLogger() : Logger(false, false) {}
32 void log(Level level,
const std::string &,
const std::string &msg,
33 const std::map<std::string, std::any> *)
override {
34 std::lock_guard<std::mutex> lock(mutex);
35 if (level == Level::Error)
36 errors.push_back(msg);
38 std::vector<std::string> getErrors() {
39 std::lock_guard<std::mutex> lock(mutex);
43 std::vector<std::string> errors;
46TEST(AcceleratorServiceThreadTest, TaskExceptionsAreLogged) {
47 Context ctxt(std::make_unique<RecordingLogger>());
48 auto ownedLogger = std::make_unique<RecordingLogger>();
49 RecordingLogger &logger = *ownedLogger;
50 std::atomic<int> throwingRuns = 0, otherRuns = 0;
54 ctxt.setLogger(std::move(ownedLogger));
55 serviceThread.addTask([&]() {
56 if (throwingRuns++ == 0)
57 throw std::runtime_error(
"task failure");
59 serviceThread.addTask([&]() { ++otherRuns; });
62 auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(10);
63 while ((throwingRuns < 2 || otherRuns < 2) &&
64 std::chrono::steady_clock::now() < deadline)
65 std::this_thread::sleep_for(std::chrono::milliseconds(1));
67 EXPECT_GE(throwingRuns, 2);
68 EXPECT_GE(otherRuns, 2);
69 std::vector<std::string> errors = logger.getErrors();
70 ASSERT_EQ(errors.size(), 1u);
71 EXPECT_NE(errors[0].find(
"task failure"), std::string::npos);
static void log(char *epId, bool toClient, const MessageData &msg)
Emit the contents of 'msg' to the log file in hex.
Background thread which services various requests.
AcceleratorConnections, Accelerators, and Manifests must all share a context.