check_client_async.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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_ServerConfig *config;
  16. UA_Boolean running;
  17. UA_ServerNetworkLayer nl;
  18. pthread_t server_thread;
  19. static void * serverloop(void *_) {
  20. while(running)
  21. UA_Server_run_iterate(server, true);
  22. return NULL;
  23. }
  24. static void setup(void) {
  25. running = true;
  26. config = UA_ServerConfig_new_default();
  27. server = UA_Server_new(config);
  28. UA_Server_run_startup(server);
  29. pthread_create(&server_thread, NULL, serverloop, NULL);
  30. }
  31. static void teardown(void) {
  32. running = false;
  33. pthread_join(server_thread, NULL);
  34. UA_Server_run_shutdown(server);
  35. UA_Server_delete(server);
  36. UA_ServerConfig_delete(config);
  37. }
  38. static void
  39. asyncReadCallback(UA_Client *client, void *userdata,
  40. UA_UInt32 requestId, const UA_ReadResponse *response) {
  41. UA_UInt16 *asyncCounter = (UA_UInt16*)userdata;
  42. (*asyncCounter)++;
  43. UA_sleep(1000);
  44. }
  45. START_TEST(Client_read_async) {
  46. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  47. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  48. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  49. UA_UInt16 asyncCounter = 0;
  50. UA_ReadRequest rr;
  51. UA_ReadRequest_init(&rr);
  52. UA_ReadValueId rvid;
  53. UA_ReadValueId_init(&rvid);
  54. rvid.attributeId = UA_ATTRIBUTEID_VALUE;
  55. rvid.nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_CURRENTTIME);
  56. rr.nodesToRead = &rvid;
  57. rr.nodesToReadSize = 1;
  58. /* 1st request */
  59. retval = __UA_Client_AsyncService(client, &rr, &UA_TYPES[UA_TYPES_READREQUEST],
  60. (UA_ClientAsyncServiceCallback)asyncReadCallback,
  61. &UA_TYPES[UA_TYPES_READRESPONSE], &asyncCounter, NULL);
  62. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  63. /* 2nd request */
  64. retval = __UA_Client_AsyncService(client, &rr, &UA_TYPES[UA_TYPES_READREQUEST],
  65. (UA_ClientAsyncServiceCallback)asyncReadCallback,
  66. &UA_TYPES[UA_TYPES_READRESPONSE], &asyncCounter, NULL);
  67. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  68. /* Process async responses during 100ms */
  69. retval = UA_Client_runAsync(client, 1000);
  70. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  71. ck_assert_uint_eq(asyncCounter, 2);
  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. }