File: test_thread_safe.cpp

package info (click to toggle)
zeromq3 4.3.5-1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,548 kB
  • sloc: cpp: 56,475; ansic: 4,968; makefile: 1,607; sh: 1,400; xml: 196; python: 40
file content (79 lines) | stat: -rw-r--r-- 2,091 bytes parent folder | download
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
/* SPDX-License-Identifier: MPL-2.0 */

#include "testutil.hpp"
#include "testutil_unity.hpp"

SETUP_TEARDOWN_TESTCONTEXT

//  Client threads loop on send/recv until told to exit
void client_thread (void *client_)
{
    for (int count = 0; count < 15000; count++) {
        send_string_expect_success (client_, "0", 0);
    }
    send_string_expect_success (client_, "1", 0);
}

void test_thread_safe ()
{
    char my_endpoint[MAX_SOCKET_STRING];

    void *server = test_context_socket (ZMQ_SERVER);
    bind_loopback_ipv4 (server, my_endpoint, sizeof my_endpoint);

    void *client = test_context_socket (ZMQ_CLIENT);

    TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (client, my_endpoint));

    void *t1 = zmq_threadstart (client_thread, client);
    void *t2 = zmq_threadstart (client_thread, client);

    char data;
    int threads_completed = 0;
    while (threads_completed < 2) {
        TEST_ASSERT_SUCCESS_ERRNO (zmq_recv (server, &data, 1, 0));
        if (data == '1')
            threads_completed++; //  Thread ended
    }
    zmq_threadclose (t1);
    zmq_threadclose (t2);

    test_context_socket_close (server);
    test_context_socket_close (client);
}

void test_getsockopt_thread_safe (void *const socket_)
{
    int thread_safe;
    size_t size = sizeof (int);
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_getsockopt (socket_, ZMQ_THREAD_SAFE, &thread_safe, &size));
    TEST_ASSERT_EQUAL_INT (1, thread_safe);
}

void test_client_getsockopt_thread_safe ()
{
    void *client = test_context_socket (ZMQ_CLIENT);
    test_getsockopt_thread_safe (client);
    test_context_socket_close (client);
}

void test_server_getsockopt_thread_safe ()
{
    void *server = test_context_socket (ZMQ_SERVER);
    test_getsockopt_thread_safe (server);
    test_context_socket_close (server);
}

int main (void)
{
    setup_test_environment ();

    // TODO this file could be merged with test_client_server
    UNITY_BEGIN ();
    RUN_TEST (test_client_getsockopt_thread_safe);
    RUN_TEST (test_server_getsockopt_thread_safe);
    RUN_TEST (test_thread_safe);

    return UNITY_END ();
}