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