| 1 | /*: |
| 2 | Copyright (c) 2007-2017 Contributors as noted in the AUTHORS file |
| 3 | |
| 4 | This file is part of libzmq, the ZeroMQ core engine in C++. |
| 5 | |
| 6 | libzmq is free software; you can redistribute it and/or modify it under |
| 7 | the terms of the GNU Lesser General Public License (LGPL) as published |
| 8 | by the Free Software Foundation; either version 3 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | As a special exception, the Contributors give you permission to link |
| 12 | this library with independent modules to produce an executable, |
| 13 | regardless of the license terms of these independent modules, and to |
| 14 | copy and distribute the resulting executable under terms of your choice, |
| 15 | provided that you also meet, for each linked independent module, the |
| 16 | terms and conditions of the license of that module. An independent |
| 17 | module is a module which is not derived from or based on this library. |
| 18 | If you modify this library, you must extend this exception to your |
| 19 | version of the library. |
| 20 | |
| 21 | libzmq is distributed in the hope that it will be useful, but WITHOUT |
| 22 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 23 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
| 24 | License for more details. |
| 25 | |
| 26 | You should have received a copy of the GNU Lesser General Public License |
| 27 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 28 | */ |
| 29 | |
| 30 | #include "testutil.hpp" |
| 31 | #include "testutil_unity.hpp" |
| 32 | |
| 33 | SETUP_TEARDOWN_TESTCONTEXT |
| 34 | |
| 35 | // Client threads loop on send/recv until told to exit |
| 36 | void client_thread (void *client_) |
| 37 | { |
| 38 | for (int count = 0; count < 15000; count++) { |
| 39 | send_string_expect_success (client_, "0" , 0); |
| 40 | } |
| 41 | send_string_expect_success (client_, "1" , 0); |
| 42 | } |
| 43 | |
| 44 | void test_thread_safe () |
| 45 | { |
| 46 | char my_endpoint[MAX_SOCKET_STRING]; |
| 47 | |
| 48 | void *server = test_context_socket (ZMQ_SERVER); |
| 49 | bind_loopback_ipv4 (server, my_endpoint, sizeof my_endpoint); |
| 50 | |
| 51 | void *client = test_context_socket (ZMQ_CLIENT); |
| 52 | |
| 53 | TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (client, my_endpoint)); |
| 54 | |
| 55 | void *t1 = zmq_threadstart (client_thread, client); |
| 56 | void *t2 = zmq_threadstart (client_thread, client); |
| 57 | |
| 58 | char data; |
| 59 | int threads_completed = 0; |
| 60 | while (threads_completed < 2) { |
| 61 | TEST_ASSERT_SUCCESS_ERRNO (zmq_recv (server, &data, 1, 0)); |
| 62 | if (data == '1') |
| 63 | threads_completed++; // Thread ended |
| 64 | } |
| 65 | zmq_threadclose (t1); |
| 66 | zmq_threadclose (t2); |
| 67 | |
| 68 | test_context_socket_close (server); |
| 69 | test_context_socket_close (client); |
| 70 | } |
| 71 | |
| 72 | void test_getsockopt_thread_safe (void *const socket_) |
| 73 | { |
| 74 | int thread_safe; |
| 75 | size_t size = sizeof (int); |
| 76 | TEST_ASSERT_SUCCESS_ERRNO ( |
| 77 | zmq_getsockopt (socket_, ZMQ_THREAD_SAFE, &thread_safe, &size)); |
| 78 | TEST_ASSERT_EQUAL_INT (1, thread_safe); |
| 79 | } |
| 80 | |
| 81 | void test_client_getsockopt_thread_safe () |
| 82 | { |
| 83 | void *client = test_context_socket (ZMQ_CLIENT); |
| 84 | test_getsockopt_thread_safe (client); |
| 85 | test_context_socket_close (client); |
| 86 | } |
| 87 | |
| 88 | void test_server_getsockopt_thread_safe () |
| 89 | { |
| 90 | void *server = test_context_socket (ZMQ_SERVER); |
| 91 | test_getsockopt_thread_safe (server); |
| 92 | test_context_socket_close (server); |
| 93 | } |
| 94 | |
| 95 | int main (void) |
| 96 | { |
| 97 | setup_test_environment (); |
| 98 | |
| 99 | // TODO this file could be merged with test_client_server |
| 100 | UNITY_BEGIN (); |
| 101 | RUN_TEST (test_client_getsockopt_thread_safe); |
| 102 | RUN_TEST (test_server_getsockopt_thread_safe); |
| 103 | RUN_TEST (test_thread_safe); |
| 104 | |
| 105 | return UNITY_END (); |
| 106 | } |
| 107 | |