check_client_async.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. #include <pthread.h>
  7. #include "ua_types.h"
  8. #include "ua_server.h"
  9. #include "ua_client.h"
  10. #include "ua_config_standard.h"
  11. #include "ua_network_tcp.h"
  12. #include "check.h"
  13. #include "testing_clock.h"
  14. UA_Server *server;
  15. UA_Boolean *running;
  16. UA_ServerNetworkLayer nl;
  17. pthread_t server_thread;
  18. static void * serverloop(void *_) {
  19. while(*running)
  20. UA_Server_run_iterate(server, true);
  21. return NULL;
  22. }
  23. static void setup(void) {
  24. running = UA_Boolean_new();
  25. *running = true;
  26. UA_ServerConfig config = UA_ServerConfig_standard;
  27. nl = UA_ServerNetworkLayerTCP(UA_ConnectionConfig_standard, 16664);
  28. config.networkLayers = &nl;
  29. config.networkLayersSize = 1;
  30. server = UA_Server_new(config);
  31. UA_Server_run_startup(server);
  32. pthread_create(&server_thread, NULL, serverloop, NULL);
  33. }
  34. static void teardown(void) {
  35. *running = false;
  36. pthread_join(server_thread, NULL);
  37. UA_Server_run_shutdown(server);
  38. UA_Boolean_delete(running);
  39. UA_Server_delete(server);
  40. nl.deleteMembers(&nl);
  41. }
  42. static void
  43. asyncReadCallback(UA_Client *client, void *userdata,
  44. UA_UInt32 requestId, const UA_ReadResponse *response) {
  45. UA_UInt16 *asyncCounter = (UA_UInt16*)userdata;
  46. (*asyncCounter)++;
  47. UA_sleep(1000);
  48. }
  49. START_TEST(Client_read_async) {
  50. UA_Client *client = UA_Client_new(UA_ClientConfig_standard);
  51. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:16664");
  52. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  53. UA_UInt16 asyncCounter = 0;
  54. UA_ReadRequest rr;
  55. UA_ReadRequest_init(&rr);
  56. UA_ReadValueId rvid;
  57. UA_ReadValueId_init(&rvid);
  58. rvid.attributeId = UA_ATTRIBUTEID_VALUE;
  59. rvid.nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_CURRENTTIME);
  60. rr.nodesToRead = &rvid;
  61. rr.nodesToReadSize = 1;
  62. /* 1st request */
  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. /* 2nd request */
  68. retval = __UA_Client_AsyncService(client, &rr, &UA_TYPES[UA_TYPES_READREQUEST],
  69. (UA_ClientAsyncServiceCallback)asyncReadCallback,
  70. &UA_TYPES[UA_TYPES_READRESPONSE], &asyncCounter, NULL);
  71. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  72. /* Process async responses during 100ms */
  73. retval = UA_Client_runAsync(client, 1000);
  74. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  75. ck_assert_uint_eq(asyncCounter, 2);
  76. UA_Client_disconnect(client);
  77. UA_Client_delete(client);
  78. }
  79. END_TEST
  80. static Suite* testSuite_Client(void) {
  81. Suite *s = suite_create("Client");
  82. TCase *tc_client = tcase_create("Client Basic");
  83. tcase_add_checked_fixture(tc_client, setup, teardown);
  84. tcase_add_test(tc_client, Client_read_async);
  85. suite_add_tcase(s,tc_client);
  86. return s;
  87. }
  88. int main(void) {
  89. Suite *s = testSuite_Client();
  90. SRunner *sr = srunner_create(s);
  91. srunner_set_fork_status(sr, CK_NOFORK);
  92. srunner_run_all(sr,CK_NORMAL);
  93. int number_failed = srunner_ntests_failed(sr);
  94. srunner_free(sr);
  95. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  96. }