check_mt_readWriteDelete.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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/server_config_default.h>
  5. #include <open62541/plugin/log_stdout.h>
  6. #include <open62541/client_config_default.h>
  7. #include <open62541/client_highlevel.h>
  8. #include <check.h>
  9. #include <testing_clock.h>
  10. #include "thread_wrapper.h"
  11. #include "mt_testing.h"
  12. #define NUMBER_OF_READ_WORKERS 10
  13. #define NUMBER_OF_WRITE_WORKERS 10
  14. #define ITERATIONS_PER_WORKER 100
  15. #define NUMBER_OF_READ_CLIENTS 10
  16. #define NUMBER_OF_WRITE_CLIENTS 10
  17. #define ITERATIONS_PER_CLIENT 100
  18. UA_NodeId pumpTypeId = {1, UA_NODEIDTYPE_NUMERIC, {1001}};
  19. static
  20. void AddVariableNode(void) {
  21. /* Add a variable node to the address space */
  22. UA_VariableAttributes attr = UA_VariableAttributes_default;
  23. UA_Int32 myInteger = 42;
  24. UA_Variant_setScalar(&attr.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  25. attr.description = UA_LOCALIZEDTEXT("en-US","Temperature");
  26. attr.displayName = UA_LOCALIZEDTEXT("en-US","Temperature");
  27. attr.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
  28. UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "Temperature");
  29. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  30. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  31. UA_StatusCode res =
  32. UA_Server_addVariableNode(tc.server, pumpTypeId, parentNodeId,
  33. parentReferenceNodeId, myIntegerName,
  34. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  35. attr, NULL, NULL);
  36. ck_assert_int_eq(UA_STATUSCODE_GOOD, res);
  37. }
  38. static void setup(void) {
  39. tc.running = true;
  40. tc.server = UA_Server_new();
  41. UA_ServerConfig_setDefault(UA_Server_getConfig(tc.server));
  42. AddVariableNode();
  43. UA_Server_run_startup(tc.server);
  44. THREAD_CREATE(server_thread, serverloop);
  45. }
  46. static
  47. void server_deleteValue(void *value) {
  48. UA_StatusCode ret = UA_Server_deleteNode(tc.server, pumpTypeId, true);
  49. ck_assert_int_eq(UA_STATUSCODE_GOOD, ret);
  50. }
  51. static
  52. void server_readValue(void *value) {
  53. UA_ReadValueId rvi;
  54. UA_ReadValueId_init(&rvi);
  55. rvi.nodeId = pumpTypeId;
  56. rvi.attributeId = UA_ATTRIBUTEID_VALUE;
  57. UA_Variant var;
  58. UA_Variant_init(&var);
  59. UA_StatusCode retval = UA_Server_readValue(tc.server, rvi.nodeId, &var);
  60. if (retval == UA_STATUSCODE_GOOD) {
  61. ck_assert_int_eq(42, *(UA_Int32 *)var.data);
  62. UA_Variant_deleteMembers(&var);
  63. }
  64. else {
  65. ck_assert_int_eq(retval, UA_STATUSCODE_BADNODEIDUNKNOWN);
  66. }
  67. }
  68. static
  69. void server_writeValue(void *value) {
  70. UA_WriteValue wValue;
  71. UA_WriteValue_init(&wValue);
  72. UA_Int32 testValue = 42;
  73. UA_Variant_setScalar(&wValue.value.value, &testValue, &UA_TYPES[UA_TYPES_INT32]);
  74. wValue.nodeId = pumpTypeId;
  75. wValue.attributeId = UA_ATTRIBUTEID_VALUE;
  76. wValue.value.hasValue = true;
  77. UA_StatusCode retval = UA_Server_write(tc.server, &wValue);
  78. ck_assert(retval == UA_STATUSCODE_BADNODEIDUNKNOWN || retval == UA_STATUSCODE_GOOD);
  79. }
  80. static
  81. void client_writeValue(void *value) {
  82. ThreadContext tmp = (*(ThreadContext *) value);
  83. UA_Variant val;
  84. UA_Int32 testValue = 42;
  85. UA_Variant_setScalar(&val, &testValue, &UA_TYPES[UA_TYPES_INT32]);
  86. UA_StatusCode retval = UA_Client_writeValueAttribute(tc.clients[tmp.index], pumpTypeId, &val);
  87. ck_assert(retval == UA_STATUSCODE_BADNODEIDUNKNOWN || retval == UA_STATUSCODE_GOOD);
  88. }
  89. static
  90. void client_readValue(void *value) {
  91. ThreadContext tmp = (*(ThreadContext *) value);
  92. UA_Variant val;
  93. UA_NodeId nodeId = pumpTypeId;
  94. UA_StatusCode retval = UA_Client_readValueAttribute(tc.clients[tmp.index], nodeId, &val);
  95. if (retval == UA_STATUSCODE_GOOD) {
  96. ck_assert_int_eq(42, *(UA_Int32 *)val.data);
  97. UA_Variant_deleteMembers(&val);
  98. }
  99. else {
  100. ck_assert_int_eq(retval, UA_STATUSCODE_BADNODEIDUNKNOWN);
  101. }
  102. }
  103. static
  104. void initTest(void) {
  105. initThreadContext(NUMBER_OF_READ_WORKERS + NUMBER_OF_WRITE_WORKERS + 1, NUMBER_OF_READ_CLIENTS + NUMBER_OF_WRITE_CLIENTS, NULL);
  106. size_t i = 0;
  107. for (; i < NUMBER_OF_READ_WORKERS; i++) {
  108. setThreadContext(&tc.workerContext[i], i, ITERATIONS_PER_WORKER, server_readValue);
  109. }
  110. for (; i < NUMBER_OF_READ_WORKERS + NUMBER_OF_WRITE_WORKERS; i++) {
  111. setThreadContext(&tc.workerContext[i], i, ITERATIONS_PER_WORKER, server_writeValue);
  112. }
  113. //Thread deleting the variable
  114. setThreadContext(&tc.workerContext[tc.numberOfWorkers - 1], i, 1, server_deleteValue);
  115. i = 0;
  116. for (; i < NUMBER_OF_READ_CLIENTS; i++) {
  117. setThreadContext(&tc.clientContext[i], i, ITERATIONS_PER_CLIENT, client_readValue);
  118. }
  119. for (; i < NUMBER_OF_READ_CLIENTS + NUMBER_OF_WRITE_CLIENTS; i++) {
  120. setThreadContext(&tc.clientContext[i], i, ITERATIONS_PER_CLIENT, client_writeValue);
  121. }
  122. }
  123. START_TEST(readWriteDelete) {
  124. startMultithreading();
  125. }
  126. END_TEST
  127. static Suite* testSuite_immutableNodes(void) {
  128. Suite *s = suite_create("Multithreading");
  129. TCase *valueCallback = tcase_create("Read-Write-Delete attribute");
  130. initTest();
  131. tcase_add_checked_fixture(valueCallback, setup, teardown);
  132. tcase_add_test(valueCallback, readWriteDelete);
  133. suite_add_tcase(s,valueCallback);
  134. return s;
  135. }
  136. int main(void) {
  137. Suite *s = testSuite_immutableNodes();
  138. SRunner *sr = srunner_create(s);
  139. srunner_set_fork_status(sr, CK_NOFORK);
  140. srunner_run_all(sr, CK_NORMAL);
  141. int number_failed = srunner_ntests_failed(sr);
  142. srunner_free(sr);
  143. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  144. }