check_mt_addDeleteObject.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 "deviceObjectType.h"
  11. #include "mt_testing.h"
  12. #define NUMBER_OF_WORKERS 10
  13. #define ITERATIONS_PER_WORKER 10
  14. #define NUMBER_OF_CLIENTS 10
  15. #define ITERATIONS_PER_CLIENT 10
  16. static void setup(void) {
  17. tc.running = true;
  18. tc.server = UA_Server_new();
  19. UA_ServerConfig_setDefault(UA_Server_getConfig(tc.server));
  20. defineObjectTypes();
  21. addPumpTypeConstructor(tc.server);
  22. UA_Server_run_startup(tc.server);
  23. THREAD_CREATE(server_thread, serverloop);
  24. }
  25. static void checkServer(void) {
  26. for (size_t i = 0; i < NUMBER_OF_WORKERS * ITERATIONS_PER_WORKER; i++) {
  27. char string_buf[20];
  28. snprintf(string_buf, sizeof(string_buf), "Server %zu", i);
  29. UA_NodeId reqNodeId = UA_NODEID_STRING(1, string_buf);
  30. UA_NodeId resNodeId;
  31. UA_StatusCode ret = UA_Server_readNodeId(tc.server, reqNodeId, &resNodeId);
  32. ck_assert_int_eq(ret, UA_STATUSCODE_BADNODEIDUNKNOWN);
  33. }
  34. }
  35. static
  36. void server_addObject(void* value) {
  37. ThreadContext tmp = (*(ThreadContext *) value);
  38. size_t offset = tmp.index * tmp.upperBound;
  39. size_t number = offset + tmp.counter;
  40. char string_buf[20];
  41. snprintf(string_buf, sizeof(string_buf), "Server %zu", number);
  42. UA_NodeId reqId = UA_NODEID_STRING(1, string_buf);
  43. UA_ObjectAttributes oAttr = UA_ObjectAttributes_default;
  44. oAttr.displayName = UA_LOCALIZEDTEXT("en-US", string_buf);
  45. //Entering the constructor of the object (callback to user space) it is possible that a thread deletes the object while initialization
  46. UA_StatusCode retval = UA_Server_addObjectNode(tc.server, reqId,
  47. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  48. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  49. UA_QUALIFIEDNAME(1, string_buf),
  50. pumpTypeId,
  51. oAttr, NULL, NULL);
  52. ck_assert(retval == UA_STATUSCODE_GOOD || retval == UA_STATUSCODE_BADNODEIDUNKNOWN);
  53. }
  54. static
  55. void server_deleteObject(void* value){
  56. ThreadContext tmp = (*(ThreadContext *) value);
  57. size_t offset = tmp.index * tmp.upperBound;
  58. size_t number = offset + tmp.counter;
  59. char string_buf[20];
  60. snprintf(string_buf, sizeof(string_buf), "Server %zu", number);
  61. UA_StatusCode ret = UA_STATUSCODE_GOOD;
  62. do {
  63. ret = UA_Server_deleteNode(tc.server, UA_NODEID_STRING(1, string_buf), true);
  64. } while (ret != UA_STATUSCODE_GOOD);
  65. }
  66. static
  67. void initTest(void) {
  68. initThreadContext(NUMBER_OF_WORKERS, NUMBER_OF_CLIENTS, checkServer);
  69. for (size_t i = 0; i < tc.numberOfWorkers; i++) {
  70. setThreadContext(&tc.workerContext[i], i, ITERATIONS_PER_WORKER, server_addObject);
  71. }
  72. for (size_t i = 0; i < tc.numberofClients; i++) {
  73. setThreadContext(&tc.clientContext[i], i, ITERATIONS_PER_CLIENT, server_deleteObject);
  74. }
  75. }
  76. START_TEST(addDeleteObjectTypeNodes) {
  77. startMultithreading();
  78. }
  79. END_TEST
  80. static Suite* testSuite_immutableNodes(void) {
  81. Suite *s = suite_create("Multithreading");
  82. TCase *valueCallback = tcase_create("Add-Delete nodes");
  83. initTest();
  84. tcase_add_checked_fixture(valueCallback, setup, teardown);
  85. tcase_add_test(valueCallback, addDeleteObjectTypeNodes);
  86. suite_add_tcase(s,valueCallback);
  87. return s;
  88. }
  89. int main(void) {
  90. Suite *s = testSuite_immutableNodes();
  91. SRunner *sr = srunner_create(s);
  92. srunner_set_fork_status(sr, CK_NOFORK);
  93. srunner_run_all(sr, CK_NORMAL);
  94. int number_failed = srunner_ntests_failed(sr);
  95. srunner_free(sr);
  96. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  97. }