summaryrefslogtreecommitdiffstats
path: root/debian/vendor-h2o/deps/libyrmcds/t/t.h
blob: fd79d59cd4f4b3c96f51624e33d29590eed3d646 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// test utilities

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <yrmcds.h>

static int n_failures = 0;

static void check_error(yrmcds_error e) {
    if( e == YRMCDS_OK )
        return;

    fprintf(stderr, "yrmcds error: %s\n", yrmcds_strerror(e));
    exit(1);
}

static yrmcds* get_yrmcds(yrmcds* c) {
    const char* host = getenv("YRMCDS_HOST");
    if( host == NULL ) {
        return NULL;
    }
    uint16_t port = 11211;
    if( getenv("YRMCDS_PORT") ) {
        port = (uint16_t)atoi(getenv("YRMCDS_PORT"));
    }

    check_error( yrmcds_connect(c, host, port) );
    return c;
}

static void test_main(yrmcds* c);

#define TEST_MAIN() static void test_main(yrmcds* c)

int main(int argc, char** argv) {
    yrmcds c_;
    yrmcds* c = get_yrmcds(&c_);
    if( c == NULL ) {
        fprintf(stderr, "No YRMCDS_HOST.  Skipped.\n");
        return 0;
    }

    test_main(c);
    yrmcds_close(c);

    if( n_failures > 0 ) {
        fprintf(stderr, "%d tests failed.\n", n_failures);
        return 1;
    }
    fprintf(stderr, "Passed.\n");
    return 0;
}

#define DEF_TEST(name) void test_##name(yrmcds* c)

#define CALL_TEST(name)                                   \
    fprintf(stderr, "[%s]\n", #name);                     \
    test_##name(c);                                       \
    uint32_t serial_##name;                               \
    check_error( yrmcds_flush(c, 0, 0, &serial_##name) ); \
    while( 1 ) {                                          \
        yrmcds_response r;                                \
        check_error( yrmcds_recv(c, &r) );                \
        if( r.serial == serial_##name ) break;            \
    }                                                     \
    sleep(1)

#define ASSERT(expr, to_return)                                 \
    if( ! (expr) ) {                                            \
        fprintf(stderr, "assertion failure at line %d: %s\n",   \
                __LINE__, #expr);                               \
        n_failures++;                                           \
        if( to_return ) return;                                 \
    }