CIRCT 24.0.0git
Loading...
Searching...
No Matches
ESIRuntimeServiceThreadTest.cpp
Go to the documentation of this file.
1//===- ESIRuntimeServiceThreadTest.cpp - Service thread tests -------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "esi/Accelerator.h"
10#include "esi/Context.h"
11#include "esi/Logging.h"
12#include "gtest/gtest.h"
13
14#include <any>
15#include <atomic>
16#include <chrono>
17#include <map>
18#include <memory>
19#include <mutex>
20#include <stdexcept>
21#include <string>
22#include <thread>
23#include <vector>
24
25using namespace esi;
26
27namespace {
28
29/// Records the error messages logged to it.
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);
37 }
38 std::vector<std::string> getErrors() {
39 std::lock_guard<std::mutex> lock(mutex);
40 return errors;
41 }
42 std::mutex mutex;
43 std::vector<std::string> errors;
44};
45
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;
51 {
52 AcceleratorServiceThread serviceThread(&ctxt);
53 // The context's current logger is used, even if it was replaced.
54 ctxt.setLogger(std::move(ownedLogger));
55 serviceThread.addTask([&]() {
56 if (throwingRuns++ == 0)
57 throw std::runtime_error("task failure");
58 });
59 serviceThread.addTask([&]() { ++otherRuns; });
60
61 // The thread carries on running both tasks.
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));
66 }
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);
72}
73
74} // namespace
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.
Definition Context.h:34
Definition esi.py:1