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