check_client_async.c 8.6 KB

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