Rules are ordered and first-match-wins: model glob, path glob, required context, hook/retry overrides, pass <endpoint>[,<endpoint>] [as <model>] or block. Endpoints carry shell pre-hooks (exit != 0 = not yet, retried with backoff after an optional wake command), an offline window after a failed connect, and two timeouts (idle + total) so a thinking model logs still_working instead of looking hung. tests/run.sh: 14 checks against C stubs (failing, gated, slow, dead endpoints).
98 lines
3.3 KiB
C
98 lines
3.3 KiB
C
/* tests/stub.c — a stub OpenAI-compatible endpoint for the test suite.
|
|
*
|
|
* stub <port> <name> [--fail] [--delay SECONDS]
|
|
*
|
|
* --fail always answers 503 (an endpoint that is up but will not serve)
|
|
* --delay waits that long before answering (to exercise "still_working")
|
|
*/
|
|
#define _POSIX_C_SOURCE 200809L
|
|
|
|
#include <arpa/inet.h>
|
|
#include <netinet/in.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/socket.h>
|
|
#include <time.h>
|
|
#include <unistd.h>
|
|
|
|
static void send_all(int fd, const char *buf, size_t len)
|
|
{
|
|
size_t off = 0;
|
|
while (off < len) {
|
|
ssize_t n = write(fd, buf + off, len - off);
|
|
if (n <= 0) return;
|
|
off += (size_t)n;
|
|
}
|
|
}
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
if (argc < 3) {
|
|
fprintf(stderr, "usage: stub <port> <name> [--fail] [--delay SECONDS]\n");
|
|
return 2;
|
|
}
|
|
int port = atoi(argv[1]);
|
|
const char *name = argv[2];
|
|
int fail = 0;
|
|
double delay = 0;
|
|
for (int i = 3; i < argc; i++) {
|
|
if (!strcmp(argv[i], "--fail")) fail = 1;
|
|
else if (!strcmp(argv[i], "--delay") && i + 1 < argc) delay = atof(argv[++i]);
|
|
}
|
|
|
|
int lfd = socket(AF_INET, SOCK_STREAM, 0);
|
|
int one = 1;
|
|
setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);
|
|
struct sockaddr_in addr;
|
|
memset(&addr, 0, sizeof addr);
|
|
addr.sin_family = AF_INET;
|
|
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
|
addr.sin_port = htons((unsigned short)port);
|
|
if (bind(lfd, (struct sockaddr *)&addr, sizeof addr) || listen(lfd, 16)) {
|
|
perror("bind/listen");
|
|
return 1;
|
|
}
|
|
fprintf(stderr, "stub %s on %d%s\n", name, port, fail ? " (failing)" : "");
|
|
|
|
for (;;) {
|
|
int cfd = accept(lfd, NULL, NULL);
|
|
if (cfd < 0) continue;
|
|
|
|
char req[8192];
|
|
ssize_t n = read(cfd, req, sizeof req - 1);
|
|
if (n <= 0) { close(cfd); continue; }
|
|
req[n] = '\0';
|
|
|
|
if (delay > 0) {
|
|
struct timespec ts = { (time_t)delay, (time_t)((delay - (time_t)delay) * 1e9) };
|
|
nanosleep(&ts, NULL);
|
|
}
|
|
|
|
char body[512];
|
|
if (fail) {
|
|
const char *b = "{\"error\":\"not serving\"}";
|
|
snprintf(body, sizeof body,
|
|
"HTTP/1.1 503 Service Unavailable\r\nContent-Type: application/json\r\n"
|
|
"Content-Length: %zu\r\nConnection: close\r\n\r\n%s", strlen(b), b);
|
|
} else if (!strncmp(req, "GET", 3)) {
|
|
char b[256];
|
|
snprintf(b, sizeof b, "{\"object\":\"list\",\"data\":[{\"id\":\"%s\"}]}", name);
|
|
snprintf(body, sizeof body,
|
|
"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n"
|
|
"Content-Length: %zu\r\nConnection: close\r\n\r\n%s", strlen(b), b);
|
|
} else {
|
|
char b[320];
|
|
snprintf(b, sizeof b,
|
|
"{\"id\":\"stub\",\"model\":\"%s\",\"choices\":[{\"index\":0,"
|
|
"\"message\":{\"role\":\"assistant\",\"content\":\"served by %s\"}}]}",
|
|
name, name);
|
|
snprintf(body, sizeof body,
|
|
"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n"
|
|
"Content-Length: %zu\r\nConnection: close\r\n\r\n%s", strlen(b), b);
|
|
}
|
|
send_all(cfd, body, strlen(body));
|
|
close(cfd);
|
|
}
|
|
}
|