check_mt_readValueAttribute.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. UA_NodeId pumpTypeId = {1, UA_NODEIDTYPE_NUMERIC, {1001}};
  15. static
  16. void addVariableNode(void) {
  17. UA_VariableAttributes attr = UA_VariableAttributes_default;
  18. UA_Int32 myInteger = 42;
  19. UA_Variant_setScalar(&attr.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  20. attr.description = UA_LOCALIZEDTEXT("en-US","Temperature");
  21. attr.displayName = UA_LOCALIZEDTEXT("en-US","Temperature");
  22. UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "Temperature");
  23. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  24. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  25. UA_StatusCode res =
  26. UA_Server_addVariableNode(tc.server, pumpTypeId, parentNodeId,
  27. parentReferenceNodeId, myIntegerName,
  28. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  29. attr, NULL, NULL);
  30. ck_assert_int_eq(UA_STATUSCODE_GOOD, res);
  31. }
  32. static void setup(void) {
  33. tc.running = true;
  34. tc.server = UA_Server_new();
  35. UA_ServerConfig_setDefault(UA_Server_getConfig(tc.server));
  36. addVariableNode();
  37. UA_Server_run_startup(tc.server);
  38. THREAD_CREATE(server_thread, serverloop);
  39. }
  40. static
  41. void server_readValueAttribute(void * value) {
  42. UA_ReadValueId rvi;
  43. UA_ReadValueId_init(&rvi);
  44. rvi.nodeId = pumpTypeId;
  45. rvi.attributeId = UA_ATTRIBUTEID_VALUE;
  46. // read 1
  47. UA_DataValue resp = UA_Server_read(tc.server, &rvi, UA_TIMESTAMPSTORETURN_NEITHER);
  48. ck_assert_int_eq(UA_STATUSCODE_GOOD, resp.status);
  49. ck_assert_int_eq(true, resp.hasValue);
  50. ck_assert_int_eq(0, resp.value.arrayLength);
  51. ck_assert(&UA_TYPES[UA_TYPES_INT32] == resp.value.type);
  52. ck_assert_int_eq(42, *(UA_Int32* )resp.value.data);
  53. UA_DataValue_deleteMembers(&resp);
  54. // read 2
  55. UA_Variant var;
  56. UA_Variant_init(&var);
  57. UA_StatusCode ret = UA_Server_readValue(tc.server, rvi.nodeId, &var);
  58. ck_assert_int_eq(UA_STATUSCODE_GOOD, ret);
  59. ck_assert_int_eq(42, *(UA_Int32 *)var.data);
  60. UA_Variant_deleteMembers(&var);
  61. }
  62. static
  63. void client_readValueAttribute(void * value) {
  64. ThreadContext tmp = (*(ThreadContext *) value);
  65. UA_Variant val;
  66. UA_NodeId nodeId = pumpTypeId;
  67. UA_StatusCode retval = UA_Client_readValueAttribute(tc.clients[tmp.index], nodeId, &val);
  68. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  69. ck_assert_int_eq(42, *(UA_Int32 *)val.data);
  70. UA_Variant_deleteMembers(&val);
  71. }
  72. static
  73. void initTest(void) {
  74. initThreadContext(NUMBER_OF_WORKERS, NUMBER_OF_CLIENTS, NULL);
  75. for (size_t i = 0; i < tc.numberOfWorkers; i++) {
  76. setThreadContext(&tc.workerContext[i], i, ITERATIONS_PER_WORKER, server_readValueAttribute);
  77. }
  78. for (size_t i = 0; i < tc.numberofClients; i++) {
  79. setThreadContext(&tc.clientContext[i], i, ITERATIONS_PER_CLIENT, client_readValueAttribute);
  80. }
  81. }
  82. START_TEST(readValueAttribute) {
  83. startMultithreading();
  84. }
  85. END_TEST
  86. static Suite* testSuite_immutableNodes(void) {
  87. Suite *s = suite_create("Multithreading");
  88. TCase *valueCallback = tcase_create("Read Write attribute");
  89. initTest();
  90. tcase_add_checked_fixture(valueCallback, setup, teardown);
  91. tcase_add_test(valueCallback, readValueAttribute);
  92. suite_add_tcase(s,valueCallback);
  93. return s;
  94. }
  95. int main(void) {
  96. Suite *s = testSuite_immutableNodes();
  97. SRunner *sr = srunner_create(s);
  98. srunner_set_fork_status(sr, CK_NOFORK);
  99. srunner_run_all(sr, CK_NORMAL);
  100. int number_failed = srunner_ntests_failed(sr);
  101. srunner_free(sr);
  102. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  103. }