zhangmeng
2024-04-22 16935f4aebffdd1b6580b844391a0aa0f4f3012b
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
//
// Copyright 2020 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
//
// 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 <nuts.h>
 
static void
test_surv_identity(void)
{
    nng_socket s;
    int        p;
    char *     n;
 
    NUTS_PASS(nng_surveyor0_open(&s));
    NUTS_PASS(nng_socket_get_int(s, NNG_OPT_PROTO, &p));
    NUTS_TRUE(p == NNG_SURVEYOR0_SELF);
    NUTS_PASS(nng_socket_get_int(s, NNG_OPT_PEER, &p));
    NUTS_TRUE(p == NNG_SURVEYOR0_PEER); // 49
    NUTS_PASS(nng_socket_get_string(s, NNG_OPT_PROTONAME, &n));
    NUTS_MATCH(n, NNG_SURVEYOR0_SELF_NAME);
    nng_strfree(n);
    NUTS_PASS(nng_socket_get_string(s, NNG_OPT_PEERNAME, &n));
    NUTS_MATCH(n, NNG_SURVEYOR0_PEER_NAME);
    nng_strfree(n);
    NUTS_CLOSE(s);
}
 
static void
test_surv_ttl_option(void)
{
    nng_socket  surv;
    int         v;
    bool        b;
    size_t      sz;
    const char *opt = NNG_OPT_MAXTTL;
 
    NUTS_PASS(nng_surveyor0_open(&surv));
 
    NUTS_PASS(nng_socket_set_int(surv, opt, 1));
    NUTS_FAIL(nng_socket_set_int(surv, opt, 0), NNG_EINVAL);
    NUTS_FAIL(nng_socket_set_int(surv, opt, -1), NNG_EINVAL);
    // This test will fail if the NNI_MAX_MAX_TTL is changed from the
    // builtin default of 15.
    NUTS_FAIL(nng_socket_set_int(surv, opt, 16), NNG_EINVAL);
    NUTS_FAIL(nng_socket_set_int(surv, opt, 256), NNG_EINVAL);
    NUTS_PASS(nng_socket_set_int(surv, opt, 3));
    NUTS_PASS(nng_socket_get_int(surv, opt, &v));
    NUTS_TRUE(v == 3);
    v  = 0;
    sz = sizeof(v);
    NUTS_PASS(nng_socket_get(surv, opt, &v, &sz));
    NUTS_TRUE(v == 3);
    NUTS_TRUE(sz == sizeof(v));
 
    NUTS_FAIL(nng_socket_set(surv, opt, "", 1), NNG_EINVAL);
    sz = 1;
    NUTS_FAIL(nng_socket_get(surv, opt, &v, &sz), NNG_EINVAL);
    NUTS_FAIL(nng_socket_set_bool(surv, opt, true), NNG_EBADTYPE);
    NUTS_FAIL(nng_socket_get_bool(surv, opt, &b), NNG_EBADTYPE);
 
    NUTS_CLOSE(surv);
}
 
static void
test_surv_survey_time_option(void)
{
    nng_socket   surv;
    nng_duration d;
    bool         b;
    size_t       sz  = sizeof(b);
    const char * opt = NNG_OPT_SURVEYOR_SURVEYTIME;
 
    NUTS_PASS(nng_surveyor0_open(&surv));
 
    NUTS_PASS(nng_socket_set_ms(surv, opt, 10));
    NUTS_FAIL(nng_socket_set(surv, opt, "", 1), NNG_EINVAL);
    NUTS_FAIL(nng_socket_get(surv, opt, &b, &sz), NNG_EINVAL);
    NUTS_FAIL(nng_socket_set_bool(surv, opt, true), NNG_EBADTYPE);
    NUTS_FAIL(nng_socket_get_bool(surv, opt, &b), NNG_EBADTYPE);
 
    NUTS_PASS(nng_socket_get_ms(surv, opt, &d));
    NUTS_TRUE(d == 10);
    NUTS_CLOSE(surv);
}
 
void
test_surv_recv_bad_state(void)
{
    nng_socket surv;
    nng_msg *  msg = NULL;
 
    NUTS_PASS(nng_surveyor0_open(&surv));
    NUTS_FAIL(nng_recvmsg(surv, &msg, 0), NNG_ESTATE);
    NUTS_TRUE(msg == NULL);
    NUTS_CLOSE(surv);
}
 
static void
test_surv_recv_garbage(void)
{
    nng_socket resp;
    nng_socket surv;
    nng_msg *  m;
    uint32_t   surv_id;
 
    NUTS_PASS(nng_respondent0_open_raw(&resp));
    NUTS_PASS(nng_surveyor0_open(&surv));
    NUTS_PASS(nng_socket_set_ms(surv, NNG_OPT_RECVTIMEO, 100));
    NUTS_PASS(nng_socket_set_ms(surv, NNG_OPT_SENDTIMEO, 1000));
    NUTS_PASS(nng_socket_set_ms(resp, NNG_OPT_SENDTIMEO, 1000));
    NUTS_PASS(nng_socket_set_ms(resp, NNG_OPT_SENDTIMEO, 1000));
 
    NUTS_MARRY(surv, resp);
 
    NUTS_PASS(nng_msg_alloc(&m, 0));
    NUTS_PASS(nng_sendmsg(surv, m, 0));
 
    NUTS_PASS(nng_recvmsg(resp, &m, 0));
 
    // The message will have a header that contains the 32-bit pipe ID,
    // followed by the 32-bit request ID.  We will discard the request
    // ID before sending it out.
    NUTS_TRUE(nng_msg_header_len(m) == 8);
    NUTS_PASS(nng_msg_header_chop_u32(m, &surv_id));
 
    NUTS_PASS(nng_sendmsg(resp, m, 0));
    NUTS_FAIL(nng_recvmsg(surv, &m, 0), NNG_ETIMEDOUT);
 
    NUTS_CLOSE(surv);
    NUTS_CLOSE(resp);
}
 
#define SECOND 1000
 
void
test_surv_resp_exchange(void)
{
    nng_socket surv;
    nng_socket resp;
 
    NUTS_PASS(nng_surveyor0_open(&surv));
    NUTS_PASS(nng_respondent0_open(&resp));
 
    NUTS_PASS(nng_socket_set_ms(surv, NNG_OPT_RECVTIMEO, SECOND));
    NUTS_PASS(nng_socket_set_ms(resp, NNG_OPT_RECVTIMEO, SECOND));
    NUTS_PASS(nng_socket_set_ms(surv, NNG_OPT_SENDTIMEO, SECOND));
    NUTS_PASS(nng_socket_set_ms(resp, NNG_OPT_SENDTIMEO, SECOND));
 
    NUTS_MARRY(resp, surv);
 
    NUTS_SEND(surv, "ping");
    NUTS_RECV(resp, "ping");
    NUTS_SEND(resp, "pong");
    NUTS_RECV(surv, "pong");
 
    NUTS_CLOSE(surv);
    NUTS_CLOSE(resp);
}
 
void
test_surv_cancel(void)
{
    nng_socket surv;
    nng_socket resp;
 
    NUTS_PASS(nng_respondent0_open(&resp));
    NUTS_PASS(nng_surveyor0_open(&surv));
 
    NUTS_PASS(nng_socket_set_ms(surv, NNG_OPT_RECVTIMEO, SECOND));
    NUTS_PASS(nng_socket_set_ms(resp, NNG_OPT_RECVTIMEO, SECOND));
    NUTS_PASS(nng_socket_set_ms(surv, NNG_OPT_SENDTIMEO, 5 * SECOND));
    NUTS_PASS(nng_socket_set_ms(resp, NNG_OPT_SENDTIMEO, 5 * SECOND));
    NUTS_PASS(nng_socket_set_int(surv, NNG_OPT_SENDBUF, 16));
 
    NUTS_MARRY(resp, surv);
 
    // Send req #1 (abc).
    NUTS_SEND(surv, "abc");
 
    // Sleep a bit.  This is so that we ensure that our request gets
    // to the far side.  (If we cancel too fast, then our outgoing send
    // will be canceled before it gets to the peer.)
    NUTS_SLEEP(100);
 
    // Send the next next request ("def").  Note that
    // the RESP side server will have already buffered the receive
    // request, and should simply be waiting for us to reply to abc.
    NUTS_SEND(surv, "def");
 
    // Receive the first request (should be abc) on the REP server.
    NUTS_RECV(resp, "abc");
 
    // RESP sends the reply to first command.  This will be discarded
    // by the SURV socket.
    NUTS_SEND(resp, "abc");
 
    // Now get the next command from the REP; should be "def".
    NUTS_RECV(resp, "def");
 
    // And send it back to REQ.
    NUTS_SEND(resp, "def");
 
    // Try a req command.  This should give back "def"
    NUTS_RECV(surv, "def");
 
    NUTS_CLOSE(surv);
    NUTS_CLOSE(resp);
}
 
void
test_surv_cancel_abort_recv(void)
{
    nng_aio *    aio;
    nng_duration time = SECOND * 10; // 10s (kind of never)
    nng_socket   surv;
    nng_socket   resp;
 
    NUTS_PASS(nng_respondent0_open(&resp));
    NUTS_PASS(nng_surveyor0_open(&surv));
    NUTS_PASS(nng_aio_alloc(&aio, NULL, NULL));
 
    NUTS_PASS(nng_socket_set_ms(surv, NNG_OPT_SURVEYOR_SURVEYTIME, time));
    NUTS_PASS(nng_socket_set_int(surv, NNG_OPT_SENDBUF, 16));
    NUTS_PASS(nng_socket_set_ms(surv, NNG_OPT_RECVTIMEO, 5 * SECOND));
    NUTS_PASS(nng_socket_set_ms(resp, NNG_OPT_RECVTIMEO, 5 * SECOND));
    NUTS_PASS(nng_socket_set_ms(surv, NNG_OPT_SENDTIMEO, 5 * SECOND));
    NUTS_PASS(nng_socket_set_ms(resp, NNG_OPT_SENDTIMEO, 5 * SECOND));
 
    NUTS_MARRY(resp, surv);
 
    // Send survey #1 (abc).
    NUTS_SEND(surv, "abc");
 
    // Wait for it to get ot the other side.
    NUTS_SLEEP(100);
 
    nng_aio_set_timeout(aio, 5 * SECOND);
    nng_recv_aio(surv, aio);
 
    // Give time for this recv to post properly.
    NUTS_SLEEP(100);
 
    // Send the next next request ("def").  Note that
    // the respondent side server will have already buffered the receive
    // request, and should simply be waiting for us to reply to
    // abc.
    NUTS_SEND(surv, "def");
 
    // Our pending I/O should have been canceled.
    nng_aio_wait(aio);
    NUTS_FAIL(nng_aio_result(aio), NNG_ECANCELED);
 
    // Receive the first request (should be abc) on the respondent.
    NUTS_RECV(resp, "abc");
 
    // Respondent sends the reply to first survey.  This will be
    // discarded by the SURV socket.
    NUTS_SEND(resp, "abc");
 
    // Now get the next survey from the RESP; should be "def".
    NUTS_RECV(resp, "def");
 
    // And send it back to REQ.
    NUTS_SEND(resp, "def");
 
    // Try a req command.  This should give back "def"
    NUTS_RECV(surv, "def");
 
    nng_aio_free(aio);
    NUTS_CLOSE(surv);
    NUTS_CLOSE(resp);
}
 
static void
test_surv_cancel_post_recv(void)
{
    nng_socket surv;
    nng_socket resp;
 
    NUTS_PASS(nng_surveyor0_open(&surv));
    NUTS_PASS(nng_respondent0_open(&resp));
    NUTS_PASS(nng_socket_set_ms(surv, NNG_OPT_SENDTIMEO, 1000));
    NUTS_PASS(nng_socket_set_ms(resp, NNG_OPT_SENDTIMEO, 1000));
    NUTS_PASS(nng_socket_set_ms(surv, NNG_OPT_RECVTIMEO, 1000));
    NUTS_PASS(nng_socket_set_ms(resp, NNG_OPT_RECVTIMEO, 1000));
    NUTS_MARRY(surv, resp);
 
    NUTS_SEND(surv, "ONE");
    NUTS_RECV(resp, "ONE");
    NUTS_SEND(resp, "one");
    NUTS_SLEEP(100); // Make sure reply arrives!
    NUTS_SEND(surv, "TWO");
    NUTS_RECV(resp, "TWO");
    NUTS_SEND(resp, "two");
    NUTS_RECV(surv, "two");
 
    NUTS_CLOSE(surv);
    NUTS_CLOSE(resp);
}
 
static void
test_surv_poll_writeable(void)
{
    int        fd;
    nng_socket surv;
    nng_socket resp;
 
    NUTS_PASS(nng_surveyor0_open(&surv));
    NUTS_PASS(nng_respondent0_open(&resp));
    NUTS_PASS(nng_socket_get_int(surv, NNG_OPT_SENDFD, &fd));
    NUTS_TRUE(fd >= 0);
 
    // Survey is broadcast, so we can always write.
    NUTS_TRUE(nuts_poll_fd(fd));
 
    NUTS_MARRY(surv, resp);
 
    // Now it's writable.
    NUTS_TRUE(nuts_poll_fd(fd));
 
    NUTS_CLOSE(surv);
    NUTS_CLOSE(resp);
}
 
void
test_surv_poll_readable(void)
{
    int        fd;
    nng_socket surv;
    nng_socket resp;
    nng_msg *  msg;
 
    NUTS_PASS(nng_surveyor0_open(&surv));
    NUTS_PASS(nng_respondent0_open(&resp));
    NUTS_PASS(nng_socket_get_int(surv, NNG_OPT_RECVFD, &fd));
    NUTS_TRUE(fd >= 0);
 
    // Not readable if not connected!
    NUTS_TRUE(nuts_poll_fd(fd) == false);
 
    // Even after connect (no message yet)
    NUTS_MARRY(surv, resp);
    NUTS_TRUE(nuts_poll_fd(fd) == false);
 
    // But once we send messages, it is.
    // We have to send a request, in order to send a reply.
 
    NUTS_PASS(nng_msg_alloc(&msg, 0));
    NUTS_PASS(nng_msg_append(msg, "xyz", 3));
    NUTS_PASS(nng_sendmsg(surv, msg, 0));
    NUTS_PASS(nng_recvmsg(resp, &msg, 0)); // recv on rep
    NUTS_PASS(nng_sendmsg(resp, msg, 0));  // echo it back
    NUTS_SLEEP(200); // give time for message to arrive
 
    NUTS_TRUE(nuts_poll_fd(fd) == true);
 
    // and receiving makes it no longer ready
    NUTS_PASS(nng_recvmsg(surv, &msg, 0));
    nng_msg_free(msg);
    NUTS_TRUE(nuts_poll_fd(fd) == false);
 
    // TODO verify unsolicited response
 
    NUTS_CLOSE(surv);
    NUTS_CLOSE(resp);
}
 
static void
test_surv_ctx_no_poll(void)
{
    int        fd;
    nng_socket surv;
    nng_ctx    ctx;
 
    NUTS_PASS(nng_surveyor0_open(&surv));
    NUTS_PASS(nng_ctx_open(&ctx, surv));
    NUTS_FAIL(nng_ctx_get_int(ctx, NNG_OPT_SENDFD, &fd), NNG_ENOTSUP);
    NUTS_FAIL(nng_ctx_get_int(ctx, NNG_OPT_RECVFD, &fd), NNG_ENOTSUP);
    NUTS_PASS(nng_ctx_close(ctx));
    NUTS_CLOSE(surv);
}
 
static void
test_surv_ctx_recv_nonblock(void)
{
    nng_socket surv;
    nng_socket resp;
    nng_ctx    ctx;
    nng_aio *  aio;
    nng_msg *  msg;
 
    NUTS_PASS(nng_surveyor0_open(&surv));
    NUTS_PASS(nng_respondent0_open(&resp));
    NUTS_PASS(nng_ctx_open(&ctx, surv));
    NUTS_PASS(nng_aio_alloc(&aio, NULL, NULL));
    NUTS_PASS(nng_msg_alloc(&msg, 0));
 
    NUTS_MARRY(surv, resp);
 
    nng_aio_set_msg(aio, msg);
    nng_ctx_send(ctx, aio);
    nng_aio_wait(aio);
    NUTS_PASS(nng_aio_result(aio));
    nng_aio_set_timeout(aio, 0); // Instant timeout
    nng_ctx_recv(ctx, aio);
 
    nng_aio_wait(aio);
    NUTS_FAIL(nng_aio_result(aio), NNG_ETIMEDOUT);
    NUTS_CLOSE(surv);
    NUTS_CLOSE(resp);
    nng_aio_free(aio);
}
 
static void
test_surv_ctx_send_nonblock(void)
{
    nng_socket surv;
    nng_ctx    ctx;
    nng_aio *  aio;
    nng_msg *  msg;
 
    NUTS_PASS(nng_surveyor0_open(&surv));
    NUTS_PASS(nng_ctx_open(&ctx, surv));
    NUTS_PASS(nng_aio_alloc(&aio, NULL, NULL));
    NUTS_PASS(nng_msg_alloc(&msg, 0));
 
    nng_aio_set_msg(aio, msg);
    nng_aio_set_timeout(aio, 0); // Instant timeout
    nng_ctx_send(ctx, aio);
    nng_aio_wait(aio);
    NUTS_PASS(nng_aio_result(aio)); // We never block
    NUTS_CLOSE(surv);
    nng_aio_free(aio);
}
 
static void
test_surv_send_best_effort(void)
{
    nng_socket surv;
    nng_socket resp;
 
    NUTS_PASS(nng_surveyor0_open(&surv));
    NUTS_PASS(nng_respondent0_open(&resp));
    NUTS_MARRY(surv, resp);
 
    for (int i = 0; i < 200; i++) {
        NUTS_SEND(surv, "junk");
    }
 
    NUTS_CLOSE(surv);
    NUTS_CLOSE(resp);
}
 
static void
test_surv_survey_timeout(void)
{
    nng_socket surv;
    nng_socket resp;
    char       buf[16];
    size_t     sz;
 
    NUTS_PASS(nng_surveyor0_open(&surv));
    NUTS_PASS(nng_respondent0_open(&resp));
    NUTS_PASS(nng_socket_set_ms(surv, NNG_OPT_SURVEYOR_SURVEYTIME, 50));
    NUTS_PASS(nng_socket_set_ms(surv, NNG_OPT_RECVTIMEO, 100));
 
    NUTS_MARRY(surv, resp);
 
    NUTS_SEND(surv, "hello");
    NUTS_RECV(resp, "hello");
 
    sz = sizeof(buf);
    NUTS_FAIL(nng_recv(surv, buf, &sz, 0), NNG_ETIMEDOUT);
    NUTS_SEND(resp, "world");
    NUTS_FAIL(nng_recv(surv, buf, &sz, 0), NNG_ESTATE);
 
    NUTS_CLOSE(surv);
    NUTS_CLOSE(resp);
}
 
static void
test_surv_ctx_recv_close_socket(void)
{
    nng_socket surv;
    nng_socket resp;
    nng_ctx    ctx;
    nng_aio *  aio;
    nng_msg *  m;
 
    NUTS_PASS(nng_surveyor0_open(&surv));
    NUTS_PASS(nng_respondent0_open(&resp));
    NUTS_PASS(nng_ctx_open(&ctx, surv));
    NUTS_PASS(nng_aio_alloc(&aio, NULL, NULL));
    NUTS_MARRY(surv, resp);
    NUTS_PASS(nng_msg_alloc(&m, 0));
    nng_aio_set_msg(aio, m);
    nng_ctx_send(ctx, aio);
    nng_aio_wait(aio);
    NUTS_PASS(nng_aio_result(aio));
 
    nng_ctx_recv(ctx, aio);
    nng_close(surv);
 
    NUTS_FAIL(nng_aio_result(aio), NNG_ECLOSED);
    nng_aio_free(aio);
    NUTS_CLOSE(resp);
}
 
static void
test_surv_context_multi(void)
{
    nng_socket surv;
    nng_socket resp;
    nng_ctx    c[5];
    nng_aio *  aio;
    nng_msg *  m;
    int        cnt = sizeof(c) / sizeof(c[0]);
 
    NUTS_PASS(nng_surveyor0_open(&surv));
    NUTS_PASS(nng_respondent0_open(&resp));
    NUTS_MARRY(surv, resp);
    NUTS_PASS(nng_socket_set_ms(surv, NNG_OPT_SURVEYOR_SURVEYTIME, 200));
    NUTS_PASS(nng_aio_alloc(&aio, NULL, NULL));
 
    for (int i = 0; i < cnt; i++) {
        NUTS_PASS(nng_ctx_open(&c[i], surv));
    }
 
    for (int i = 0; i < cnt; i++) {
        NUTS_PASS(nng_msg_alloc(&m, 0));
        NUTS_PASS(nng_msg_append_u32(m, i));
        nng_aio_set_msg(aio, m);
        nng_ctx_send(c[i], aio);
        nng_aio_wait(aio);
        NUTS_PASS(nng_aio_result(aio));
    }
 
    for (int i = 0; i < cnt; i++) {
        NUTS_PASS(nng_recvmsg(resp, &m, 0));
        NUTS_PASS(nng_sendmsg(resp, m, 0));
    }
 
    for (int i = cnt - 1; i >= 0; i--) {
        uint32_t x;
        nng_ctx_recv(c[i], aio);
        nng_aio_wait(aio);
        NUTS_PASS(nng_aio_result(aio));
        m = nng_aio_get_msg(aio);
        TEST_ASSERT(m != NULL);
        NUTS_PASS(nng_msg_trim_u32(m, &x));
        NUTS_TRUE(x == (uint32_t) i);
        nng_msg_free(m);
    }
 
    for (int i = 0; i < cnt; i++) {
        nng_ctx_recv(c[i], aio);
        nng_aio_wait(aio);
        NUTS_TRUE(nng_aio_result(aio) != 0);
    }
    for (int i = 0; i < cnt; i++) {
        nng_ctx_close(c[i]);
    }
    NUTS_CLOSE(surv);
    NUTS_CLOSE(resp);
    nng_aio_free(aio);
}
 
static void
test_surv_validate_peer(void)
{
    nng_socket s1, s2;
    nng_stat * stats;
    nng_stat * reject;
    char *     addr;
 
    NUTS_ADDR(addr, "inproc");
    NUTS_PASS(nng_surveyor0_open(&s1));
    NUTS_PASS(nng_surveyor0_open(&s2));
 
    NUTS_PASS(nng_listen(s1, addr, NULL, 0));
    NUTS_PASS(nng_dial(s2, addr, NULL, NNG_FLAG_NONBLOCK));
 
    NUTS_SLEEP(100);
    NUTS_PASS(nng_stats_get(&stats));
 
    NUTS_TRUE(stats != NULL);
    NUTS_TRUE((reject = nng_stat_find_socket(stats, s1)) != NULL);
    NUTS_TRUE((reject = nng_stat_find(reject, "reject")) != NULL);
 
    NUTS_TRUE(nng_stat_type(reject) == NNG_STAT_COUNTER);
    NUTS_TRUE(nng_stat_value(reject) > 0);
 
    NUTS_PASS(nng_close(s1));
    NUTS_PASS(nng_close(s2));
    nng_stats_free(stats);
}
 
TEST_LIST = {
    { "survey identity", test_surv_identity },
    { "survey ttl option", test_surv_ttl_option },
    { "survey survey time option", test_surv_survey_time_option },
    { "survey recv bad state", test_surv_recv_bad_state },
    { "survey recv garbage", test_surv_recv_garbage },
    { "survey respondent exchange", test_surv_resp_exchange },
    { "survey cancel", test_surv_cancel },
    { "survey cancel abort recv", test_surv_cancel_abort_recv },
    { "survey cancel post recv", test_surv_cancel_post_recv },
    { "survey poll writable", test_surv_poll_writeable },
    { "survey poll readable", test_surv_poll_readable },
    { "survey context does not poll", test_surv_ctx_no_poll },
    { "survey context recv close socket",
        test_surv_ctx_recv_close_socket },
    { "survey context recv nonblock", test_surv_ctx_recv_nonblock },
    { "survey context send nonblock", test_surv_ctx_send_nonblock },
    { "survey timeout", test_surv_survey_timeout },
    { "survey send best effort", test_surv_send_best_effort },
    { "survey context multi", test_surv_context_multi },
    { "survey validate peer", test_surv_validate_peer },
    { NULL, NULL },
};