check_client_async.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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_config_default.h"
  13. #include "ua_network_tcp.h"
  14. #include "check.h"
  15. #include "testing_clock.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
  42. asyncReadCallback(UA_Client *client, void *userdata,
  43. UA_UInt32 requestId, const UA_ReadResponse *response) {
  44. UA_UInt16 *asyncCounter = (UA_UInt16*)userdata;
  45. (*asyncCounter)++;
  46. UA_fakeSleep(10);
  47. }
  48. START_TEST(Client_read_async) {
  49. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  50. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  51. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  52. UA_UInt16 asyncCounter = 0;
  53. UA_ReadRequest rr;
  54. UA_ReadRequest_init(&rr);
  55. UA_ReadValueId rvid;
  56. UA_ReadValueId_init(&rvid);
  57. rvid.attributeId = UA_ATTRIBUTEID_VALUE;
  58. rvid.nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_CURRENTTIME);
  59. rr.nodesToRead = &rvid;
  60. rr.nodesToReadSize = 1;
  61. /* Send 100 requests */
  62. for(size_t i = 0; i < 100; i++) {
  63. retval = __UA_Client_AsyncService(client, &rr, &UA_TYPES[UA_TYPES_READREQUEST],
  64. (UA_ClientAsyncServiceCallback)asyncReadCallback,
  65. &UA_TYPES[UA_TYPES_READRESPONSE], &asyncCounter, NULL);
  66. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  67. }
  68. /* Process async responses during 1s */
  69. retval = UA_Client_runAsync(client, 999);
  70. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  71. ck_assert_uint_eq(asyncCounter, 100);
  72. UA_Client_disconnect(client);
  73. UA_Client_delete(client);
  74. }
  75. END_TEST
  76. static Suite* testSuite_Client(void) {
  77. Suite *s = suite_create("Client");
  78. TCase *tc_client = tcase_create("Client Basic");
  79. tcase_add_checked_fixture(tc_client, setup, teardown);
  80. tcase_add_test(tc_client, Client_read_async);
  81. suite_add_tcase(s,tc_client);
  82. return s;
  83. }
  84. int main(void) {
  85. Suite *s = testSuite_Client();
  86. SRunner *sr = srunner_create(s);
  87. srunner_set_fork_status(sr, CK_NOFORK);
  88. srunner_run_all(sr,CK_NORMAL);
  89. int number_failed = srunner_ntests_failed(sr);
  90. srunner_free(sr);
  91. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  92. }