check_client_async.c 3.1 KB

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