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 "ua_types.h"
  7. #include "ua_server.h"
  8. #include "ua_client.h"
  9. #include "ua_config_default.h"
  10. #include "ua_network_tcp.h"
  11. #include "check.h"
  12. #include "testing_clock.h"
  13. #include "thread_wrapper.h"
  14. UA_Server *server;
  15. UA_ServerConfig *config;
  16. UA_Boolean running;
  17. UA_ServerNetworkLayer nl;
  18. THREAD_HANDLE server_thread;
  19. THREAD_CALLBACK(serverloop) {
  20. while(running)
  21. UA_Server_run_iterate(server, true);
  22. return 0;
  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. THREAD_CREATE(server_thread, serverloop);
  30. }
  31. static void teardown(void) {
  32. running = false;
  33. THREAD_JOIN(server_thread);
  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_fakeSleep(10);
  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. /* Send 100 requests */
  59. for(size_t i = 0; i < 100; i++) {
  60. retval = __UA_Client_AsyncService(client, &rr, &UA_TYPES[UA_TYPES_READREQUEST],
  61. (UA_ClientAsyncServiceCallback)asyncReadCallback,
  62. &UA_TYPES[UA_TYPES_READRESPONSE], &asyncCounter, NULL);
  63. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  64. }
  65. /* Process async responses during 1s */
  66. retval = UA_Client_runAsync(client, 999);
  67. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  68. ck_assert_uint_eq(asyncCounter, 100);
  69. UA_Client_disconnect(client);
  70. UA_Client_delete(client);
  71. }
  72. END_TEST
  73. static Suite* testSuite_Client(void) {
  74. Suite *s = suite_create("Client");
  75. TCase *tc_client = tcase_create("Client Basic");
  76. tcase_add_checked_fixture(tc_client, setup, teardown);
  77. tcase_add_test(tc_client, Client_read_async);
  78. suite_add_tcase(s,tc_client);
  79. return s;
  80. }
  81. int main(void) {
  82. Suite *s = testSuite_Client();
  83. SRunner *sr = srunner_create(s);
  84. srunner_set_fork_status(sr, CK_NOFORK);
  85. srunner_run_all(sr,CK_NORMAL);
  86. int number_failed = srunner_ntests_failed(sr);
  87. srunner_free(sr);
  88. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  89. }