check_client_async.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 <stdio.h>
  12. #include <stdlib.h>
  13. #include "testing_clock.h"
  14. #include "testing_networklayers.h"
  15. #include "thread_wrapper.h"
  16. UA_Server *server;
  17. UA_Boolean running;
  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 setup(void) {
  25. running = true;
  26. server = UA_Server_new();
  27. UA_ServerConfig_setDefault(UA_Server_getConfig(server));
  28. UA_Server_run_startup(server);
  29. THREAD_CREATE(server_thread, serverloop);
  30. }
  31. static void teardown(void) {
  32. running = false;
  33. THREAD_JOIN(server_thread);
  34. UA_Server_run_shutdown(server);
  35. UA_Server_delete(server);
  36. }
  37. static void asyncReadCallback(UA_Client *client, void *userdata,
  38. UA_UInt32 requestId, const UA_ReadResponse *response) {
  39. UA_UInt16 *asyncCounter = (UA_UInt16*) userdata;
  40. if (response->responseHeader.serviceResult == UA_STATUSCODE_BADTIMEOUT) {
  41. (*asyncCounter) = 9999;
  42. UA_fakeSleep(10);
  43. } else {
  44. (*asyncCounter)++;
  45. UA_fakeSleep(10);
  46. }
  47. }
  48. static void asyncReadValueAtttributeCallback(UA_Client *client, void *userdata,
  49. UA_UInt32 requestId, UA_Variant *var) {
  50. UA_UInt16 *asyncCounter = (UA_UInt16*) userdata;
  51. (*asyncCounter)++;
  52. UA_fakeSleep(10);
  53. }
  54. START_TEST(Client_highlevel_async_readValue) {
  55. UA_Client *client = UA_Client_new();
  56. UA_ClientConfig *clientConfig = UA_Client_getConfig(client);
  57. UA_ClientConfig_setDefault(clientConfig);
  58. clientConfig->outStandingPublishRequests = 0;
  59. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  60. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  61. UA_Client_recv = client->connection.recv;
  62. client->connection.recv = UA_Client_recvTesting;
  63. UA_UInt16 asyncCounter = 0;
  64. UA_UInt32 reqId = 0;
  65. retval = UA_Client_readValueAttribute_async(client,
  66. UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_CURRENTTIME),
  67. (UA_ClientAsyncReadValueAttributeCallback) asyncReadValueAtttributeCallback,
  68. (void*)&asyncCounter, &reqId);
  69. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  70. /* Process async responses during 1s */
  71. UA_Client_run_iterate(client, 999 + 1);
  72. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  73. ck_assert_uint_eq(asyncCounter, 1);
  74. /* Simulate network cable unplugged (no response from server) */
  75. UA_Client_recvTesting_result = UA_STATUSCODE_GOODNONCRITICALTIMEOUT;
  76. UA_Client_disconnect(client);
  77. UA_Client_delete(client);
  78. } END_TEST
  79. START_TEST(Client_read_async) {
  80. UA_Client *client = UA_Client_new();
  81. UA_ClientConfig *clientConfig = UA_Client_getConfig(client);
  82. UA_ClientConfig_setDefault(clientConfig);
  83. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  84. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  85. UA_UInt16 asyncCounter = 0;
  86. UA_ReadRequest rr;
  87. UA_ReadRequest_init(&rr);
  88. UA_ReadValueId rvid;
  89. UA_ReadValueId_init(&rvid);
  90. rvid.attributeId = UA_ATTRIBUTEID_VALUE;
  91. rvid.nodeId = UA_NODEID_NUMERIC(0,
  92. UA_NS0ID_SERVER_SERVERSTATUS_CURRENTTIME);
  93. rr.nodesToRead = &rvid;
  94. rr.nodesToReadSize = 1;
  95. /* Send 100 requests */
  96. for (size_t i = 0; i < 100; i++) {
  97. retval = __UA_Client_AsyncService(client, &rr,
  98. &UA_TYPES[UA_TYPES_READREQUEST],
  99. (UA_ClientAsyncServiceCallback) asyncReadCallback,
  100. &UA_TYPES[UA_TYPES_READRESPONSE], &asyncCounter, NULL);
  101. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  102. }
  103. /* Process async responses during 1s */
  104. retval = UA_Client_run_iterate(client, 999);
  105. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  106. ck_assert_uint_eq(asyncCounter, 100);
  107. UA_Client_disconnect(client);
  108. UA_Client_delete(client);
  109. }END_TEST
  110. START_TEST(Client_read_async_timed) {
  111. UA_Client *client = UA_Client_new();
  112. UA_ClientConfig *clientConfig = UA_Client_getConfig(client);
  113. UA_ClientConfig_setDefault(clientConfig);
  114. clientConfig->outStandingPublishRequests = 0;
  115. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  116. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  117. UA_Client_recv = client->connection.recv;
  118. client->connection.recv = UA_Client_recvTesting;
  119. UA_UInt16 asyncCounter = 0;
  120. UA_ReadRequest rr;
  121. UA_ReadRequest_init(&rr);
  122. UA_ReadValueId rvid;
  123. UA_ReadValueId_init(&rvid);
  124. rvid.attributeId = UA_ATTRIBUTEID_VALUE;
  125. rvid.nodeId = UA_NODEID_NUMERIC(0,
  126. UA_NS0ID_SERVER_SERVERSTATUS_CURRENTTIME);
  127. rr.nodesToRead = &rvid;
  128. rr.nodesToReadSize = 1;
  129. retval = __UA_Client_AsyncServiceEx(client, &rr,
  130. &UA_TYPES[UA_TYPES_READREQUEST],
  131. (UA_ClientAsyncServiceCallback) asyncReadCallback,
  132. &UA_TYPES[UA_TYPES_READRESPONSE], &asyncCounter, NULL, 999);
  133. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  134. /* Process async responses during 1s */
  135. retval = UA_Client_run_iterate(client, 999 + 1);
  136. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  137. ck_assert_uint_eq(asyncCounter, 1);
  138. /* Simulate network cable unplugged (no response from server) */
  139. UA_Client_recvTesting_result = UA_STATUSCODE_GOODNONCRITICALTIMEOUT;
  140. retval = __UA_Client_AsyncServiceEx(client, &rr,
  141. &UA_TYPES[UA_TYPES_READREQUEST],
  142. (UA_ClientAsyncServiceCallback) asyncReadCallback,
  143. &UA_TYPES[UA_TYPES_READRESPONSE], &asyncCounter, NULL, 100);
  144. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  145. /* Process async responses during 1s */
  146. UA_Client_run_iterate(client, 100 + 1);
  147. ck_assert_uint_eq(asyncCounter, 9999);
  148. UA_Client_disconnect(client);
  149. UA_Client_delete(client);
  150. }END_TEST
  151. static UA_Boolean inactivityCallbackTriggered = false;
  152. static void inactivityCallback(UA_Client *client) {
  153. inactivityCallbackTriggered = true;
  154. }
  155. START_TEST(Client_connectivity_check) {
  156. UA_Client *client = UA_Client_new();
  157. UA_ClientConfig *clientConfig = UA_Client_getConfig(client);
  158. UA_ClientConfig_setDefault(clientConfig);
  159. clientConfig->outStandingPublishRequests = 0;
  160. clientConfig->inactivityCallback = inactivityCallback;
  161. clientConfig->connectivityCheckInterval = 1000;
  162. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  163. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  164. UA_Client_recv = client->connection.recv;
  165. client->connection.recv = UA_Client_recvTesting;
  166. inactivityCallbackTriggered = false;
  167. retval = UA_Client_run_iterate(client, 1000 + 1);
  168. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  169. ck_assert_uint_eq(inactivityCallbackTriggered, false);
  170. /* Simulate network cable unplugged (no response from server) */
  171. UA_Client_recvTesting_result = UA_STATUSCODE_GOODNONCRITICALTIMEOUT;
  172. retval = UA_Client_run_iterate(client,
  173. (UA_UInt16) (1000 + 1 + clientConfig->timeout));
  174. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  175. ck_assert_uint_eq(inactivityCallbackTriggered, true);
  176. UA_Client_disconnect(client);
  177. UA_Client_delete(client);
  178. }END_TEST
  179. static Suite* testSuite_Client(void) {
  180. Suite *s = suite_create("Client");
  181. TCase *tc_client = tcase_create("Client Basic");
  182. tcase_add_checked_fixture(tc_client, setup, teardown);
  183. tcase_add_test(tc_client, Client_read_async);
  184. tcase_add_test(tc_client, Client_read_async_timed);
  185. tcase_add_test(tc_client, Client_connectivity_check);
  186. tcase_add_test(tc_client, Client_highlevel_async_readValue);
  187. suite_add_tcase(s, tc_client);
  188. return s;
  189. }
  190. int main(void) {
  191. Suite *s = testSuite_Client();
  192. SRunner *sr = srunner_create(s);
  193. srunner_set_fork_status(sr, CK_NOFORK);
  194. srunner_run_all(sr, CK_NORMAL);
  195. int number_failed = srunner_ntests_failed(sr);
  196. srunner_free(sr);
  197. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  198. }