check_client_async.c 8.8 KB

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