check_client_async_connect.c 5.0 KB

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