]> git.meshlink.io Git - meshlink/blob - test/utils.h
Add missing #include in test/utils.h.
[meshlink] / test / utils.h
1 #ifndef MESHLINK_TEST_UTILS_H
2 #define MESHLINK_TEST_UTILS_H
3
4 #include <assert.h>
5 #include <time.h>
6 #include <pthread.h>
7
8 #include "../src/meshlink.h"
9
10 // Simple synchronisation between threads
11 struct sync_flag {
12         pthread_mutex_t mutex;
13         pthread_cond_t cond;
14         bool flag;
15 };
16
17 extern void init_sync_flag(struct sync_flag *s);
18 extern void set_sync_flag(struct sync_flag *s, bool value);
19 extern void reset_sync_flag(struct sync_flag *s);
20 extern bool check_sync_flag(struct sync_flag *s);
21 extern bool wait_sync_flag(struct sync_flag *s, int seconds);
22
23 /// Create a pair of meshlink instances that are already joined together.
24 extern void open_meshlink_pair(meshlink_handle_t **a, meshlink_handle_t **b, const char *prefix);
25 extern void open_meshlink_pair_ephemeral(meshlink_handle_t **a, meshlink_handle_t **b, const char *prefix);
26
27 /// Start a pair of meshlink instances and wait for them to connect together.
28 extern void start_meshlink_pair(meshlink_handle_t *a, meshlink_handle_t *b);
29
30 /// Stop a pair of meshlink instances.
31 extern void stop_meshlink_pair(meshlink_handle_t *a, meshlink_handle_t *b);
32
33 /// Stop and cleanup a pair of meshlink instances.
34 extern void close_meshlink_pair(meshlink_handle_t *a, meshlink_handle_t *b);
35
36 /// Link two meshlink instances.
37 extern void link_meshlink_pair(meshlink_handle_t *a, meshlink_handle_t *b);
38
39 /// Default log callback
40 extern void log_cb(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text);
41
42 #define assert_after(cond, timeout)\
43         do {\
44                 for(int i = 0; i++ <= timeout;) {\
45                         if(cond)\
46                                 break;\
47                         if(i == timeout)\
48                                 assert(cond);\
49                         sleep(1);\
50                 }\
51         } while(0)
52
53 #endif
54
55 /// Compare two timespec values.
56 static inline bool timespec_lt(const struct timespec *a, const struct timespec *b) {
57         if(a->tv_sec == b->tv_sec) {
58                 return a->tv_nsec < b->tv_nsec;
59         } else {
60                 return a->tv_sec < b->tv_sec;
61         }
62 }