ipc_stress.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. Copyright (c) 2012 Martin Sustrik All rights reserved.
  3. Copyright 2017 Garrett D'Amore <garrett@damore.org>
  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. The above copyright notice and this permission notice shall be included
  11. in all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  15. THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  17. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  18. IN THE SOFTWARE.
  19. */
  20. #include "../src/nn.h"
  21. #include "../src/pair.h"
  22. #include "../src/pubsub.h"
  23. #include "../src/pipeline.h"
  24. #include "../src/ipc.h"
  25. #include "testutil.h"
  26. #include "../src/utils/thread.c"
  27. #include "../src/utils/atomic.h"
  28. #include "../src/utils/atomic.c"
  29. /* Stress test the IPC transport. */
  30. #ifndef NN_HAVE_WSL
  31. #define THREAD_COUNT 10
  32. #define TEST_LOOPS 10
  33. #define SOCKET_ADDRESS "ipc://test-stress.ipc"
  34. static void server(NN_UNUSED void *arg)
  35. {
  36. int bytes;
  37. int count;
  38. int sock = nn_socket(AF_SP, NN_PULL);
  39. int res[TEST_LOOPS];
  40. nn_assert(sock >= 0);
  41. nn_assert(nn_bind(sock, SOCKET_ADDRESS) >= 0);
  42. count = THREAD_COUNT * TEST_LOOPS;
  43. memset(res, 0, sizeof (res));
  44. while (count > 0)
  45. {
  46. char *buf = NULL;
  47. int tid;
  48. int num;
  49. bytes = nn_recv(sock, &buf, NN_MSG, 0);
  50. nn_assert(bytes >= 0);
  51. nn_assert(bytes >= 2);
  52. nn_assert(buf[0] >= 'A' && buf[0] <= 'Z');
  53. nn_assert(buf[1] >= 'a' && buf[0] <= 'z');
  54. tid = buf[0]-'A';
  55. num = buf[1]-'a';
  56. nn_assert(tid < THREAD_COUNT);
  57. nn_assert(res[tid] == num);
  58. res[tid]=num+1;
  59. nn_freemsg(buf);
  60. count--;
  61. }
  62. nn_close(sock);
  63. }
  64. static void client(void *arg)
  65. {
  66. intptr_t id = (intptr_t)arg;
  67. int bytes;
  68. char msg[3];
  69. size_t sz_msg;
  70. int i;
  71. msg[0] = 'A' + id%26;
  72. msg[1] = 'a';
  73. msg[2] = '\0';
  74. /* '\0' too */
  75. sz_msg = strlen (msg) + 1;
  76. for (i = 0; i < TEST_LOOPS; i++) {
  77. int cli_sock = nn_socket (AF_SP, NN_PUSH);
  78. msg[1] = 'a' + i%26;
  79. nn_assert (cli_sock >= 0);
  80. nn_assert (nn_connect (cli_sock, SOCKET_ADDRESS) >= 0);
  81. /* Give time to allow for connect to establish. */
  82. nn_sleep (50);
  83. bytes = nn_send (cli_sock, msg, sz_msg, 0);
  84. /* This would better be handled via semaphore or condvar. */
  85. nn_sleep (100);
  86. nn_assert ((size_t)bytes == sz_msg);
  87. nn_close (cli_sock);
  88. }
  89. }
  90. int main()
  91. {
  92. int i;
  93. struct nn_thread srv_thread;
  94. struct nn_thread cli_threads[THREAD_COUNT];
  95. /* Stress the shutdown algorithm. */
  96. nn_thread_init(&srv_thread, server, NULL);
  97. for (i = 0; i != THREAD_COUNT; ++i)
  98. nn_thread_init(&cli_threads[i], client, (void *)(intptr_t)i);
  99. for (i = 0; i != THREAD_COUNT; ++i)
  100. nn_thread_term(&cli_threads[i]);
  101. return 0;
  102. }
  103. #else
  104. int main()
  105. {
  106. return (0);
  107. }
  108. #endif