Skip to content

Commit

Permalink
Add a test for thread pool restart behavior.
Browse files Browse the repository at this point in the history
  • Loading branch information
aliddell committed Dec 11, 2023
1 parent ffaf098 commit 6d61177
Showing 1 changed file with 125 additions and 42 deletions.
167 changes: 125 additions & 42 deletions tests/restart-stopped-zarr-resets-threadpool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,19 @@
/// @brief Test that restarting a previously stopped Zarr writer resets the
/// thread pool.

#include "device/hal/device.manager.h"
#include "acquire.h"
#include "platform.h" // clock
#include "platform.h" // lib
#include "logger.h"
#include "device/kit/driver.h"
#include "device/hal/driver.h"
#include "device/hal/storage.h"
#include "device/props/storage.h"

#include <filesystem>
#include <fstream>
#include <cstdio>
#include <string>
#include <stdexcept>
#include <vector>

#include "json.hpp"

namespace fs = std::filesystem;
using json = nlohmann::json;

void
reporter(int is_error,
const char* file,
int line,
const char* function,
const char* msg)
{
fprintf(is_error ? stderr : stdout,
"%s%s(%d) - %s: %s\n",
is_error ? "ERROR " : "",
file,
line,
function,
msg);
}
#define containerof(P, T, F) ((T*)(((char*)(P)) - offsetof(T, F)))

/// Helper for passing size static strings as function args.
/// For a function: `f(char*,size_t)` use `f(SIZED("hello"))`.
Expand All @@ -53,26 +37,125 @@ reporter(int is_error,
#define DEVOK(e) CHECK(Device_Ok == (e))
#define OK(e) CHECK(AcquireStatus_Ok == (e))

/// example: `ASSERT_EQ(int,"%d",42,meaning_of_life())`
#define ASSERT_EQ(T, fmt, a, b) \
do { \
T a_ = (T)(a); \
T b_ = (T)(b); \
EXPECT(a_ == b_, "Expected %s==%s but " fmt "!=" fmt, #a, #b, a_, b_); \
} while (0)
void
reporter(int is_error,
const char* file,
int line,
const char* function,
const char* msg)
{
fprintf(is_error ? stderr : stdout,
"%s%s(%d) - %s: %s\n",
is_error ? "ERROR " : "",
file,
line,
function,
msg);
}

/// Check that a>b
/// example: `ASSERT_GT(int,"%d",43,meaning_of_life())`
#define ASSERT_GT(T, fmt, a, b) \
do { \
T a_ = (T)(a); \
T b_ = (T)(b); \
EXPECT( \
a_ > b_, "Expected (%s) > (%s) but " fmt "<=" fmt, #a, #b, a_, b_); \
} while (0)
typedef struct Driver* (*init_func_t)(void (*reporter)(int is_error,
const char* file,
int line,
const char* function,
const char* msg));

struct Storage*
get_zarr(lib* lib)
{

CHECK(lib_open_by_name(lib, "acquire-driver-zarr"));

auto init = (init_func_t)lib_load(lib, "acquire_driver_init_v0");
auto driver = init(reporter);
CHECK(driver);

struct Storage* zarr = nullptr;
for (uint32_t i = 0; i < driver->device_count(driver); ++i) {
DeviceIdentifier id;
DEVOK(driver->describe(driver, &id, i));
std::string dev_name{ id.name };

if (id.kind == DeviceKind_Storage && dev_name == "Zarr") {
struct Device* device = nullptr;

DEVOK(driver_open_device(driver, i, &device));
zarr = containerof(device, struct Storage, device);
break;
}
}

return zarr;
}

void
configure(struct Storage* zarr)
{
struct StorageProperties props = { 0 };
storage_properties_init(&props, 0, SIZED(TEST ".zarr"), nullptr, 0, { 0 });

CHECK(DeviceState_Armed == zarr->set(zarr, &props));
}

void
acquire_one_frame(struct Storage* zarr)
{
CHECK(DeviceState_Running == zarr->start(zarr));
struct ImageShape shape = {
.dims = {
.channels = 1,
.width = 64,
.height = 48,
.planes = 1,
},
.strides = {
.channels = 1,
.width = 1,
.height = 64,
.planes = 64 * 48
},
.type = SampleType_u8,
};
zarr->reserve_image_shape(zarr, &shape);

auto* frame = (struct VideoFrame*)malloc(sizeof(VideoFrame) + 64 * 48);
frame->bytes_of_frame = sizeof(*frame) + 64 * 48;

frame->shape = shape;
frame->frame_id = 0;
frame->hardware_frame_id = 0;
frame->timestamps = { 0, 0 };

size_t nbytes{ frame->bytes_of_frame };
zarr->append(zarr, frame, &nbytes);

free(frame);

CHECK(DeviceState_Armed == zarr->stop(zarr));
}

int
main()
{
EXPECT(false, "Implement this test.");
logger_set_reporter(reporter);
lib lib{};

try {
struct Storage* zarr = get_zarr(&lib);
CHECK(zarr);

configure(zarr);

acquire_one_frame(zarr);
acquire_one_frame(zarr); // thread pool should reset here

lib_close(&lib);
return 0;
} catch (std::exception& e) {
ERR("%s", e.what());
} catch (...) {
ERR("Unknown exception");
}

lib_close(&lib);
return 1;
}

0 comments on commit 6d61177

Please sign in to comment.