xref: /5.5.2/moxi/src/check_work.c (revision d0366df5)
1#include <stdio.h>
2#include <stdlib.h>
3#include <math.h>
4#include <string.h>
5#include <sys/time.h>
6#include <pthread.h>
7#include <check.h>
8
9#include "memcached.h"
10#include "cproxy.h"
11#include "work.h"
12
13struct agg {
14  pthread_mutex_t lock;
15  int visits;
16};
17
18int main_check(int argc, char **argv);
19
20static void test_collect_worker(void *data0, void *data1) {
21  fail_unless(data0 != NULL, "wsc");
22  fail_unless(data1 != NULL, "wsc");
23  fail_unless(data0 == data1, "wsc");
24
25  work_collect *c = data0;
26  struct agg *agg = c->data;
27
28  fail_unless(agg != NULL, "agg");
29
30  pthread_mutex_lock(&agg->lock);
31  agg->visits++;
32  pthread_mutex_unlock(&agg->lock);
33
34  work_collect_one(c);
35}
36
37static void test_collect_main(void) {
38  int nthreads = settings.num_threads;
39
40  struct agg agg;
41
42  pthread_mutex_init(&agg.lock, NULL);
43
44  agg.visits = 0;
45
46  work_collect c;
47  work_collect_init(&c, nthreads - 1, &agg);
48
49  for (int i = 1; i < nthreads; i++) {
50    LIBEVENT_THREAD *t = thread_by_index(i);
51    fail_unless(NULL != t, "tc");
52    fail_unless(NULL != t->work_queue, "tc");
53    work_send(t->work_queue, test_collect_worker, &c, &c);
54  }
55
56  work_collect_wait(&c);
57
58  fail_unless(agg.visits == nthreads - 1, "collect");
59}
60
61START_TEST(test_collect)
62{
63  test_collect_main();
64}
65END_TEST
66
67static void test_one_worker(void *data0, void *data1) {
68  char *s0 = data0;
69  char *s1 = data1;
70
71  fail_unless(strcmp(s0, "hello") == 0, "tcw");
72  fail_unless(strcmp(s1, "world") == 0, "tcw");
73}
74
75START_TEST(test_one)
76{
77  LIBEVENT_THREAD *t = thread_by_index(1);
78  fail_unless(NULL != t, "tc");
79  fail_unless(NULL != t->work_queue, "tc");
80  work_send(t->work_queue, test_one_worker, "hello", "world");
81  sleep(1);
82}
83END_TEST
84
85static
86void setup(void)
87{
88    start_main("moxi", NULL);
89}
90
91static Suite* work_suite(void)
92{
93    Suite *s = suite_create("work");
94
95    /* Core test case */
96    TCase *tc_core = tcase_create("core");
97    tcase_add_checked_fixture(tc_core, setup, NULL);
98    tcase_add_test(tc_core, test_one);
99    tcase_add_test(tc_core, test_collect);
100    suite_add_tcase(s, tc_core);
101
102    return s;
103}
104
105/**
106 * Run this like...
107 *  check_work -vvv -p 11211
108 */
109int main(int argc, char **argv)
110{
111    int number_failed;
112    Suite *s = work_suite();
113    SRunner *sr = srunner_create(s);
114    srunner_run_all(sr, CK_ENV);
115    number_failed = srunner_ntests_failed(sr);
116    srunner_free(sr);
117
118    return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
119}
120