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