check_mt_addVariableNode.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 "mt_testing.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. UA_Server_run_startup(tc.server);
  19. THREAD_CREATE(server_thread, serverloop);
  20. }
  21. static
  22. void checkServer(void) {
  23. for (size_t i = 0; i < NUMBER_OF_WORKERS * ITERATIONS_PER_WORKER; i++) {
  24. UA_ReadValueId rvi;
  25. UA_ReadValueId_init(&rvi);
  26. char string_buf[20];
  27. snprintf(string_buf, sizeof(string_buf), "Server %zu", i);
  28. rvi.nodeId = UA_NODEID_STRING(1, string_buf);
  29. rvi.attributeId = UA_ATTRIBUTEID_VALUE;
  30. UA_DataValue resp = UA_Server_read(tc.server, &rvi, UA_TIMESTAMPSTORETURN_NEITHER);
  31. ck_assert_int_eq(resp.status, UA_STATUSCODE_GOOD);
  32. ck_assert_int_eq(0, resp.value.arrayLength);
  33. ck_assert(&UA_TYPES[UA_TYPES_INT32] == resp.value.type);
  34. ck_assert_int_eq(42, *(UA_Int32* )resp.value.data);
  35. UA_DataValue_deleteMembers(&resp);
  36. }
  37. for (size_t i = 0; i < NUMBER_OF_CLIENTS * ITERATIONS_PER_CLIENT; i++) {
  38. UA_ReadValueId rvi;
  39. UA_ReadValueId_init(&rvi);
  40. char string_buf[20];
  41. snprintf(string_buf, sizeof(string_buf), "Client %zu", i);
  42. rvi.nodeId = UA_NODEID_STRING(1, string_buf);
  43. rvi.attributeId = UA_ATTRIBUTEID_VALUE;
  44. UA_DataValue resp = UA_Server_read(tc.server, &rvi, UA_TIMESTAMPSTORETURN_NEITHER);
  45. ck_assert_int_eq(resp.status, UA_STATUSCODE_GOOD);
  46. ck_assert_int_eq(2, resp.value.arrayLength);
  47. ck_assert(&UA_TYPES[UA_TYPES_INT32] == resp.value.type);
  48. ck_assert_int_eq(10, *(UA_Int32 *)resp.value.data);
  49. ck_assert_int_eq(20, *((UA_Int32 *)resp.value.data + 1));
  50. UA_DataValue_deleteMembers(&resp);
  51. }
  52. }
  53. static
  54. void server_addVariable(void* value) {
  55. ThreadContext tmp = (*(ThreadContext *) value);
  56. size_t offset = tmp.index * tmp.upperBound;
  57. size_t number = offset + tmp.counter;
  58. char string_buf[20];
  59. snprintf(string_buf, sizeof(string_buf), "Server %zu", number);
  60. UA_VariableAttributes attr = UA_VariableAttributes_default;
  61. UA_Int32 myInteger = 42;
  62. UA_Variant_setScalar(&attr.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  63. attr.description = UA_LOCALIZEDTEXT("en-US",string_buf);
  64. attr.displayName = UA_LOCALIZEDTEXT("en-US",string_buf);
  65. attr.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
  66. attr.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
  67. UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, string_buf);
  68. UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, string_buf);
  69. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  70. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  71. UA_StatusCode ret = UA_Server_addVariableNode(tc.server, myIntegerNodeId, parentNodeId,
  72. parentReferenceNodeId, myIntegerName,
  73. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE), attr, NULL, NULL);
  74. ck_assert_uint_eq(ret, UA_STATUSCODE_GOOD);
  75. }
  76. static
  77. void client_addVariable(void* value){
  78. ThreadContext tmp = (*(ThreadContext *) value);
  79. size_t offset = tmp.index * tmp.upperBound;
  80. size_t number = offset + tmp.counter;
  81. char string_buf[20];
  82. snprintf(string_buf, sizeof(string_buf), "Client %zu", number);
  83. UA_VariableAttributes attr = UA_VariableAttributes_default;
  84. attr.description = UA_LOCALIZEDTEXT("en-US", string_buf);
  85. attr.displayName = UA_LOCALIZEDTEXT("en-US", string_buf);
  86. UA_NodeId nodeId = UA_NODEID_STRING(1, string_buf);
  87. UA_QualifiedName integerName = UA_QUALIFIEDNAME(1, string_buf);
  88. UA_Int32 values[2] = {10, 20};
  89. UA_Variant_setArray(&attr.value, values, 2, &UA_TYPES[UA_TYPES_INT32]);
  90. attr.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
  91. attr.valueRank = UA_VALUERANK_ONE_DIMENSION;
  92. UA_UInt32 arrayDims[1] = {2};
  93. attr.arrayDimensions = arrayDims;
  94. attr.arrayDimensionsSize = 1;
  95. UA_StatusCode retval = UA_Client_addVariableNode(tc.clients[tmp.index], nodeId,
  96. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  97. UA_NODEID_NUMERIC(0, UA_NS0ID_HASPROPERTY),
  98. integerName,
  99. UA_NODEID_NULL, attr, NULL);
  100. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  101. }
  102. static
  103. void initTest(void) {
  104. initThreadContext(NUMBER_OF_WORKERS, NUMBER_OF_CLIENTS, checkServer);
  105. for (size_t i = 0; i < tc.numberOfWorkers; i++) {
  106. setThreadContext(&tc.workerContext[i], i, ITERATIONS_PER_WORKER, server_addVariable);
  107. }
  108. for (size_t i = 0; i < tc.numberofClients; i++) {
  109. setThreadContext(&tc.clientContext[i], i, ITERATIONS_PER_CLIENT, client_addVariable);
  110. }
  111. }
  112. START_TEST(addVariableNodes) {
  113. startMultithreading();
  114. }
  115. END_TEST
  116. static Suite* testSuite_immutableNodes(void) {
  117. Suite *s = suite_create("Multithreading");
  118. TCase *valueCallback = tcase_create("Add variable nodes");
  119. initTest();
  120. tcase_add_checked_fixture(valueCallback, setup, teardown);
  121. tcase_add_test(valueCallback, addVariableNodes);
  122. suite_add_tcase(s,valueCallback);
  123. return s;
  124. }
  125. int main(void) {
  126. Suite *s = testSuite_immutableNodes();
  127. SRunner *sr = srunner_create(s);
  128. srunner_set_fork_status(sr, CK_NOFORK);
  129. srunner_run_all(sr, CK_NORMAL);
  130. int number_failed = srunner_ntests_failed(sr);
  131. srunner_free(sr);
  132. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  133. }