check_client_async.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. } END_TEST
  81. START_TEST(Client_read_async) {
  82. UA_Client *client = UA_Client_new();
  83. UA_ClientConfig *clientConfig = UA_Client_getConfig(client);
  84. UA_ClientConfig_setDefault(clientConfig);
  85. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  86. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  87. UA_UInt16 asyncCounter = 0;
  88. UA_ReadRequest rr;
  89. UA_ReadRequest_init(&rr);
  90. UA_ReadValueId rvid;
  91. UA_ReadValueId_init(&rvid);
  92. rvid.attributeId = UA_ATTRIBUTEID_VALUE;
  93. rvid.nodeId = UA_NODEID_NUMERIC(0,
  94. UA_NS0ID_SERVER_SERVERSTATUS_CURRENTTIME);
  95. rr.nodesToRead = &rvid;
  96. rr.nodesToReadSize = 1;
  97. /* Send 100 requests */
  98. for (size_t i = 0; i < 100; i++) {
  99. retval = __UA_Client_AsyncService(client, &rr,
  100. &UA_TYPES[UA_TYPES_READREQUEST],
  101. (UA_ClientAsyncServiceCallback) asyncReadCallback,
  102. &UA_TYPES[UA_TYPES_READRESPONSE], &asyncCounter, NULL);
  103. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  104. }
  105. /* Process async responses during 1s */
  106. retval = UA_Client_run_iterate(client, 999);
  107. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  108. ck_assert_uint_eq(asyncCounter, 100);
  109. UA_Client_disconnect(client);
  110. UA_Client_delete(client);
  111. }END_TEST
  112. START_TEST(Client_read_async_timed) {
  113. UA_Client *client = UA_Client_new();
  114. UA_ClientConfig *clientConfig = UA_Client_getConfig(client);
  115. UA_ClientConfig_setDefault(clientConfig);
  116. clientConfig->outStandingPublishRequests = 0;
  117. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  118. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  119. UA_Client_recv = client->connection.recv;
  120. client->connection.recv = UA_Client_recvTesting;
  121. UA_UInt16 asyncCounter = 0;
  122. UA_ReadRequest rr;
  123. UA_ReadRequest_init(&rr);
  124. UA_ReadValueId rvid;
  125. UA_ReadValueId_init(&rvid);
  126. rvid.attributeId = UA_ATTRIBUTEID_VALUE;
  127. rvid.nodeId = UA_NODEID_NUMERIC(0,
  128. UA_NS0ID_SERVER_SERVERSTATUS_CURRENTTIME);
  129. rr.nodesToRead = &rvid;
  130. rr.nodesToReadSize = 1;
  131. retval = __UA_Client_AsyncServiceEx(client, &rr,
  132. &UA_TYPES[UA_TYPES_READREQUEST],
  133. (UA_ClientAsyncServiceCallback) asyncReadCallback,
  134. &UA_TYPES[UA_TYPES_READRESPONSE], &asyncCounter, NULL, 999);
  135. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  136. /* Process async responses during 1s */
  137. retval = UA_Client_run_iterate(client, 999 + 1);
  138. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  139. ck_assert_uint_eq(asyncCounter, 1);
  140. /* Simulate network cable unplugged (no response from server) */
  141. UA_Client_recvTesting_result = UA_STATUSCODE_GOODNONCRITICALTIMEOUT;
  142. retval = __UA_Client_AsyncServiceEx(client, &rr,
  143. &UA_TYPES[UA_TYPES_READREQUEST],
  144. (UA_ClientAsyncServiceCallback) asyncReadCallback,
  145. &UA_TYPES[UA_TYPES_READRESPONSE], &asyncCounter, NULL, 100);
  146. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  147. /* Process async responses during 1s */
  148. UA_Client_run_iterate(client, 100 + 1);
  149. ck_assert_uint_eq(asyncCounter, 9999);
  150. UA_Client_disconnect(client);
  151. UA_Client_delete(client);
  152. }END_TEST
  153. static UA_Boolean inactivityCallbackTriggered = false;
  154. static void inactivityCallback(UA_Client *client) {
  155. inactivityCallbackTriggered = true;
  156. }
  157. START_TEST(Client_connectivity_check) {
  158. UA_Client *client = UA_Client_new();
  159. UA_ClientConfig *clientConfig = UA_Client_getConfig(client);
  160. UA_ClientConfig_setDefault(clientConfig);
  161. clientConfig->outStandingPublishRequests = 0;
  162. clientConfig->inactivityCallback = inactivityCallback;
  163. clientConfig->connectivityCheckInterval = 1000;
  164. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  165. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  166. UA_Client_recv = client->connection.recv;
  167. client->connection.recv = UA_Client_recvTesting;
  168. inactivityCallbackTriggered = false;
  169. retval = UA_Client_run_iterate(client, 1000 + 1);
  170. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  171. ck_assert_uint_eq(inactivityCallbackTriggered, false);
  172. /* Simulate network cable unplugged (no response from server) */
  173. UA_Client_recvTesting_result = UA_STATUSCODE_GOODNONCRITICALTIMEOUT;
  174. retval = UA_Client_run_iterate(client,
  175. (UA_UInt16) (1000 + 1 + clientConfig->timeout));
  176. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  177. ck_assert_uint_eq(inactivityCallbackTriggered, true);
  178. UA_Client_disconnect(client);
  179. UA_Client_delete(client);
  180. }END_TEST
  181. static Suite* testSuite_Client(void) {
  182. Suite *s = suite_create("Client");
  183. TCase *tc_client = tcase_create("Client Basic");
  184. tcase_add_checked_fixture(tc_client, setup, teardown);
  185. tcase_add_test(tc_client, Client_read_async);
  186. tcase_add_test(tc_client, Client_read_async_timed);
  187. tcase_add_test(tc_client, Client_connectivity_check);
  188. tcase_add_test(tc_client, Client_highlevel_async_readValue);
  189. suite_add_tcase(s, tc_client);
  190. return s;
  191. }
  192. int main(void) {
  193. Suite *s = testSuite_Client();
  194. SRunner *sr = srunner_create(s);
  195. srunner_set_fork_status(sr, CK_NOFORK);
  196. srunner_run_all(sr, CK_NORMAL);
  197. int number_failed = srunner_ntests_failed(sr);
  198. srunner_free(sr);
  199. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  200. }