aboutsummaryrefslogtreecommitdiff
path: root/tests/platform.c
blob: eed5adab35c041771952d3a26c600c4ec62bce3f (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
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
//
// Copyright 2016 Garrett D'Amore <garrett@damore.org>
//
// This software is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
// file was obtained (LICENSE.txt).  A copy of the license may also be
// found online at https://opensource.org/licenses/MIT.
//

#include "convey.h"
#include "nng.h"
#include "core/nng_impl.h"

#ifndef	_WIN32
#include <sys/time.h>
#endif

uint64_t
getms(void)
{
#ifdef	_WIN32
	return (GetTickCount())	;
#else
	static time_t epoch;
	struct timeval tv;

	if (epoch == 0) {
		epoch = time(NULL);
	}
	gettimeofday(&tv, NULL);

	if (tv.tv_sec < epoch) {
		// Broken clock.
		// This will force all other timing tests to fail
		return (0);
	}
	tv.tv_sec -= epoch;
	return (((uint64_t)(tv.tv_sec ) * 1000) + (tv.tv_usec / 1000));
#endif
}

// Add is for testing threads.
void
add(void *arg)
{
	*(int *)arg += 1;
}

// Notify tests for verifying condvars.
struct notifyarg {
	int did;
	int when;
	nni_mtx mx;
	nni_cv cv;
};

void
notifyafter(void *arg)
{
	struct notifyarg *na = arg;

	nni_usleep(na->when);
	nni_mtx_lock(&na->mx);
	na->did = 1;
	nni_cv_wake(&na->cv);
	nni_mtx_unlock(&na->mx);
}

TestMain("Platform Operations", {

	int rv = nni_init();	// This is required for anything else to work
	Convey("Platform init worked", {
		So(rv == 0);
	})
	Convey("The clock works", {
		uint64_t now = getms();

		Convey("usleep works", {
			nni_usleep(100000);

			So((getms() - now) >= 100);	// cannot be *shorter*!!
			So((getms() - now) < 150);	// crummy clock resolution?
		})
		Convey("times work", {
			uint64_t msend;
			int usdelta;
			int msdelta;
			nni_time usend;
			nni_time usnow = nni_clock();
			nni_usleep(200000);
			usend = nni_clock();
			msend = getms();

			So(usend > usnow);
			So(msend > now);
			usdelta = (int)((usend - usnow) / 1000);
			msdelta = (int)((msend - now));
			So(usdelta >= 200);
			So(usdelta < 220);
			So(abs(msdelta - usdelta) < 20);
		})
	})
	Convey("Mutexes work", {
		nni_mtx mx;
		int rv;

		rv = nni_mtx_init(&mx);
		So(rv == 0);

		Convey("We can lock a mutex", {
			nni_mtx_lock(&mx);
			So(1);
			Convey("And cannot recursively lock", {
				rv = nni_mtx_trylock(&mx);
				So(rv != 0);
			})
			Convey("And we can unlock it", {
				nni_mtx_unlock(&mx);
				So(1);
				Convey("And then lock it again", {
					rv = nni_mtx_trylock(&mx);
					So(rv == 0);
				})
			})
		})
		Convey("We can finalize it", {
			nni_mtx_fini(&mx);
		})
	})

	Convey("Threads work", {
		nni_thr thr;
		int val = 0;
		int rv;

		Convey("We can create threads", {
			rv = nni_thr_init(&thr, add, &val);
			So(rv == 0);
			nni_thr_run(&thr);

			Convey("It ran", {
				nni_usleep(50000);	// for context switch
				So(val == 1);
			})
			Convey("We can reap it", {
				nni_thr_fini(&thr);
			})
		})
	})
	Convey("Condition variables work", {
		struct notifyarg arg;
		nni_thr thr;

		So(nni_mtx_init(&arg.mx) == 0);
		So(nni_cv_init(&arg.cv, &arg.mx) == 0);
		So(nni_thr_init(&thr, notifyafter, &arg) == 0);

		Reset({
			nni_thr_fini(&thr);
			nni_cv_fini(&arg.cv);
			nni_mtx_fini(&arg.mx);
		});

		Convey("Notification works", {
			arg.did = 0;
			arg.when = 10000;
			nni_thr_run(&thr);

			nni_mtx_lock(&arg.mx);
			if (!arg.did) {
				nni_cv_wait(&arg.cv);
			}
			nni_mtx_unlock(&arg.mx);
			So(arg.did == 1);
		})

		Convey("Timeout works", {
			arg.did = 0;
			arg.when = 200000;
			nni_thr_run(&thr);
			nni_mtx_lock(&arg.mx);
			if (!arg.did) {
				nni_cv_until(&arg.cv, nni_clock() + 10000);
			}
			So(arg.did == 0);
			nni_mtx_unlock(&arg.mx);
		})

		Convey("Not running works", {
			arg.did = 0;
			arg.when = 1;
			nni_mtx_lock(&arg.mx);
			if (!arg.did) {
				nni_cv_until(&arg.cv, nni_clock() + 10000);
			}
			So(arg.did == 0);
			nni_mtx_unlock(&arg.mx);
		})
	})
})