check_client_async_connect.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #ifndef WIN32
  7. #include <unistd.h>
  8. #endif
  9. #include "ua_types.h"
  10. #include "ua_server.h"
  11. #include "ua_client.h"
  12. #include "client/ua_client_internal.h"
  13. #include "ua_client_highlevel_async.h"
  14. #include "ua_config_default.h"
  15. #include "ua_network_tcp.h"
  16. #include "check.h"
  17. #include "testing_clock.h"
  18. #include "testing_networklayers.h"
  19. #include "thread_wrapper.h"
  20. UA_Server *server;
  21. UA_ServerConfig *config;
  22. UA_Boolean running;
  23. UA_ServerNetworkLayer nl;
  24. THREAD_HANDLE server_thread;
  25. THREAD_CALLBACK(serverloop) {
  26. while(running)
  27. UA_Server_run_iterate(server, true);
  28. return 0;
  29. }
  30. static void
  31. onConnect (UA_Client *Client, void *connected, UA_UInt32 requestId,
  32. void *response) {
  33. if (UA_Client_getState (Client) == UA_CLIENTSTATE_SESSION)
  34. *(UA_Boolean *)connected = true;
  35. }
  36. static void setup(void) {
  37. running = true;
  38. config = UA_ServerConfig_new_default();
  39. server = UA_Server_new(config);
  40. UA_Server_run_startup(server);
  41. THREAD_CREATE(server_thread, serverloop);
  42. /* Waiting server is up */
  43. UA_comboSleep(1000);
  44. }
  45. static void teardown(void) {
  46. running = false;
  47. THREAD_JOIN(server_thread);
  48. UA_Server_run_shutdown(server);
  49. UA_Server_delete(server);
  50. UA_ServerConfig_delete(config);
  51. }
  52. static void
  53. asyncBrowseCallback(UA_Client *Client, void *userdata,
  54. UA_UInt32 requestId, UA_BrowseResponse *response) {
  55. UA_UInt16 *asyncCounter = (UA_UInt16*)userdata;
  56. (*asyncCounter)++;
  57. }
  58. START_TEST(Client_connect_async){
  59. UA_StatusCode retval;
  60. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  61. UA_Boolean connected = false;
  62. UA_Client_connect_async(client, "opc.tcp://localhost:4840", onConnect,
  63. &connected);
  64. /*Windows needs time to response*/
  65. UA_sleep_ms(100);
  66. UA_UInt32 reqId = 0;
  67. UA_UInt16 asyncCounter = 0;
  68. UA_BrowseRequest bReq;
  69. UA_BrowseRequest_init (&bReq);
  70. bReq.requestedMaxReferencesPerNode = 0;
  71. bReq.nodesToBrowse = UA_BrowseDescription_new ();
  72. bReq.nodesToBrowseSize = 1;
  73. bReq.nodesToBrowse[0].nodeId = UA_NODEID_NUMERIC (0, UA_NS0ID_OBJECTSFOLDER);
  74. bReq.nodesToBrowse[0].resultMask = UA_BROWSERESULTMASK_ALL; /* return everything */
  75. /* Connected gets updated when client is connected */
  76. do{
  77. if(connected) {
  78. /* If not connected requests are not sent */
  79. UA_Client_sendAsyncBrowseRequest (client, &bReq, asyncBrowseCallback,
  80. &asyncCounter, &reqId);
  81. }
  82. /* Manual clock for unit tests */
  83. UA_comboSleep(20);
  84. retval = UA_Client_run_iterate(client, 0);
  85. /*fix infinite loop, but why is server occasionally shut down in Appveyor?!*/
  86. if(retval == UA_STATUSCODE_BADCONNECTIONCLOSED)
  87. break;
  88. } while(reqId < 10);
  89. UA_BrowseRequest_deleteMembers(&bReq);
  90. ck_assert_uint_eq(connected, true);
  91. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  92. /* With default setting the client uses 4 requests to connect */
  93. ck_assert_uint_eq(asyncCounter, 10-4);
  94. UA_Client_disconnect(client);
  95. UA_Client_delete (client);
  96. }
  97. END_TEST
  98. START_TEST(Client_no_connection) {
  99. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  100. UA_Boolean connected = false;
  101. UA_StatusCode retval = UA_Client_connect_async(client, "opc.tcp://localhost:4840", onConnect,
  102. &connected);
  103. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  104. UA_Client_recv = client->connection.recv;
  105. client->connection.recv = UA_Client_recvTesting;
  106. //simulating unconnected server
  107. UA_Client_recvTesting_result = UA_STATUSCODE_BADCONNECTIONCLOSED;
  108. retval = UA_Client_run_iterate(client, 0);
  109. ck_assert_uint_eq(retval, UA_STATUSCODE_BADCONNECTIONCLOSED);
  110. UA_Client_disconnect(client);
  111. UA_Client_delete(client);
  112. }
  113. END_TEST
  114. START_TEST(Client_without_run_iterate) {
  115. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  116. UA_Boolean connected = false;
  117. UA_Client_connect_async(client, "opc.tcp://localhost:4840", onConnect,
  118. &connected);
  119. UA_Client_delete(client);
  120. }
  121. END_TEST
  122. static Suite* testSuite_Client(void) {
  123. Suite *s = suite_create("Client");
  124. TCase *tc_client_connect = tcase_create("Client Connect Async");
  125. tcase_add_checked_fixture(tc_client_connect, setup, teardown);
  126. tcase_add_test(tc_client_connect, Client_connect_async);
  127. tcase_add_test(tc_client_connect, Client_no_connection);
  128. tcase_add_test(tc_client_connect, Client_without_run_iterate);
  129. suite_add_tcase(s,tc_client_connect);
  130. return s;
  131. }
  132. int main(void) {
  133. Suite *s = testSuite_Client();
  134. SRunner *sr = srunner_create(s);
  135. srunner_set_fork_status(sr, CK_NOFORK);
  136. srunner_run_all(sr, CK_NORMAL);
  137. int number_failed = srunner_ntests_failed(sr);
  138. srunner_free(sr);
  139. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  140. }