mirror of
https://github.com/latentPrion/libspinscale.git
synced 2026-08-12 23:48:22 +00:00
spinscale: split probe harness from test support.
Move ProbeComponentThreadHarness into spinscale_probe_support (sscl::probe) so tools can link it without gtest; keep a sscl::tests compatibility shim. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1,118 +0,0 @@
|
||||
#include <support/probeComponentThread.h>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include <spinscale/component.h>
|
||||
|
||||
namespace sscl::tests {
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr sscl::ThreadId PROBE_PUPPETEER_THREAD_ID = 2;
|
||||
|
||||
class ProbeDummyPuppeteerComponent
|
||||
: public sscl::pptr::PuppeteerComponent
|
||||
{
|
||||
public:
|
||||
explicit ProbeDummyPuppeteerComponent(
|
||||
const std::shared_ptr<sscl::PuppeteerThread>& componentThreadIn)
|
||||
: sscl::pptr::PuppeteerComponent(componentThreadIn)
|
||||
{}
|
||||
|
||||
void handleLoopExceptionHook() override
|
||||
{
|
||||
std::cerr << "ProbeComponentThreadHarness: puppeteer loop exception\n";
|
||||
}
|
||||
};
|
||||
|
||||
void probePuppeteerMain(
|
||||
const sscl::PuppeteerThread::EntryFnArguments& args,
|
||||
const std::function<void(
|
||||
const std::shared_ptr<sscl::ComponentThread>&)>& work,
|
||||
std::promise<std::exception_ptr>& donePromise)
|
||||
{
|
||||
sscl::PuppeteerThread& thr = args.usableBeforeJolt;
|
||||
thr.initializeTls();
|
||||
sscl::ComponentThread::setPuppeteerThreadId(PROBE_PUPPETEER_THREAD_ID);
|
||||
|
||||
std::shared_ptr<sscl::PuppeteerThread> thrPtr =
|
||||
std::static_pointer_cast<sscl::PuppeteerThread>(thr.shared_from_this());
|
||||
sscl::ComponentThread::setPuppeteerThread(thrPtr);
|
||||
|
||||
try {
|
||||
work(thrPtr);
|
||||
donePromise.set_value(nullptr);
|
||||
}
|
||||
catch (...) {
|
||||
donePromise.set_value(std::current_exception());
|
||||
}
|
||||
|
||||
thr.getIoContext().stop();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
ProbeComponentThreadHarness::ProbeComponentThreadHarness(
|
||||
const char *threadName)
|
||||
: threadName(threadName),
|
||||
dummyComponent(std::make_shared<ProbeDummyPuppeteerComponent>(
|
||||
std::shared_ptr<sscl::PuppeteerThread>()))
|
||||
{}
|
||||
|
||||
ProbeComponentThreadHarness::~ProbeComponentThreadHarness() = default;
|
||||
|
||||
std::shared_ptr<sscl::ComponentThread>
|
||||
ProbeComponentThreadHarness::componentThread() const
|
||||
{
|
||||
return lastComponentThread;
|
||||
}
|
||||
|
||||
void ProbeComponentThreadHarness::runSync(
|
||||
const std::function<void(
|
||||
const std::shared_ptr<sscl::ComponentThread>&)>& work)
|
||||
{
|
||||
std::promise<std::exception_ptr> donePromise;
|
||||
std::future<std::exception_ptr> doneFuture = donePromise.get_future();
|
||||
|
||||
std::shared_ptr<sscl::PuppeteerThread> runThread =
|
||||
std::make_shared<sscl::PuppeteerThread>(
|
||||
PROBE_PUPPETEER_THREAD_ID,
|
||||
threadName,
|
||||
[&work, &donePromise](
|
||||
const sscl::PuppeteerThread::EntryFnArguments& args)
|
||||
{
|
||||
probePuppeteerMain(args, work, donePromise);
|
||||
},
|
||||
*dummyComponent,
|
||||
nullptr);
|
||||
|
||||
dummyComponent->thread = runThread;
|
||||
lastComponentThread = runThread;
|
||||
runThread->thread.join();
|
||||
|
||||
std::exception_ptr probeException = doneFuture.get();
|
||||
if (probeException) {
|
||||
std::rethrow_exception(probeException);
|
||||
}
|
||||
}
|
||||
|
||||
void runNonViralNurseryOnComponentThread(
|
||||
const std::shared_ptr<sscl::ComponentThread>& componentThread,
|
||||
std::function<sscl::co::NonViralNonPostingInvoker(
|
||||
sscl::co::NonViralTaskNursery::Slot::Lease&)> invokerFactory,
|
||||
std::chrono::milliseconds timeout)
|
||||
{
|
||||
(void)timeout;
|
||||
|
||||
sscl::co::NonViralTaskNursery nursery;
|
||||
nursery.openAdmission();
|
||||
nursery.launch(
|
||||
[&invokerFactory](sscl::co::NonViralTaskNursery::Slot::Lease& lease)
|
||||
{
|
||||
return invokerFactory(lease);
|
||||
});
|
||||
nursery.closeAdmission();
|
||||
nursery.syncAwaitAllSettlements(componentThread->getIoContext());
|
||||
}
|
||||
|
||||
} // namespace sscl::tests
|
||||
@@ -1,65 +1,19 @@
|
||||
#ifndef SPINSCALE_TEST_SUPPORT_PROBE_COMPONENT_THREAD_H
|
||||
#define SPINSCALE_TEST_SUPPORT_PROBE_COMPONENT_THREAD_H
|
||||
|
||||
#include <chrono>
|
||||
#include <exception>
|
||||
#include <functional>
|
||||
#include <future>
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
/** EXPLANATION:
|
||||
* Compatibility shim: probe harness lives in spinscale_probe_support under
|
||||
* sscl::probe. Test code may keep including this path and using sscl::tests
|
||||
* names; tools should include <probe/probeComponentThread.h> directly.
|
||||
*/
|
||||
|
||||
#include <spinscale/componentThread.h>
|
||||
#include <spinscale/co/invokers.h>
|
||||
#include <spinscale/co/nonViralTaskNursery.h>
|
||||
#include <probe/probeComponentThread.h>
|
||||
|
||||
namespace sscl::tests {
|
||||
|
||||
constexpr std::chrono::milliseconds defaultProbeTaskTimeout{10000};
|
||||
|
||||
void runNonViralNurseryOnComponentThread(
|
||||
const std::shared_ptr<sscl::ComponentThread>& componentThread,
|
||||
std::function<sscl::co::NonViralNonPostingInvoker(
|
||||
sscl::co::NonViralTaskNursery::Slot::Lease&)> invokerFactory,
|
||||
std::chrono::milliseconds timeout = defaultProbeTaskTimeout);
|
||||
|
||||
class ProbeComponentThreadHarness
|
||||
{
|
||||
public:
|
||||
explicit ProbeComponentThreadHarness(
|
||||
const char *threadName = "spinscale-probe");
|
||||
~ProbeComponentThreadHarness();
|
||||
|
||||
ProbeComponentThreadHarness(const ProbeComponentThreadHarness &) = delete;
|
||||
ProbeComponentThreadHarness &operator=(
|
||||
const ProbeComponentThreadHarness &) = delete;
|
||||
|
||||
std::shared_ptr<sscl::ComponentThread> componentThread() const;
|
||||
|
||||
void runSync(
|
||||
const std::function<void(
|
||||
const std::shared_ptr<sscl::ComponentThread>&)>& work);
|
||||
|
||||
template <typename InvokerFactory>
|
||||
void runNonViralNurseryTask(
|
||||
InvokerFactory &&invokerFactory,
|
||||
std::chrono::milliseconds timeout = defaultProbeTaskTimeout)
|
||||
{
|
||||
runSync(
|
||||
[this, &invokerFactory, timeout](
|
||||
const std::shared_ptr<sscl::ComponentThread>& componentThread)
|
||||
{
|
||||
sscl::tests::runNonViralNurseryOnComponentThread(
|
||||
componentThread,
|
||||
std::forward<InvokerFactory>(invokerFactory),
|
||||
timeout);
|
||||
});
|
||||
}
|
||||
|
||||
private:
|
||||
std::string threadName;
|
||||
std::shared_ptr<sscl::pptr::PuppeteerComponent> dummyComponent;
|
||||
std::shared_ptr<sscl::ComponentThread> lastComponentThread;
|
||||
};
|
||||
using sscl::probe::defaultProbeTaskTimeout;
|
||||
using sscl::probe::runNonViralNurseryOnComponentThread;
|
||||
using sscl::probe::ProbeComponentThreadHarness;
|
||||
|
||||
} // namespace sscl::tests
|
||||
|
||||
|
||||
Reference in New Issue
Block a user