check_client_async_connect.c 5.1 KB

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