1 /*
2 Copyright (c) 2012 Martin Sustrik All rights reserved.
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"),
6 to deal in the Software without restriction, including without limitation
7 the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 and/or sell copies of the Software, and to permit persons to whom
9 the Software is furnished to do so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included
12 in all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20 IN THE SOFTWARE.
21 */
22
23 #include "../src/nn.h"
24 #include "../src/pair.h"
25
26 #include "../src/utils/thread.c"
27 #include "testutil.h"
28
worker(NN_UNUSED void * arg)29 static void worker (NN_UNUSED void *arg)
30 {
31 int rc;
32 int s;
33 char buf [3];
34
35 /* Test socket. */
36 s = test_socket (AF_SP, NN_PAIR);
37
38 /* Launch blocking function to check that it will be unblocked once
39 nn_term() is called from the main thread. */
40 rc = nn_recv (s, buf, sizeof (buf), 0);
41 nn_assert (rc == -1 && nn_errno () == EBADF);
42
43 /* Check that all subsequent operations fail in synchronous manner. */
44 rc = nn_recv (s, buf, sizeof (buf), 0);
45 nn_assert (rc == -1 && nn_errno () == EBADF);
46 test_close (s);
47 }
48
main()49 int main ()
50 {
51 int rc;
52 int s;
53 struct nn_thread thread;
54
55 /* Close the socket with no associated endpoints. */
56 s = test_socket (AF_SP, NN_PAIR);
57 test_close (s);
58
59 /* Test nn_term() before nn_close(). */
60 nn_thread_init (&thread, worker, NULL);
61 nn_sleep (100);
62 nn_term ();
63
64 /* Check that it's not possible to create new sockets after nn_term(). */
65 rc = nn_socket (AF_SP, NN_PAIR);
66 nn_assert (rc == -1);
67 errno_assert (nn_errno () == ETERM);
68
69 /* Wait till worker thread terminates. */
70 nn_thread_term (&thread);
71
72 return 0;
73 }
74
75