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