PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/branches/ruby/binding-c/runtime/c/src/test/common/test_conn.c

#
C | 184 lines | 135 code | 41 blank | 8 comment | 24 complexity | 49bf85d9e05c4cac84b06803205a526c MD5 | raw file
Possible License(s): Apache-2.0
  1. /**
  2. * etchconn test routines.
  3. */
  4. #include "CUnit.h"
  5. #include "Basic.h"
  6. #include "Console.h"
  7. #include "Automated.h"
  8. #include "etchconn.h"
  9. #include "apr_time.h"
  10. #include "etchlog.h"
  11. #define MAX_TEST_CONNECTIONS 10
  12. static apr_pool_t *pool_ptr = NULL;
  13. static etch_tcpconn *tcpconn_ptr = NULL;
  14. static etch_tcpconn *tcpconn2_ptr = NULL;
  15. static etch_tcplistener *listener_ptr = NULL;
  16. static etch_tcpconn *accepted[MAX_TEST_CONNECTIONS];
  17. static BOOLEAN got_accepted(apr_socket_t *socket_ptr);
  18. static void got_data(void *data_ptr, size_t len);
  19. int setup(void)
  20. {
  21. apr_status_t r;
  22. r = apr_initialize();
  23. if (r == APR_SUCCESS)
  24. r = apr_pool_create(&pool_ptr, NULL);
  25. if (r == APR_SUCCESS )
  26. {
  27. listener_ptr = etchconn_create_listener("127.0.0.1", 7302, 5, 5, pool_ptr);
  28. if (listener_ptr != NULL)
  29. listener_ptr->conn.handle_accepted = got_accepted;
  30. tcpconn_ptr = etchconn_create_tcp("127.0.0.1", 7302, 5, pool_ptr);
  31. /* create another connection to listener */
  32. tcpconn2_ptr = etchconn_create_tcp("127.0.0.1", 7302, 5, pool_ptr);
  33. }
  34. memset(accepted, 0, sizeof(accepted));
  35. return (listener_ptr != NULL && tcpconn_ptr != NULL && tcpconn2_ptr != NULL ? 0 : 1);
  36. }
  37. int teardown(void)
  38. {
  39. int i;
  40. etchconn_destroy((etchconn*)tcpconn_ptr);
  41. etchconn_destroy((etchconn*)tcpconn2_ptr);
  42. etchconn_destroy((etchconn*)listener_ptr);
  43. for(i = 0; i < MAX_TEST_CONNECTIONS && accepted[i] != NULL; i++)
  44. {
  45. etchconn_destroy((etchconn*)accepted[i]);
  46. accepted[i] = NULL;
  47. }
  48. if (pool_ptr != NULL)
  49. apr_pool_destroy(pool_ptr);
  50. pool_ptr = NULL;
  51. apr_terminate();
  52. return 0;
  53. }
  54. static BOOLEAN got_accepted(apr_socket_t *socket_ptr)
  55. {
  56. BOOLEAN rc = FALSE;
  57. int i;
  58. etch_tcpconn *accepted_ptr = etchconn_create_tcp_with_socket(socket_ptr);
  59. if (accepted_ptr != NULL)
  60. {
  61. /* need to initialize data handler */
  62. accepted_ptr->conn.handle_data = got_data;
  63. rc = etchconn_start((etchconn *) accepted_ptr);
  64. /* save the pointer for later releasing */
  65. i = 0;
  66. while (i < MAX_TEST_CONNECTIONS && accepted[i] != NULL)
  67. ++i;
  68. if (i < MAX_TEST_CONNECTIONS)
  69. accepted[i] = accepted_ptr;
  70. }
  71. return rc;
  72. }
  73. static void got_data(void *data_ptr, size_t len)
  74. {
  75. etchlog_report("etchconntest", ETCHLOG_DEBUG, "Got data: %s\n", (const char*) data_ptr);
  76. }
  77. void testSimple(void)
  78. {
  79. int i;
  80. int rc;
  81. char data[] = "this is testSimple data.";
  82. CU_ASSERT(etchconn_start((etchconn*)listener_ptr));
  83. rc = etchmon_wait_until_equal(listener_ptr->conn.monitor_ptr, ETCHCONN_UP, (strlen(ETCHCONN_UP)+1)*sizeof(char),0);
  84. CU_ASSERT(etchconn_start((etchconn*)tcpconn_ptr));
  85. rc = etchmon_wait_until_equal(tcpconn_ptr->conn.monitor_ptr, ETCHCONN_UP, (strlen(ETCHCONN_UP)+1)*sizeof(char),0);
  86. CU_ASSERT(rc==ETCHMON_STATUS_SUCCESS);
  87. for(i = 0; i < 50; i++)
  88. {
  89. CU_ASSERT(etchconn_send_tcp((etchconn*)tcpconn_ptr, data, (strlen(data)+1)*sizeof(char)));
  90. apr_sleep(100000);
  91. }
  92. apr_sleep(1000000);
  93. etchconn_stop((etchconn*)tcpconn_ptr);
  94. apr_sleep(1000000);
  95. CU_ASSERT_PTR_NOT_NULL(accepted[0]);
  96. etchconn_stop((etchconn*)accepted[0]);
  97. etchconn_stop((etchconn*)listener_ptr);
  98. }
  99. void testMoreConnections(void)
  100. {
  101. int i;
  102. int rc;
  103. char data[] = "CONN1: this is testMoreConnections data.";
  104. char data2[] = "CONN2: this is testMoreConnections data.";
  105. CU_ASSERT(etchconn_start((etchconn*)listener_ptr));
  106. rc = etchmon_wait_until_equal(listener_ptr->conn.monitor_ptr, ETCHCONN_UP, (strlen(ETCHCONN_UP)+1)*sizeof(char),0);
  107. CU_ASSERT(etchconn_start((etchconn*)tcpconn_ptr));
  108. /* create another connection to listener */
  109. CU_ASSERT(etchconn_start((etchconn*) tcpconn2_ptr));
  110. rc = etchmon_wait_until_equal(tcpconn_ptr->conn.monitor_ptr, ETCHCONN_UP, (strlen(ETCHCONN_UP)+1)*sizeof(char),0);
  111. CU_ASSERT(rc==ETCHMON_STATUS_SUCCESS);
  112. rc = etchmon_wait_until_equal(tcpconn2_ptr->conn.monitor_ptr, ETCHCONN_UP, (strlen(ETCHCONN_UP)+1)*sizeof(char),0);
  113. CU_ASSERT(rc==ETCHMON_STATUS_SUCCESS);
  114. for(i = 0; i < 100; i++)
  115. {
  116. CU_ASSERT(etchconn_send_tcp((etchconn*)tcpconn_ptr, data, (strlen(data)+1)*sizeof(char)));
  117. CU_ASSERT(etchconn_send_tcp((etchconn*)tcpconn2_ptr, data2, (strlen(data2)+1)*sizeof(char)));
  118. apr_sleep(10000);
  119. }
  120. apr_sleep(1000000);
  121. etchconn_stop((etchconn*)tcpconn_ptr);
  122. etchconn_stop((etchconn*)tcpconn2_ptr);
  123. apr_sleep(1000000);
  124. CU_ASSERT_PTR_NOT_NULL(accepted[0]);
  125. etchconn_stop((etchconn*)accepted[0]);
  126. CU_ASSERT_PTR_NOT_NULL(accepted[1]);
  127. etchconn_stop((etchconn*)accepted[1]);
  128. etchconn_stop((etchconn*)listener_ptr);
  129. }
  130. int main(int argc, char **argv)
  131. {
  132. CU_pSuite ps;
  133. CU_pTest pt;
  134. printf("hellor world!");
  135. CU_initialize_registry();
  136. ps = CU_add_suite("etchconn test suit", setup, teardown);
  137. pt = CU_add_test(ps, "testSimple", testSimple);
  138. pt = CU_add_test(ps, "testMoreConnections", testMoreConnections);
  139. CU_automated_run_tests();
  140. /*CU_console_run_tests();*/
  141. CU_cleanup_registry();
  142. return 0;
  143. }