check_client_async_connect.c 5.1 KB

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