|
- /* Real preparation workers and native dispatch code, mocked I/O only. */
- #include "guard.h"
- #include <sys/types.h>
- #include <time.h>
- #include <errno.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- static unsigned checks;
- #define CHECK(x) do { if (!(x)) { fprintf(stderr, "FAIL validation %d: %s\n", __LINE__, #x); exit(1); } checks++; } while (0)
- static volatile int disposed, gate;
- static int injected_errno, inherited_errno, calls, poison_clock;
- static long injected_rc;
- double monotime(void)
- {
- struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t);
- if (__sync_fetch_and_add(&poison_clock, 0)) errno = ENOMEM;
- return t.tv_sec + t.tv_nsec / 1e9;
- }
- static void pause_ms(unsigned ms)
- {
- struct timespec t = {ms / 1000, (long)(ms % 1000) * 1000000};
- while (nanosleep(&t, &t) && errno == EINTR) { }
- }
- int platform_validation_init(unsigned slots) { return slots == 16 ? 0 : -1; }
- int platform_prepare(unsigned slot, const config_t *c, const process_t *p,
- int real, uint64_t ceiling, int *handle, validation_t *t)
- {
- double start = monotime();
- (void)slot; (void)c; (void)real; (void)ceiling;
- if (p->pid == 1) pause_ms(1283);
- if (p->pid >= 100) while (__sync_fetch_and_add(&gate, 0)) pause_ms(1);
- *handle = p->pid;
- t->read_ms = 1000 * (monotime() - start);
- errno = EAGAIN; return 0;
- }
- void platform_dispose(int handle)
- { if (handle >= 0) __sync_fetch_and_add(&disposed, 1); }
- void validation_late_result(int pid, const validation_t *t, int error)
- { (void)pid; (void)t; (void)error; }
- static ssize_t injected_write(int fd, const void *buf, size_t n)
- {
- (void)fd; (void)buf; (void)n;
- calls++; errno = injected_errno; return injected_rc;
- }
- #define write injected_write
- #include "../src/signal.c"
- #undef write
- #include "../src/validation.c"
-
- int main(void)
- {
- config_t c; process_t p; validation_t t; signal_result_t r;
- int ticket, i, j;
- double start;
- const int previous[] = {EAGAIN, ENOMEM};
- const int errors[] = {0, ESRCH, EPERM, EINVAL, EAGAIN};
- memset(&c, 0, sizeof (c)); memset(&p, 0, sizeof (p));
- CHECK(validation_start() == 0);
- p.pid = 1; start = monotime();
- CHECK(validation_acquire(&c, &p, 1, 500, 50, &ticket, &t) == -1);
- CHECK(errno == ETIMEDOUT && ticket == -1);
- CHECK(monotime() - start < 0.150);
- CHECK(validation_acquire(&c, &p, 1, 500, 50, &ticket, &t) == -1 && errno == EBUSY);
- p.pid = 2;
- CHECK(validation_acquire(&c, &p, 1, 500, 50, &ticket, &t) == 0);
- CHECK(validation_handle(ticket) == 2 && monotime() - start < 0.200);
- validation_release(ticket);
- CHECK(calls == 0); /* A validator, including a late one, cannot signal. */
- pause_ms(1350);
- CHECK(__sync_fetch_and_add(&disposed, 0) == 2 && calls == 0);
- __sync_lock_test_and_set(&poison_clock, 1);
- for (i = 0; i < 2; i++) for (j = 0; j < 5; j++) {
- inherited_errno = previous[i]; injected_errno = errors[j];
- injected_rc = injected_errno ? -1 : (long)(2 * sizeof (long));
- errno = inherited_errno;
- platform_dispatch(10, KILL, &r);
- CHECK(r.attempted && r.rc == injected_rc && r.error == injected_errno);
- CHECK(r.returned >= r.attempt && r.attempt > 0);
- }
- injected_rc = 1; injected_errno = EAGAIN;
- platform_dispatch(10, KILL, &r);
- CHECK(r.rc == 1 && r.error == 0); /* short write is not an errno */
- injected_rc = (long)(2 * sizeof (long)); injected_errno = EAGAIN;
- platform_dispatch(10, TERM, &r);
- CHECK(r.error == 0 && r.rc == injected_rc);
- __sync_lock_test_and_set(&poison_clock, 0);
- __sync_lock_test_and_set(&gate, 1);
- for (i = 0; i < 16; i++) {
- p.pid = 100 + i;
- CHECK(validation_acquire(&c, &p, 1, 500, 10, &ticket, &t) == -1);
- CHECK(errno == ETIMEDOUT);
- }
- p.pid = 200;
- start = monotime();
- CHECK(validation_acquire(&c, &p, 1, 500, 50, &ticket, &t) == -1);
- CHECK(errno == EBUSY && monotime() - start < 0.050);
- __sync_lock_test_and_set(&gate, 0); pause_ms(50);
- p.pid = 2;
- CHECK(validation_acquire(&c, &p, 1, 500, 50, &ticket, &t) == 0);
- validation_release(ticket);
- printf("PASS %u validation checks: real 1283ms worker abandoned, fallback, late disposal, pool saturation; actual dispatch code with mocked write; no signals\n", checks);
- return 0;
- }
|