check_nodestore.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 <stdio.h>
  5. #include <stdlib.h>
  6. #include <time.h>
  7. #include "ua_types.h"
  8. #include "ua_plugin_nodestore.h"
  9. #include "ua_nodestore_default.h"
  10. #include "ua_util.h"
  11. #include "check.h"
  12. /* container_of */
  13. #define container_of(ptr, type, member) \
  14. (type *)((uintptr_t)ptr - offsetof(type,member))
  15. #ifdef UA_ENABLE_MULTITHREADING
  16. #include <pthread.h>
  17. #endif
  18. /* Dirty redifinition from ua_nodestore_default.c to check that all nodes were
  19. * released */
  20. typedef struct UA_NodeMapEntry {
  21. struct UA_NodeMapEntry *orig; /* the version this is a copy from (or NULL) */
  22. UA_UInt16 refCount; /* How many consumers have a reference to the node? */
  23. UA_Boolean deleted; /* Node was marked as deleted and can be deleted when refCount == 0 */
  24. UA_Node node;
  25. } UA_NodeMapEntry;
  26. static void checkAllReleased(void *context, const UA_Node* node) {
  27. UA_NodeMapEntry *entry = container_of(node, UA_NodeMapEntry, node);
  28. ck_assert_int_eq(entry->refCount, 1); /* The count is increased when the visited node is checked out */
  29. }
  30. UA_Nodestore ns;
  31. static void setup(void) {
  32. UA_Nodestore_default_new(&ns);
  33. }
  34. static void teardown(void) {
  35. ns.iterate(ns.context, NULL, checkAllReleased);
  36. ns.deleteNodestore(ns.context);
  37. }
  38. static int zeroCnt = 0;
  39. static int visitCnt = 0;
  40. static void checkZeroVisitor(void *context, const UA_Node* node) {
  41. visitCnt++;
  42. if (node == NULL) zeroCnt++;
  43. }
  44. static UA_Node* createNode(UA_Int16 nsid, UA_Int32 id) {
  45. UA_Node *p = ns.newNode(ns.context, UA_NODECLASS_VARIABLE);
  46. p->nodeId.identifierType = UA_NODEIDTYPE_NUMERIC;
  47. p->nodeId.namespaceIndex = nsid;
  48. p->nodeId.identifier.numeric = id;
  49. p->nodeClass = UA_NODECLASS_VARIABLE;
  50. return p;
  51. }
  52. START_TEST(replaceExistingNode) {
  53. UA_Node* n1 = createNode(0,2253);
  54. ns.insertNode(ns.context, n1, NULL);
  55. UA_NodeId in1 = UA_NODEID_NUMERIC(0, 2253);
  56. UA_Node* n2;
  57. ns.getNodeCopy(ns.context, &in1, &n2);
  58. UA_StatusCode retval = ns.replaceNode(ns.context, n2);
  59. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  60. }
  61. END_TEST
  62. START_TEST(replaceOldNode) {
  63. UA_Node* n1 = createNode(0,2253);
  64. ns.insertNode(ns.context, n1, NULL);
  65. UA_NodeId in1 = UA_NODEID_NUMERIC(0,2253);
  66. UA_Node* n2;
  67. UA_Node* n3;
  68. ns.getNodeCopy(ns.context, &in1, &n2);
  69. ns.getNodeCopy(ns.context, &in1, &n3);
  70. /* shall succeed */
  71. UA_StatusCode retval = ns.replaceNode(ns.context, n2);
  72. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  73. /* shall fail */
  74. retval = ns.replaceNode(ns.context, n3);
  75. ck_assert_int_ne(retval, UA_STATUSCODE_GOOD);
  76. }
  77. END_TEST
  78. START_TEST(findNodeInUA_NodeStoreWithSingleEntry) {
  79. UA_Node* n1 = createNode(0,2253);
  80. ns.insertNode(ns.context, n1, NULL);
  81. UA_NodeId in1 = UA_NODEID_NUMERIC(0,2253);
  82. const UA_Node* nr = ns.getNode(ns.context, &in1);
  83. ck_assert_int_eq((uintptr_t)n1, (uintptr_t)nr);
  84. ns.releaseNode(ns.context, nr);
  85. }
  86. END_TEST
  87. START_TEST(failToFindNodeInOtherUA_NodeStore) {
  88. UA_Node* n1 = createNode(0,2255);
  89. ns.insertNode(ns.context, n1, NULL);
  90. UA_NodeId in1 = UA_NODEID_NUMERIC(1, 2255);
  91. const UA_Node* nr = ns.getNode(ns.context, &in1);
  92. ck_assert_int_eq((uintptr_t)nr, 0);
  93. }
  94. END_TEST
  95. START_TEST(findNodeInUA_NodeStoreWithSeveralEntries) {
  96. UA_Node* n1 = createNode(0,2253);
  97. ns.insertNode(ns.context, n1, NULL);
  98. UA_Node* n2 = createNode(0,2255);
  99. ns.insertNode(ns.context, n2, NULL);
  100. UA_Node* n3 = createNode(0,2257);
  101. ns.insertNode(ns.context, n3, NULL);
  102. UA_Node* n4 = createNode(0,2200);
  103. ns.insertNode(ns.context, n4, NULL);
  104. UA_Node* n5 = createNode(0,1);
  105. ns.insertNode(ns.context, n5, NULL);
  106. UA_Node* n6 = createNode(0,12);
  107. ns.insertNode(ns.context, n6, NULL);
  108. UA_NodeId in3 = UA_NODEID_NUMERIC(0, 2257);
  109. const UA_Node* nr = ns.getNode(ns.context, &in3);
  110. ck_assert_int_eq((uintptr_t)nr, (uintptr_t)n3);
  111. ns.releaseNode(ns.context, nr);
  112. }
  113. END_TEST
  114. START_TEST(iterateOverUA_NodeStoreShallNotVisitEmptyNodes) {
  115. UA_Node* n1 = createNode(0,2253);
  116. ns.insertNode(ns.context, n1, NULL);
  117. UA_Node* n2 = createNode(0,2255);
  118. ns.insertNode(ns.context, n2, NULL);
  119. UA_Node* n3 = createNode(0,2257);
  120. ns.insertNode(ns.context, n3, NULL);
  121. UA_Node* n4 = createNode(0,2200);
  122. ns.insertNode(ns.context, n4, NULL);
  123. UA_Node* n5 = createNode(0,1);
  124. ns.insertNode(ns.context, n5, NULL);
  125. UA_Node* n6 = createNode(0,12);
  126. ns.insertNode(ns.context, n6, NULL);
  127. zeroCnt = 0;
  128. visitCnt = 0;
  129. ns.iterate(ns.context, NULL, checkZeroVisitor);
  130. ck_assert_int_eq(zeroCnt, 0);
  131. ck_assert_int_eq(visitCnt, 6);
  132. }
  133. END_TEST
  134. START_TEST(findNodeInExpandedNamespace) {
  135. for(UA_UInt32 i = 0; i < 200; i++) {
  136. UA_Node* n = createNode(0,i);
  137. ns.insertNode(ns.context, n, NULL);
  138. }
  139. // when
  140. UA_Node *n2 = createNode(0,25);
  141. const UA_Node* nr = ns.getNode(ns.context, &n2->nodeId);
  142. ck_assert_int_eq(nr->nodeId.identifier.numeric, n2->nodeId.identifier.numeric);
  143. ns.releaseNode(ns.context, nr);
  144. ns.deleteNode(ns.context, n2);
  145. }
  146. END_TEST
  147. START_TEST(iterateOverExpandedNamespaceShallNotVisitEmptyNodes) {
  148. for(UA_UInt32 i = 0; i < 200; i++) {
  149. UA_Node* n = createNode(0,i+1);
  150. ns.insertNode(ns.context, n, NULL);
  151. }
  152. // when
  153. zeroCnt = 0;
  154. visitCnt = 0;
  155. ns.iterate(ns.context, NULL, checkZeroVisitor);
  156. // then
  157. ck_assert_int_eq(zeroCnt, 0);
  158. ck_assert_int_eq(visitCnt, 200);
  159. }
  160. END_TEST
  161. START_TEST(failToFindNonExistentNodeInUA_NodeStoreWithSeveralEntries) {
  162. UA_Node* n1 = createNode(0,2253);
  163. ns.insertNode(ns.context, n1, NULL);
  164. UA_Node* n2 = createNode(0,2255);
  165. ns.insertNode(ns.context, n2, NULL);
  166. UA_Node* n3 = createNode(0,2257);
  167. ns.insertNode(ns.context, n3, NULL);
  168. UA_Node* n4 = createNode(0,2200);
  169. ns.insertNode(ns.context, n4, NULL);
  170. UA_Node* n5 = createNode(0,1);
  171. ns.insertNode(ns.context, n5, NULL);
  172. UA_NodeId id = UA_NODEID_NUMERIC(0, 12);
  173. const UA_Node* nr = ns.getNode(ns.context, &id);
  174. ck_assert_int_eq((uintptr_t)nr, 0);
  175. }
  176. END_TEST
  177. /************************************/
  178. /* Performance Profiling Test Cases */
  179. /************************************/
  180. #ifdef UA_ENABLE_MULTITHREADING
  181. struct UA_NodeStoreProfileTest {
  182. UA_Int32 min_val;
  183. UA_Int32 max_val;
  184. UA_Int32 rounds;
  185. };
  186. static void *profileGetThread(void *arg) {
  187. struct UA_NodeStoreProfileTest *test = (struct UA_NodeStoreProfileTest*) arg;
  188. UA_NodeId id;
  189. UA_NodeId_init(&id);
  190. UA_Int32 max_val = test->max_val;
  191. for(UA_Int32 x = 0; x<test->rounds; x++) {
  192. for(UA_Int32 i=test->min_val; i<max_val; i++) {
  193. id.identifier.numeric = i+1;
  194. const UA_Node *n = ns.getNode(ns.context, &id);
  195. ns.releaseNode(ns.context, n);
  196. }
  197. }
  198. return NULL;
  199. }
  200. #endif
  201. #define N 1000 /* make bigger to test */
  202. START_TEST(profileGetDelete) {
  203. clock_t begin, end;
  204. begin = clock();
  205. for(UA_UInt32 i = 0; i < N; i++) {
  206. UA_Node *n = createNode(0,i+1);
  207. ns.insertNode(ns.context, n, NULL);
  208. }
  209. #ifdef UA_ENABLE_MULTITHREADING
  210. #define THREADS 4
  211. pthread_t t[THREADS];
  212. struct UA_NodeStoreProfileTest p[THREADS];
  213. for (int i = 0; i < THREADS; i++) {
  214. p[i] = (struct UA_NodeStoreProfileTest){i*(N/THREADS), (i+1)*(N/THREADS), 50};
  215. pthread_create(&t[i], NULL, profileGetThread, &p[i]);
  216. }
  217. for (int i = 0; i < THREADS; i++)
  218. pthread_join(t[i], NULL);
  219. end = clock();
  220. printf("Time for %d create/get/delete on %d threads in a namespace: %fs.\n", N, THREADS, (double)(end - begin) / CLOCKS_PER_SEC);
  221. #else
  222. UA_NodeId id = UA_NODEID_NULL;
  223. for(size_t i = 0; i < N; i++) {
  224. id.identifier.numeric = (UA_UInt32)i+1;
  225. const UA_Node *node = ns.getNode(ns.context, &id);
  226. ns.releaseNode(ns.context, node);
  227. }
  228. end = clock();
  229. printf("Time for single-threaded %d create/get/delete in a namespace: %fs.\n", N,
  230. (double)(end - begin) / CLOCKS_PER_SEC);
  231. #endif
  232. }
  233. END_TEST
  234. static Suite * namespace_suite (void) {
  235. Suite *s = suite_create ("UA_NodeStore");
  236. TCase* tc_find = tcase_create ("Find");
  237. tcase_add_checked_fixture(tc_find, setup, teardown);
  238. tcase_add_test (tc_find, findNodeInUA_NodeStoreWithSingleEntry);
  239. tcase_add_test (tc_find, findNodeInUA_NodeStoreWithSeveralEntries);
  240. tcase_add_test (tc_find, findNodeInExpandedNamespace);
  241. tcase_add_test (tc_find, failToFindNonExistentNodeInUA_NodeStoreWithSeveralEntries);
  242. tcase_add_test (tc_find, failToFindNodeInOtherUA_NodeStore);
  243. suite_add_tcase (s, tc_find);
  244. TCase *tc_replace = tcase_create("Replace");
  245. tcase_add_checked_fixture(tc_replace, setup, teardown);
  246. tcase_add_test (tc_replace, replaceExistingNode);
  247. tcase_add_test (tc_replace, replaceOldNode);
  248. suite_add_tcase (s, tc_replace);
  249. TCase* tc_iterate = tcase_create ("Iterate");
  250. tcase_add_checked_fixture(tc_iterate, setup, teardown);
  251. tcase_add_test (tc_iterate, iterateOverUA_NodeStoreShallNotVisitEmptyNodes);
  252. tcase_add_test (tc_iterate, iterateOverExpandedNamespaceShallNotVisitEmptyNodes);
  253. suite_add_tcase (s, tc_iterate);
  254. TCase* tc_profile = tcase_create ("Profile");
  255. tcase_add_checked_fixture(tc_profile, setup, teardown);
  256. tcase_add_test (tc_profile, profileGetDelete);
  257. suite_add_tcase (s, tc_profile);
  258. return s;
  259. }
  260. int main (void) {
  261. int number_failed = 0;
  262. Suite *s = namespace_suite();
  263. SRunner *sr = srunner_create(s);
  264. srunner_set_fork_status(sr,CK_NOFORK);
  265. srunner_run_all(sr, CK_NORMAL);
  266. number_failed += srunner_ntests_failed (sr);
  267. srunner_free(sr);
  268. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  269. }