File: test_many_sockets.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 (71 lines) | stat: -rw-r--r-- 2,060 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
/* SPDX-License-Identifier: MPL-2.0 */

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

#include <stdio.h>
#include <stdlib.h>
#include <vector>

SETUP_TEARDOWN_TESTCONTEXT

void test_system_max ()
{
    // Keep allocating sockets until we run out of system resources
    const int no_of_sockets = 2 * 65536;
    zmq_ctx_set (get_test_context (), ZMQ_MAX_SOCKETS, no_of_sockets);
    std::vector<void *> sockets;

    while (true) {
        void *socket = zmq_socket (get_test_context (), ZMQ_PAIR);
        if (!socket)
            break;
        sockets.push_back (socket);
    }
    TEST_ASSERT_LESS_OR_EQUAL (no_of_sockets,
                               static_cast<int> (sockets.size ()));
    printf ("Socket creation failed after %i sockets\n",
            static_cast<int> (sockets.size ()));

    //  System is out of resources, further calls to zmq_socket should return NULL
    for (unsigned int i = 0; i < 10; ++i) {
        TEST_ASSERT_NULL (zmq_socket (get_test_context (), ZMQ_PAIR));
    }
    // Clean up.
    for (unsigned int i = 0; i < sockets.size (); ++i)
        TEST_ASSERT_SUCCESS_ERRNO (zmq_close (sockets[i]));
}

void test_zmq_default_max ()
{
    //  Keep allocating sockets until we hit the default limit
    std::vector<void *> sockets;

    while (true) {
        void *socket = zmq_socket (get_test_context (), ZMQ_PAIR);
        if (!socket)
            break;
        sockets.push_back (socket);
    }
    //  We may stop sooner if system has fewer available sockets
    TEST_ASSERT_LESS_OR_EQUAL (ZMQ_MAX_SOCKETS_DFLT, sockets.size ());

    //  Further calls to zmq_socket should return NULL
    for (unsigned int i = 0; i < 10; ++i) {
        TEST_ASSERT_NULL (zmq_socket (get_test_context (), ZMQ_PAIR));
    }

    //  Clean up
    for (unsigned int i = 0; i < sockets.size (); ++i)
        TEST_ASSERT_SUCCESS_ERRNO (zmq_close (sockets[i]));
}

int main (void)
{
    setup_test_environment ();

    UNITY_BEGIN ();
    RUN_TEST (test_system_max);
    RUN_TEST (test_zmq_default_max);
    return UNITY_END ();
}