check_mt_addObjectNode.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 <open62541/plugin/log_stdout.h>
  5. #include <open62541/client_config_default.h>
  6. #include <open62541/client_highlevel.h>
  7. #include <check.h>
  8. #include "thread_wrapper.h"
  9. #include "deviceObjectType.h"
  10. #define NUMBER_OF_WORKERS 10
  11. #define ITERATIONS_PER_WORKER 10
  12. #define NUMBER_OF_CLIENTS 10
  13. #define ITERATIONS_PER_CLIENT 10
  14. static void setup(void) {
  15. tc.running = true;
  16. tc.server = UA_Server_new();
  17. UA_ServerConfig_setDefault(UA_Server_getConfig(tc.server));
  18. defineObjectTypes();
  19. addPumpTypeConstructor(tc.server);
  20. UA_Server_run_startup(tc.server);
  21. THREAD_CREATE(server_thread, serverloop);
  22. }
  23. static
  24. void checkServer(void) {
  25. for (size_t i = 0; i < NUMBER_OF_WORKERS * ITERATIONS_PER_WORKER; i++) {
  26. char string_buf[20];
  27. snprintf(string_buf, sizeof(string_buf), "Server %zu", i);
  28. UA_NodeId reqNodeId = UA_NODEID_STRING(1, string_buf);
  29. UA_NodeId resNodeId;
  30. UA_StatusCode ret = UA_Server_readNodeId(tc.server, reqNodeId, &resNodeId);
  31. ck_assert_int_eq(ret, UA_STATUSCODE_GOOD);
  32. ck_assert(UA_NodeId_equal(&reqNodeId, &resNodeId) == UA_TRUE);
  33. UA_NodeId_deleteMembers(&resNodeId);
  34. }
  35. for (size_t i = 0; i < NUMBER_OF_CLIENTS * ITERATIONS_PER_CLIENT; i++) {
  36. char string_buf[20];
  37. snprintf(string_buf, sizeof(string_buf), "Client %zu", i);
  38. UA_NodeId reqNodeId = UA_NODEID_STRING(1, string_buf);
  39. UA_NodeId resNodeId;
  40. UA_StatusCode ret = UA_Server_readNodeId(tc.server, reqNodeId, &resNodeId);
  41. ck_assert_int_eq(ret, UA_STATUSCODE_GOOD);
  42. ck_assert(UA_NodeId_equal(&reqNodeId, &resNodeId) == UA_TRUE);
  43. UA_NodeId_deleteMembers(&resNodeId);
  44. }
  45. }
  46. static
  47. void server_addObject(void* value) {
  48. ThreadContext tmp = (*(ThreadContext *) value);
  49. size_t offset = tmp.index * tmp.upperBound;
  50. size_t number = offset + tmp.counter;
  51. char string_buf[20];
  52. snprintf(string_buf, sizeof(string_buf), "Server %zu", number);
  53. UA_NodeId reqId = UA_NODEID_STRING(1, string_buf);
  54. UA_ObjectAttributes oAttr = UA_ObjectAttributes_default;
  55. oAttr.displayName = UA_LOCALIZEDTEXT("en-US", string_buf);
  56. UA_StatusCode res = UA_Server_addObjectNode(tc.server, reqId,
  57. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  58. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  59. UA_QUALIFIEDNAME(1, string_buf),
  60. pumpTypeId,
  61. oAttr, NULL, NULL);
  62. ck_assert_int_eq(UA_STATUSCODE_GOOD, res);
  63. }
  64. static
  65. void client_addObject(void* value){
  66. ThreadContext tmp = (*(ThreadContext *) value);
  67. size_t offset = tmp.index * tmp.upperBound;
  68. size_t number = offset + tmp.counter;
  69. char string_buf[20];
  70. snprintf(string_buf, sizeof(string_buf), "Client %zu", number);
  71. UA_NodeId reqId = UA_NODEID_STRING(1, string_buf);
  72. UA_ObjectAttributes oAttr = UA_ObjectAttributes_default;
  73. oAttr.displayName = UA_LOCALIZEDTEXT("en-US", string_buf);
  74. UA_StatusCode retval = UA_Client_addObjectNode(tc.clients[tmp.index], reqId,
  75. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  76. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  77. UA_QUALIFIEDNAME(1, string_buf), pumpTypeId, oAttr, NULL);
  78. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  79. }
  80. static
  81. void initTest(void) {
  82. initThreadContext(NUMBER_OF_WORKERS, NUMBER_OF_CLIENTS, checkServer);
  83. for (size_t i = 0; i < tc.numberOfWorkers; i++) {
  84. setThreadContext(&tc.workerContext[i], i, ITERATIONS_PER_WORKER, server_addObject);
  85. }
  86. for (size_t i = 0; i < tc.numberofClients; i++) {
  87. setThreadContext(&tc.clientContext[i], i, ITERATIONS_PER_CLIENT, client_addObject);
  88. }
  89. }
  90. START_TEST(addObjectTypeNodes) {
  91. startMultithreading();
  92. }
  93. END_TEST
  94. static Suite* testSuite_immutableNodes(void) {
  95. Suite *s = suite_create("Multithreading");
  96. TCase *valueCallback = tcase_create("Add object nodes");
  97. initTest();
  98. tcase_add_checked_fixture(valueCallback, setup, teardown);
  99. tcase_add_test(valueCallback, addObjectTypeNodes);
  100. suite_add_tcase(s,valueCallback);
  101. return s;
  102. }
  103. int main(void) {
  104. Suite *s = testSuite_immutableNodes();
  105. SRunner *sr = srunner_create(s);
  106. srunner_set_fork_status(sr, CK_NOFORK);
  107. srunner_run_all(sr, CK_NORMAL);
  108. int number_failed = srunner_ntests_failed(sr);
  109. srunner_free(sr);
  110. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  111. }