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