check_mt_writeValueAttribute.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 "thread_wrapper.h"
  10. #include "mt_testing.h"
  11. #define NUMBER_OF_WORKERS 10
  12. #define ITERATIONS_PER_WORKER 10
  13. #define NUMBER_OF_CLIENTS 10
  14. #define ITERATIONS_PER_CLIENT 10
  15. UA_NodeId pumpTypeId = {1, UA_NODEIDTYPE_NUMERIC, {1001}};
  16. static
  17. void addVariableNode(void) {
  18. /* Add a variable node to the address space */
  19. UA_VariableAttributes attr = UA_VariableAttributes_default;
  20. UA_Int32 myInteger = 42;
  21. UA_Variant_setScalar(&attr.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  22. attr.description = UA_LOCALIZEDTEXT("en-US","Temperature");
  23. attr.displayName = UA_LOCALIZEDTEXT("en-US","Temperature");
  24. attr.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
  25. UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "Temperature");
  26. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  27. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  28. UA_StatusCode res =
  29. UA_Server_addVariableNode(tc.server, pumpTypeId, parentNodeId,
  30. parentReferenceNodeId, myIntegerName,
  31. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  32. attr, NULL, NULL);
  33. ck_assert_int_eq(UA_STATUSCODE_GOOD, res);
  34. }
  35. static void setup(void) {
  36. tc.running = true;
  37. tc.server = UA_Server_new();
  38. UA_ServerConfig_setDefault(UA_Server_getConfig(tc.server));
  39. addVariableNode();
  40. UA_Server_run_startup(tc.server);
  41. THREAD_CREATE(server_thread, serverloop);
  42. }
  43. static void checkServer(void) {
  44. UA_Variant var;
  45. UA_Variant_init(&var);
  46. UA_StatusCode ret = UA_Server_readValue(tc.server, pumpTypeId, &var);
  47. ck_assert_int_eq(42, *(UA_Int32 *)var.data);
  48. ck_assert_int_eq(UA_STATUSCODE_GOOD, ret);
  49. UA_Variant_deleteMembers(&var);
  50. }
  51. static
  52. void server_writeValueAttribute(void *value) {
  53. UA_WriteValue wValue;
  54. UA_WriteValue_init(&wValue);
  55. UA_Int32 testValue = 42;
  56. UA_Variant_setScalar(&wValue.value.value, &testValue, &UA_TYPES[UA_TYPES_INT32]);
  57. wValue.nodeId = pumpTypeId;
  58. wValue.attributeId = UA_ATTRIBUTEID_VALUE;
  59. wValue.value.hasValue = true;
  60. UA_StatusCode retval = UA_Server_write(tc.server, &wValue);
  61. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  62. }
  63. static
  64. void client_writeValueAttribute(void *value) {
  65. ThreadContext tmp = (*(ThreadContext *) value);
  66. UA_Variant val;
  67. UA_Int32 testValue = 42;
  68. UA_Variant_setScalar(&val, &testValue, &UA_TYPES[UA_TYPES_INT32]);
  69. UA_StatusCode retval = UA_Client_writeValueAttribute(tc.clients[tmp.index], pumpTypeId, &val);
  70. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  71. }
  72. static
  73. void initTest(void) {
  74. tc.numberOfWorkers = NUMBER_OF_WORKERS;
  75. tc.numberofClients = NUMBER_OF_CLIENTS;
  76. tc.checkServerNodes = checkServer;
  77. tc.workerContext = (ThreadContext*) UA_calloc(tc.numberOfWorkers, sizeof(ThreadContext));
  78. tc.clients = (UA_Client**) UA_calloc(tc.numberofClients, sizeof(UA_Client*));
  79. tc.clientContext = (ThreadContext*) UA_calloc(tc.numberofClients, sizeof(ThreadContext));
  80. for (size_t i = 0; i < tc.numberOfWorkers; i++) {
  81. tc.workerContext[i].index = i;
  82. tc.workerContext[i].upperBound = ITERATIONS_PER_WORKER;
  83. tc.workerContext[i].func = server_writeValueAttribute;
  84. }
  85. for (size_t i = 0; i < tc.numberofClients; i++) {
  86. tc.clientContext[i].index = i;
  87. tc.clientContext[i].upperBound = ITERATIONS_PER_CLIENT;
  88. tc.clientContext[i].func = client_writeValueAttribute;
  89. }
  90. }
  91. START_TEST(writeValueAttribute) {
  92. startMultithreading();
  93. }
  94. END_TEST
  95. static Suite* testSuite_immutableNodes(void) {
  96. Suite *s = suite_create("Multithreading");
  97. TCase *valueCallback = tcase_create("Write value attribute");
  98. initTest();
  99. tcase_add_checked_fixture(valueCallback, setup, teardown);
  100. tcase_add_test(valueCallback, writeValueAttribute);
  101. suite_add_tcase(s,valueCallback);
  102. return s;
  103. }
  104. int main(void) {
  105. Suite *s = testSuite_immutableNodes();
  106. SRunner *sr = srunner_create(s);
  107. srunner_set_fork_status(sr, CK_NOFORK);
  108. srunner_run_all(sr, CK_NORMAL);
  109. int number_failed = srunner_ntests_failed(sr);
  110. srunner_free(sr);
  111. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  112. }