summaryrefslogtreecommitdiff
path: root/test/test-worker.cpp
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2023-04-14 11:54:45 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2023-04-14 11:54:45 +0100
commitaa77548bd637ffd1a7461136e6206906dc4c61c7 (patch)
treed072bbb555685c1eaad250fdd7a02b7a918e3b24 /test/test-worker.cpp
parentSimplify worker with jthread (diff)
downloadilt-aa77548bd637ffd1a7461136e6206906dc4c61c7.tar.bz2
ilt-aa77548bd637ffd1a7461136e6206906dc4c61c7.tar.xz
ilt-aa77548bd637ffd1a7461136e6206906dc4c61c7.zip
New WorkItem/job/promise/future based interface
Diffstat (limited to 'test/test-worker.cpp')
-rw-r--r--test/test-worker.cpp39
1 files changed, 38 insertions, 1 deletions
diff --git a/test/test-worker.cpp b/test/test-worker.cpp
index 3c5ed7e..c542020 100644
--- a/test/test-worker.cpp
+++ b/test/test-worker.cpp
@@ -2,6 +2,43 @@
#include "testHelpers.h"
#include <boost/test/unit_test.hpp>
+#include <set>
#include <stream_support.hpp>
+#include <worker.h>
-BOOST_AUTO_TEST_CASE(exists) { }
+uint32_t
+workCounter()
+{
+ static std::atomic_uint32_t n;
+ usleep(1000);
+ return n++;
+}
+
+BOOST_AUTO_TEST_CASE(basic_slow_counter)
+{
+ std::vector<Worker::WorkPtrT<uint32_t>> ps;
+ for (int i {}; i < 30; ++i) {
+ ps.push_back(Worker::addWork(workCounter));
+ }
+ std::set<uint32_t> out;
+ std::transform(ps.begin(), ps.end(), std::inserter(out, out.end()), [](auto && p) {
+ return p->get();
+ });
+ BOOST_REQUIRE_EQUAL(out.size(), ps.size());
+ BOOST_CHECK_EQUAL(*out.begin(), 0);
+ BOOST_CHECK_EQUAL(*out.rbegin(), ps.size() - 1);
+}
+
+BOOST_AUTO_TEST_CASE(basic_error_handler)
+{
+ auto workitem = Worker::addWork([]() {
+ throw std::runtime_error {"test"};
+ });
+ BOOST_CHECK_THROW(workitem->get(), std::runtime_error);
+}
+
+BOOST_AUTO_TEST_CASE(basic_void_work)
+{
+ auto workitem = Worker::addWork([]() {});
+ BOOST_CHECK_NO_THROW(workitem->get());
+}