check_mt_readWriteDeleteCallback.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. UA_Int32 temperature = 42;
  20. UA_LOCK_TYPE(mu)
  21. static UA_StatusCode
  22. readTemperature(UA_Server *tmpServer,
  23. const UA_NodeId *sessionId, void *sessionContext,
  24. const UA_NodeId *nodeId, void *nodeContext,
  25. UA_Boolean sourceTimeStamp, const UA_NumericRange *range,
  26. UA_DataValue *dataValue) {
  27. UA_LOCK(mu);
  28. UA_Variant_setScalarCopy(&dataValue->value, &temperature, &UA_TYPES[UA_TYPES_INT32]);
  29. UA_UNLOCK(mu);
  30. dataValue->hasValue = true;
  31. return UA_STATUSCODE_GOOD;
  32. }
  33. static UA_StatusCode
  34. writeTemperature(UA_Server *tmpServer,
  35. const UA_NodeId *sessionId, void *sessionContext,
  36. const UA_NodeId *nodeId, void *nodeContext,
  37. const UA_NumericRange *range, const UA_DataValue *data) {
  38. UA_LOCK(mu);
  39. temperature = *(UA_Int32 *) data->value.data;
  40. UA_UNLOCK(mu);
  41. return UA_STATUSCODE_GOOD;
  42. }
  43. static
  44. void AddVariableNode(void) {
  45. UA_VariableAttributes attr = UA_VariableAttributes_default;
  46. attr.displayName = UA_LOCALIZEDTEXT("en-US", "Temperature");
  47. attr.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
  48. UA_DataSource temperatureSource;
  49. temperatureSource.read = readTemperature;
  50. temperatureSource.write = writeTemperature;
  51. UA_StatusCode retval = UA_Server_addDataSourceVariableNode(tc.server, pumpTypeId, UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  52. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), UA_QUALIFIEDNAME(1, "Temperature"),
  53. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE), attr,
  54. temperatureSource, NULL, NULL);
  55. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  56. }
  57. static void setup(void) {
  58. tc.running = true;
  59. tc.server = UA_Server_new();
  60. UA_ServerConfig_setDefault(UA_Server_getConfig(tc.server));
  61. AddVariableNode();
  62. UA_Server_run_startup(tc.server);
  63. THREAD_CREATE(server_thread, serverloop);
  64. }
  65. static
  66. void server_deleteValue(void *value) {
  67. UA_StatusCode ret = UA_Server_deleteNode(tc.server, pumpTypeId, true);
  68. ck_assert_int_eq(UA_STATUSCODE_GOOD, ret);
  69. }
  70. static
  71. void server_readValue(void *value) {
  72. UA_ReadValueId rvi;
  73. UA_ReadValueId_init(&rvi);
  74. rvi.nodeId = pumpTypeId;
  75. rvi.attributeId = UA_ATTRIBUTEID_VALUE;
  76. UA_Variant var;
  77. UA_Variant_init(&var);
  78. UA_StatusCode retval = UA_Server_readValue(tc.server, rvi.nodeId, &var);
  79. if (retval == UA_STATUSCODE_GOOD) {
  80. ck_assert_int_eq(42, *(UA_Int32 *)var.data);
  81. ck_assert_int_eq(UA_STATUSCODE_GOOD, retval);
  82. UA_Variant_deleteMembers(&var);
  83. }
  84. else {
  85. ck_assert_int_eq(retval, UA_STATUSCODE_BADNODEIDUNKNOWN);
  86. }
  87. }
  88. static
  89. void server_writeValue(void *value) {
  90. UA_WriteValue wValue;
  91. UA_WriteValue_init(&wValue);
  92. UA_Int32 testValue = 42;
  93. UA_Variant_setScalar(&wValue.value.value, &testValue, &UA_TYPES[UA_TYPES_INT32]);
  94. wValue.nodeId = pumpTypeId;
  95. wValue.attributeId = UA_ATTRIBUTEID_VALUE;
  96. wValue.value.hasValue = true;
  97. UA_StatusCode retval = UA_Server_write(tc.server, &wValue);
  98. ck_assert(retval == UA_STATUSCODE_BADNODEIDUNKNOWN || retval == UA_STATUSCODE_GOOD);
  99. }
  100. static
  101. void client_writeValue(void *value) {
  102. ThreadContext tmp = (*(ThreadContext *) value);
  103. UA_Variant val;
  104. UA_Int32 testValue = 42;
  105. UA_Variant_setScalar(&val, &testValue, &UA_TYPES[UA_TYPES_INT32]);
  106. UA_StatusCode retval = UA_Client_writeValueAttribute(tc.clients[tmp.index], pumpTypeId, &val);
  107. ck_assert(retval == UA_STATUSCODE_BADNODEIDUNKNOWN || retval == UA_STATUSCODE_GOOD);
  108. }
  109. static
  110. void client_readValue(void *value) {
  111. ThreadContext tmp = (*(ThreadContext *) value);
  112. UA_Variant val;
  113. UA_NodeId nodeId = pumpTypeId;
  114. UA_StatusCode retval = UA_Client_readValueAttribute(tc.clients[tmp.index], nodeId, &val);
  115. if (retval == UA_STATUSCODE_GOOD) {
  116. ck_assert_int_eq(42, *(UA_Int32 *)val.data);
  117. UA_Variant_deleteMembers(&val);
  118. }
  119. else {
  120. ck_assert_int_eq(retval, UA_STATUSCODE_BADNODEIDUNKNOWN);
  121. }
  122. }
  123. static
  124. void initTest(void) {
  125. UA_LOCK_INIT(mu);
  126. initThreadContext(NUMBER_OF_READ_WORKERS + NUMBER_OF_WRITE_WORKERS + 1, NUMBER_OF_READ_CLIENTS + NUMBER_OF_WRITE_CLIENTS, NULL);
  127. size_t i = 0;
  128. for (; i < NUMBER_OF_READ_WORKERS; i++) {
  129. setThreadContext(&tc.workerContext[i], i, ITERATIONS_PER_WORKER, server_readValue);
  130. }
  131. for (; i < NUMBER_OF_READ_WORKERS + NUMBER_OF_WRITE_WORKERS; i++) {
  132. setThreadContext(&tc.workerContext[i], i, ITERATIONS_PER_WORKER, server_writeValue);
  133. }
  134. //Thread deleting the variable
  135. setThreadContext(&tc.workerContext[tc.numberOfWorkers - 1], i, 1, server_deleteValue);
  136. i = 0;
  137. for (; i < NUMBER_OF_READ_CLIENTS; i++) {
  138. setThreadContext(&tc.clientContext[i], i, ITERATIONS_PER_CLIENT, client_readValue);
  139. }
  140. for (; i < NUMBER_OF_READ_CLIENTS + NUMBER_OF_WRITE_CLIENTS; i++) {
  141. setThreadContext(&tc.clientContext[i], i, ITERATIONS_PER_CLIENT, client_writeValue);
  142. }
  143. }
  144. START_TEST(readWriteDeleteCallback) {
  145. startMultithreading();
  146. }
  147. END_TEST
  148. static Suite* testSuite_immutableNodes(void) {
  149. Suite *s = suite_create("Multithreading");
  150. TCase *valueCallback = tcase_create("Read-Write-Delete-Callback");
  151. initTest();
  152. tcase_add_checked_fixture(valueCallback, setup, teardown);
  153. tcase_add_test(valueCallback, readWriteDeleteCallback);
  154. suite_add_tcase(s,valueCallback);
  155. return s;
  156. }
  157. int main(void) {
  158. Suite *s = testSuite_immutableNodes();
  159. SRunner *sr = srunner_create(s);
  160. srunner_set_fork_status(sr, CK_NOFORK);
  161. srunner_run_all(sr, CK_NORMAL);
  162. int number_failed = srunner_ntests_failed(sr);
  163. srunner_free(sr);
  164. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  165. }