aboutsummaryrefslogtreecommitdiff
path: root/src/core/init_test.c
blob: 0ef13543dc6beb2ce0c3036ec481991a053c7465 (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
//
// Copyright 2024 Staysail Systems, Inc. <info@staysail.tech>
//
// 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 "nng/nng.h"
#include <nuts.h>

nng_init_params *nng_init_get_params(void);

void
test_init_param(void)
{
	nng_init_params *p;
	p = nng_init_get_params();
	NUTS_ASSERT(p != NULL);
}

void
test_init_zero_resolvers(void)
{
	nng_init_params *pp;
	nng_init_params  p     = { 0 };
	p.num_resolver_threads = 0;
	nng_fini();
	NUTS_PASS(nng_init(&p));
	pp = nng_init_get_params();
	NUTS_ASSERT(pp->num_resolver_threads > 0);
	nng_fini();
}

void
test_init_one_task_thread(void)
{
	nng_init_params *pp;
	nng_init_params  p = { 0 };

	nng_fini();
	p.max_task_threads = 1;
	NUTS_PASS(nng_init(&p));
	pp = nng_init_get_params();
	NUTS_ASSERT(pp->max_task_threads == 1);
}

void
test_init_too_many_task_threads(void)
{
	nng_socket       s;
	nng_init_params *pp;
	nng_init_params  p = { 0 };

	p.num_task_threads = 256;
	p.max_task_threads = 4;

	nng_fini();
	NUTS_PASS(nng_init(&p));
	NUTS_OPEN(s);
	NUTS_CLOSE(s);
	pp = nng_init_get_params();
	NUTS_TRUE(pp->num_task_threads == 4);
}

void
test_init_no_expire_thread(void)
{
	nng_socket       s;
	nng_init_params *pp;
	nng_init_params  p = { 0 };

	nng_fini();
	p.num_expire_threads = 0;
	p.max_expire_threads = 0;
	NUTS_PASS(nng_init(&p));
	NUTS_OPEN(s);
	NUTS_CLOSE(s);
	pp = nng_init_get_params();
	NUTS_TRUE(pp->num_expire_threads > 0);
	NUTS_MSG("Got %d expire threads", pp->num_expire_threads);
}

void
test_init_too_many_expire_threads(void)
{
	nng_socket       s;
	nng_init_params *pp;
	nng_init_params  p = { 0 };

	nng_fini();
	p.num_expire_threads = 256;
	p.max_expire_threads = 2;
	NUTS_PASS(nng_init(&p));
	NUTS_OPEN(s);
	NUTS_CLOSE(s);
	pp = nng_init_get_params();
	NUTS_TRUE(pp->num_expire_threads == 2);
	NUTS_MSG("Got %d expire threads", pp->num_expire_threads);
}

// poller tuning only supported on Windows right now
#ifdef NNG_PLATFORM_WINDOWS
void
test_init_poller_no_threads(void)
{
	nng_socket       s;
	nng_init_params *pp;
	nng_init_params  p = { 0 };

	nng_fini();
	p.num_poller_threads = 0;
	p.max_poller_threads = 0;
	NUTS_PASS(nng_init(&p));
	NUTS_OPEN(s);
	NUTS_CLOSE(s);
	pp = nng_init_get_params();
	NUTS_TRUE(pp->num_poller_threads > 0);
	NUTS_MSG("Got %d poller threads", pp->num_expire_threads);
}

void
test_init_too_many_poller_threads(void)
{
	nng_socket       s;
	nng_init_params *pp;
	nng_init_params  p = { 0 };

	nng_fini();
	p.num_poller_threads = 256;
	p.max_poller_threads = 2;
	NUTS_PASS(nng_init(&p));
	NUTS_OPEN(s);
	NUTS_CLOSE(s);
	pp = nng_init_get_params();
	NUTS_TRUE(pp->num_poller_threads == 2);
	NUTS_MSG("Got %d poller threads", pp->num_expire_threads);
}
#endif

NUTS_TESTS = {
	{ "init parameter", test_init_param },
	{ "init zero resolvers", test_init_zero_resolvers },
	{ "init one task thread", test_init_one_task_thread },
	{ "init too many task threads", test_init_too_many_task_threads },
	{ "init no expire thread", test_init_no_expire_thread },
	{ "init too many expire threads", test_init_too_many_expire_threads },
#ifdef NNG_PLATFORM_WINDOWS
	{ "init no poller thread", test_init_poller_no_threads },
	{ "init too many poller threads", test_init_too_many_poller_threads },
#endif

	{ NULL, NULL },
};