forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtestmod_zeromqsend.c
322 lines (258 loc) · 8.23 KB
/
testmod_zeromqsend.c
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/**
* @file
*
* @brief
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/
#include "zeromqsend.h"
#include <stdio.h> // printf() & co
#include <time.h> // time()
#include <unistd.h> // usleep()
#include <kdberrors.h> // TIMEOUT ERROR
#include <kdbioplugin.h> // ElektraIoPluginSetBinding
#include <tests.h>
#include <tests_plugin.h>
#include <pthread.h>
/** change type received by readNotificationFromTestSocket() */
char * receivedChangeType;
/** key name received by readNotificationFromTestSocket() */
char * receivedKeyName;
/** variable indicating that a timeout occurred while receiving */
int receiveTimeout;
/** zmq context for tests */
void * context;
/** time in microseconds before a new socket is created. leaves the system some after binding a socket again */
#define TIME_HOLDOFF (1000 * 1000)
/** timeout for tests in seconds */
#define TEST_TIMEOUT 20
/** endpoint for tests */
#define TEST_ENDPOINT "tcp://127.0.0.1:6002"
/** extended timeouts for tests */
#define TESTCONFIG_CONNECT_TIMEOUT "5000"
#define TESTCONFIG_SUBSCRIBE_TIMEOUT "5000"
/**
* Create subscriber socket for tests.
* @internal
*
* @param subscribeFilter filter for subscriptions
* @return new socket
*/
static void * createTestSocket (char * subscribeFilter)
{
// leave the system some time before binding again
usleep (TIME_HOLDOFF);
void * subSocket = zmq_socket (context, ZMQ_SUB);
int result = zmq_bind (subSocket, TEST_ENDPOINT);
if (result != 0)
{
yield_error ("zmq_bind failed");
printf ("zmq error was: %s\n", zmq_strerror (zmq_errno ()));
exit (-1);
}
if (subscribeFilter != NULL)
{
exit_if_fail (zmq_setsockopt (subSocket, ZMQ_SUBSCRIBE, subscribeFilter, elektraStrLen (subscribeFilter)) == 0,
"subscribe failed"); // subscribe to all messages
}
return subSocket;
}
/**
* Main function for notification reader thread.
*
* Sets global variables receivedKeyName and receivedChangeType.
*
* @internal
*
* @param subSocket socket to read messages from
* @return always NULL
*/
static void * notificationReaderThreadMain (void * filter)
{
void * subSocket = createTestSocket ((char *) filter);
time_t start = time (NULL);
zmq_msg_t message;
zmq_msg_init (&message);
int more;
size_t moreSize = sizeof (more);
int rc;
int partCounter = 0;
int maxParts = 2; // change type and key name
int lastErrno;
do
{
usleep (100 * 1000); // wait 100 ms
lastErrno = 0;
int result = zmq_msg_recv (&message, subSocket, ZMQ_DONTWAIT);
// check for timeout
if (time (NULL) - start > TEST_TIMEOUT)
{
receiveTimeout = 1;
receivedChangeType = NULL;
receivedKeyName = NULL;
zmq_msg_close (&message);
zmq_close (subSocket);
return NULL;
}
// check for errors
if (result == -1)
{
lastErrno = zmq_errno ();
if (lastErrno != EAGAIN)
{
yield_error ("zmq_msg_recv failed");
printf ("zmq_msg_recv failed: %s\n", zmq_strerror (lastErrno));
zmq_msg_close (&message);
zmq_close (subSocket);
return NULL;
}
}
else
{
rc = zmq_getsockopt (subSocket, ZMQ_RCVMORE, &more, &moreSize);
if (rc < 0)
{
yield_error ("zmq_getsockopt failed");
printf ("zmq_getsockopt failed: %s\n", zmq_strerror (zmq_errno ()));
zmq_msg_close (&message);
zmq_close (subSocket);
return NULL;
}
int length = zmq_msg_size (&message);
char * buffer = elektraStrNDup (zmq_msg_data (&message), length + 1);
buffer[length] = '\0';
switch (partCounter)
{
case 0:
receivedChangeType = buffer;
break;
case 1:
receivedKeyName = buffer;
break;
default:
yield_error ("test inconsistency");
}
partCounter++;
}
} while (lastErrno == EAGAIN || (more && partCounter < maxParts));
zmq_msg_close (&message);
zmq_close (subSocket);
return NULL;
}
/**
* Create and start thread for reading notifications.
* @internal
*
* @param filter subscription filter
* @return new thread
*/
static pthread_t * startNotificationReaderThread (char * filter)
{
pthread_t * thread = elektraMalloc (sizeof *thread);
pthread_create (thread, NULL, notificationReaderThreadMain, filter);
return thread;
}
static void test_commit (void)
{
printf ("test commit notification\n");
Key * parentKey = keyNew ("system/tests/foo", KEY_END);
Key * toAdd = keyNew ("system/tests/foo/bar", KEY_END);
KeySet * ks = ksNew (0, KS_END);
KeySet * conf = ksNew (3, keyNew ("/endpoint", KEY_VALUE, TEST_ENDPOINT, KEY_END),
keyNew ("/connectTimeout", KEY_VALUE, TESTCONFIG_CONNECT_TIMEOUT, KEY_END),
keyNew ("/subscribeTimeout", KEY_VALUE, TESTCONFIG_SUBSCRIBE_TIMEOUT, KEY_END), KS_END);
PLUGIN_OPEN ("zeromqsend");
// initial get to save current state
plugin->kdbGet (plugin, ks, parentKey);
// add key to keyset
ksAppendKey (ks, toAdd);
receiveTimeout = 0;
receivedKeyName = NULL;
receivedChangeType = NULL;
pthread_t * thread = startNotificationReaderThread ("Commit");
plugin->kdbSet (plugin, ks, parentKey);
pthread_join (*thread, NULL);
succeed_if (receiveTimeout == 0, "receiving did time out");
succeed_if (!keyGetMeta (parentKey, "warnings"), "warning meta key was set");
succeed_if_same_string ("Commit", receivedChangeType);
succeed_if_same_string (keyName (parentKey), receivedKeyName);
ksDel (ks);
keyDel (parentKey);
PLUGIN_CLOSE ();
elektraFree (receivedKeyName);
elektraFree (receivedChangeType);
elektraFree (thread);
}
static void test_timeoutConnect (void)
{
printf ("test connect timeout\n");
Key * parentKey = keyNew ("system/tests/foo", KEY_END);
Key * toAdd = keyNew ("system/tests/foo/bar", KEY_END);
KeySet * ks = ksNew (0, KS_END);
KeySet * conf = ksNew (3, keyNew ("/endpoint", KEY_VALUE, TEST_ENDPOINT, KEY_END),
keyNew ("/connectTimeout", KEY_VALUE, TESTCONFIG_CONNECT_TIMEOUT, KEY_END),
keyNew ("/subscribeTimeout", KEY_VALUE, TESTCONFIG_SUBSCRIBE_TIMEOUT, KEY_END), KS_END);
PLUGIN_OPEN ("zeromqsend");
// initial get to save current state
plugin->kdbGet (plugin, ks, parentKey);
// add key to keyset
ksAppendKey (ks, toAdd);
plugin->kdbSet (plugin, ks, parentKey);
char * expectedWarningNumber = elektraFormat ("%s", ELEKTRA_ERROR_INSTALLATION);
succeed_if (keyGetMeta (parentKey, "warnings"), "warning meta key was not set");
succeed_if_same_string (expectedWarningNumber, keyValue (keyGetMeta (parentKey, "warnings/#00/number")));
ksDel (ks);
keyDel (parentKey);
PLUGIN_CLOSE ();
elektraFree (expectedWarningNumber);
}
static void test_timeoutSubscribe (void)
{
printf ("test subscribe message timeout\n");
Key * parentKey = keyNew ("system/tests/foo", KEY_END);
Key * toAdd = keyNew ("system/tests/foo/bar", KEY_END);
KeySet * ks = ksNew (0, KS_END);
KeySet * conf = ksNew (3, keyNew ("/endpoint", KEY_VALUE, TEST_ENDPOINT, KEY_END),
keyNew ("/connectTimeout", KEY_VALUE, TESTCONFIG_CONNECT_TIMEOUT, KEY_END),
keyNew ("/subscribeTimeout", KEY_VALUE, TESTCONFIG_SUBSCRIBE_TIMEOUT, KEY_END), KS_END);
PLUGIN_OPEN ("zeromqsend");
// initial get to save current state
plugin->kdbGet (plugin, ks, parentKey);
// add key to keyset
ksAppendKey (ks, toAdd);
receiveTimeout = 0;
receivedKeyName = NULL;
receivedChangeType = NULL;
// do not subscribe to Commit messages, this makes the plugin timeout due to no subscribers
pthread_t * thread = startNotificationReaderThread (NULL);
plugin->kdbSet (plugin, ks, parentKey);
// without timeout we won't return here
pthread_join (*thread, NULL);
succeed_if (receiveTimeout, "receiving did not time out");
succeed_if (receivedKeyName == NULL, "received key name should be unchanged");
succeed_if (receivedChangeType == NULL, "received change type should be unchanged");
ksDel (ks);
keyDel (parentKey);
PLUGIN_CLOSE ();
elektraFree (receivedKeyName);
elektraFree (receivedChangeType);
elektraFree (thread);
}
int main (int argc, char ** argv)
{
printf ("ZEROMQSEND TESTS\n");
printf ("================\n\n");
init (argc, argv);
int major, minor, patch;
zmq_version (&major, &minor, &patch);
printf ("zeromq version is %d.%d.%d\n", major, minor, patch);
context = zmq_ctx_new ();
// Test notification from plugin
test_commit ();
// test timeouts
test_timeoutConnect ();
test_timeoutSubscribe ();
print_result ("testmod_zeromqsend");
zmq_ctx_destroy (context);
return nbError;
}