summaryrefslogtreecommitdiff
path: root/tests/t_workqueue.c
blob: 660d882dc5749e04d791acb3fb884e1e85ef7982 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* This software was written by Dirk Engling <erdgeist@erdgeist.org>
   It is considered beerware. Prost. Skol. Cheers or whatever. */

/* System */
#include <stdlib.h>
#include <string.h>

/* Opentracker */
#include "ot_fdstate.h"
#include "ot_workqueue.h"
#include "test.h"

/* A worker asks for a class of task and blocks until one turns up, so a task
   whose class matches no worker is never claimed and whoever is waiting for
   its answer waits forever. These cases pin the matching. */

static void case_task_goes_to_its_own_class(void) {
  ot_tasktype asked;
  ot_taskid   taskid;

  test_case("a worker is handed the task of the class it asked for");
  CHECK_INT(workqueue_pushtask(41, TASK_FULLSCRAPE_TPB_ASCII), 0);
  asked  = TASK_FULLSCRAPE;
  taskid = workqueue_poptask(&asked);
  CHECK(taskid != 0);

  test_case("and the full task type, not just its class");
  CHECK_INT(asked, TASK_FULLSCRAPE_TPB_ASCII);
  workqueue_canceltask(41);
}

static void case_flags_survive_the_queue(void) {
  ot_tasktype asked;

  test_case("flags outside the class mask are carried through");
  CHECK_INT(workqueue_pushtask(42, TASK_FULLSCRAPE_TPB_URLENCODED | TASK_FLAG_GZIP), 0);
  asked = TASK_FULLSCRAPE;
  CHECK(workqueue_poptask(&asked) != 0);
  CHECK_INT(asked, TASK_FULLSCRAPE_TPB_URLENCODED | TASK_FLAG_GZIP);
  workqueue_canceltask(42);
}

static void case_classes_do_not_cross(void) {
  ot_tasktype asked;
  ot_taskid   taskid;

  test_case("a stats worker passes over a fullscrape task");
  CHECK_INT(workqueue_pushtask(43, TASK_FULLSCRAPE_TPB_BINARY), 0);
  CHECK_INT(workqueue_pushtask(44, TASK_STATS_PEERS), 0);

  asked  = TASK_STATS;
  taskid = workqueue_poptask(&asked);
  CHECK(taskid != 0);
  CHECK_INT(asked, TASK_STATS_PEERS);

  test_case("and leaves it for the one that wants it");
  asked  = TASK_FULLSCRAPE;
  taskid = workqueue_poptask(&asked);
  CHECK(taskid != 0);
  CHECK_INT(asked, TASK_FULLSCRAPE_TPB_BINARY);

  workqueue_canceltask(43);
  workqueue_canceltask(44);
}

static void case_every_task_gets_its_own_id(void) {
  ot_tasktype asked;
  ot_taskid   first, second;

  test_case("two queued tasks are handed out under different ids");
  CHECK_INT(workqueue_pushtask(45, TASK_FULLSCRAPE), 0);
  CHECK_INT(workqueue_pushtask(46, TASK_FULLSCRAPE), 0);

  asked  = TASK_FULLSCRAPE;
  first  = workqueue_poptask(&asked);
  asked  = TASK_FULLSCRAPE;
  second = workqueue_poptask(&asked);
  CHECK(first != 0);
  CHECK(second != 0);
  CHECK(first != second);

  workqueue_canceltask(45);
  workqueue_canceltask(46);
}

static void case_cancelled_task_is_not_handed_out(void) {
  ot_tasktype asked;

  test_case("a cancelled task is gone before any worker sees it");
  CHECK_INT(workqueue_pushtask(47, TASK_FULLSCRAPE_TPB_ASCII_PLUS), 0);
  CHECK_INT(workqueue_pushtask(48, TASK_FULLSCRAPE), 0);
  workqueue_canceltask(47);

  asked = TASK_FULLSCRAPE;
  CHECK(workqueue_poptask(&asked) != 0);
  CHECK_INT(asked, TASK_FULLSCRAPE);
  workqueue_canceltask(48);
}

/* Tasks are filed under a descriptor number, and the number is handed to the
   next connection the moment this one closes. */

static void case_cancel_takes_every_task_of_the_socket(void) {
  ot_tasktype asked;

  test_case("cancelling a socket leaves none of its tasks behind");
  CHECK_INT(workqueue_pushtask(49, TASK_FULLSCRAPE_TPB_ASCII), 0);
  CHECK_INT(workqueue_pushtask(49, TASK_FULLSCRAPE_TPB_BINARY), 0);
  CHECK_INT(workqueue_pushtask(50, TASK_FULLSCRAPE), 0);
  workqueue_canceltask(49);

  asked = TASK_FULLSCRAPE;
  CHECK(workqueue_poptask(&asked) != 0);
  CHECK_INT(asked, TASK_FULLSCRAPE);
  workqueue_canceltask(50);
}

static void case_result_names_the_connection_that_asked(void) {
  ot_tasktype   asked = TASK_STATS;
  ot_taskid     taskid;
  struct iovec *iovector;
  size_t        iovec_entries;
  int           is_partial;
  uint32_t      asked_under, answered_under = ~0u;

  CHECK_INT(fdstate_init(), 0);
  asked_under = fdstate_open(51, NULL, 0);

  test_case("a result comes back with the generation its task was pushed under");
  CHECK_INT(workqueue_pushtask(51, TASK_STATS_PEERS), 0);
  taskid = workqueue_poptask(&asked);
  CHECK(taskid != 0);

  /* The connection goes away without cancelling and the number is reused */
  fdstate_close(51);
  fdstate_open(51, NULL, 0);

  CHECK_INT(workqueue_pushresult(taskid, 0, NULL), 0);
  CHECK_INT(workqueue_popresult(&iovec_entries, &iovector, &is_partial, &answered_under), 51);
  CHECK_INT(answered_under, asked_under);

  test_case("which is no longer the one the descriptor carries");
  CHECK(fdstate_generation(51) != answered_under);

  fdstate_close(51);
  fdstate_deinit();
}

void test_workqueue(void) {
  case_task_goes_to_its_own_class();
  case_flags_survive_the_queue();
  case_classes_do_not_cross();
  case_every_task_gets_its_own_id();
  case_cancelled_task_is_not_handed_out();
  case_cancel_takes_every_task_of_the_socket();
  case_result_names_the_connection_that_asked();
}