123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include "ua_types.h"
- #include "server/ua_namespace.h"
- #include "check.h"
- #ifdef MULTITHREADING
- #include <pthread.h>
- #include <urcu.h>
- #endif
- int zeroCnt = 0;
- int visitCnt = 0;
- void checkZeroVisitor(const UA_Node* node) {
- visitCnt++;
- if (node == UA_NULL) zeroCnt++;
- }
- void printVisitor(const UA_Node* node) {
- printf("%d\n", node->nodeId.identifier.numeric);
- }
- START_TEST(test_UA_Namespace) {
- UA_Namespace *ns = UA_NULL;
- UA_Namespace_new(&ns);
- UA_Namespace_delete(ns);
- }
- END_TEST
- UA_Int32 createNode(UA_Node** p, UA_Int16 nsid, UA_Int32 id) {
- UA_VariableNode_new((UA_VariableNode **)p);
- (*p)->nodeId.identifierType = UA_NODEIDTYPE_NUMERIC;
- (*p)->nodeId.namespaceIndex = nsid;
- (*p)->nodeId.identifier.numeric = id;
- (*p)->nodeClass = UA_NODECLASS_VARIABLE;
- return UA_SUCCESS;
- }
- START_TEST(findNodeInUA_NamespaceWithSingleEntry) {
- #ifdef MULTITHREADING
- rcu_register_thread();
- #endif
- // given
- UA_Namespace *ns;
- UA_Namespace_new(&ns);
- UA_Node* n1; createNode(&n1,0,2253);
- UA_Namespace_insert(ns, &n1, UA_NAMESPACE_INSERT_UNIQUE | UA_NAMESPACE_INSERT_GETMANAGED);
- const UA_Node* nr = UA_NULL;
- UA_Int32 retval;
- // when
- retval = UA_Namespace_get(ns,&n1->nodeId,&nr);
- // then
- ck_assert_int_eq(retval, UA_SUCCESS);
- ck_assert_ptr_eq(nr,n1);
- // finally
- UA_Namespace_releaseManagedNode(n1);
- UA_Namespace_releaseManagedNode(nr);
- UA_Namespace_delete(ns);
- #ifdef MULTITHREADING
- rcu_unregister_thread();
- #endif
- }
- END_TEST
- START_TEST(failToFindNodeInOtherUA_Namespace) {
- #ifdef MULTITHREADING
- rcu_register_thread();
- #endif
- // given
- UA_Namespace *ns = UA_NULL;
- UA_Namespace_new(&ns);
- UA_Node* n1; createNode(&n1,0,2253); UA_Namespace_insert(ns, &n1, 0);
- UA_Node* n2; createNode(&n2,0,2253); UA_Namespace_insert(ns, &n2, 0);
- const UA_Node* nr = UA_NULL;
- // when
- UA_Node* n; createNode(&n,1,2255);
- UA_Int32 retval = UA_Namespace_get(ns,&n->nodeId, &nr);
- // then
- ck_assert_int_ne(retval, UA_SUCCESS);
- // finally
- UA_Node_delete(n);
- UA_Namespace_releaseManagedNode(nr);
- UA_Namespace_delete(ns);
- #ifdef MULTITHREADING
- rcu_unregister_thread();
- #endif
- }
- END_TEST
- START_TEST(findNodeInUA_NamespaceWithSeveralEntries) {
- #ifdef MULTITHREADING
- rcu_register_thread();
- #endif
- // given
- UA_Namespace *ns;
- UA_Namespace_new(&ns);
- UA_Node* n1; createNode(&n1,0,2253); UA_Namespace_insert(ns, &n1, 0);
- UA_Node* n2; createNode(&n2,0,2255); UA_Namespace_insert(ns, &n2, 0);
- UA_Node* n3; createNode(&n3,0,2257); UA_Namespace_insert(ns, &n3, UA_NAMESPACE_INSERT_GETMANAGED);
- UA_Node* n4; createNode(&n4,0,2200); UA_Namespace_insert(ns, &n4, 0);
- UA_Node* n5; createNode(&n5,0,1); UA_Namespace_insert(ns, &n5, 0);
- UA_Node* n6; createNode(&n6,0,12); UA_Namespace_insert(ns, &n6, 0);
- const UA_Node* nr = UA_NULL;
- UA_Int32 retval;
- // when
- retval = UA_Namespace_get(ns,&(n3->nodeId),&nr);
- // then
- ck_assert_int_eq(retval, UA_SUCCESS);
- ck_assert_ptr_eq(nr,n3);
- // finally
- UA_Namespace_releaseManagedNode(n3);
- UA_Namespace_releaseManagedNode(nr);
- UA_Namespace_delete(ns);
- #ifdef MULTITHREADING
- rcu_unregister_thread();
- #endif
- }
- END_TEST
- START_TEST(iterateOverUA_NamespaceShallNotVisitEmptyNodes) {
- #ifdef MULTITHREADING
- rcu_register_thread();
- #endif
- // given
- UA_Namespace *ns;
- UA_Namespace_new(&ns);
- UA_Node* n1; createNode(&n1,0,2253); UA_Namespace_insert(ns, &n1, 0);
- UA_Node* n2; createNode(&n2,0,2255); UA_Namespace_insert(ns, &n2, 0);
- UA_Node* n3; createNode(&n3,0,2257); UA_Namespace_insert(ns, &n3, 0);
- UA_Node* n4; createNode(&n4,0,2200); UA_Namespace_insert(ns, &n4, 0);
- UA_Node* n5; createNode(&n5,0,1); UA_Namespace_insert(ns, &n5, 0);
- UA_Node* n6; createNode(&n6,0,12); UA_Namespace_insert(ns, &n6, 0);
- UA_Int32 retval;
- // when
- zeroCnt = 0;
- visitCnt = 0;
- retval = UA_Namespace_iterate(ns,checkZeroVisitor);
- // then
- ck_assert_int_eq(retval, UA_SUCCESS);
- ck_assert_int_eq(zeroCnt, 0);
- ck_assert_int_eq(visitCnt, 6);
- // finally
- UA_Namespace_delete(ns);
- #ifdef MULTITHREADING
- rcu_unregister_thread();
- #endif
- }
- END_TEST
- START_TEST(findNodeInExpandedNamespace) {
- #ifdef MULTITHREADING
- rcu_register_thread();
- #endif
- // given
- UA_Namespace *ns;
- UA_Namespace_new(&ns);
- UA_Node* n;
- UA_Int32 i=0;
- for (; i<200; i++) {
- createNode(&n,0,i); UA_Namespace_insert(ns, &n, 0);
- }
- const UA_Node* nr = UA_NULL;
- UA_Int32 retval;
- // when
- createNode(&n,0,25);
- retval = UA_Namespace_get(ns,&(n->nodeId),&nr);
- // then
- ck_assert_int_eq(retval, UA_SUCCESS);
- ck_assert_int_eq(nr->nodeId.identifier.numeric,n->nodeId.identifier.numeric);
- // finally
- UA_free((void*)n);
- UA_Namespace_releaseManagedNode(nr);
- UA_Namespace_delete(ns);
- #ifdef MULTITHREADING
- rcu_unregister_thread();
- #endif
- }
- END_TEST
- START_TEST(iterateOverExpandedNamespaceShallNotVisitEmptyNodes) {
- #ifdef MULTITHREADING
- rcu_register_thread();
- #endif
- // given
- UA_Namespace *ns;
- UA_Namespace_new(&ns);
- UA_Node* n;
- UA_Int32 i=0;
- for (; i<200; i++) {
- createNode(&n,0,i); UA_Namespace_insert(ns, &n, 0);
- }
- // when
- UA_Int32 retval;
- zeroCnt = 0;
- visitCnt = 0;
- retval = UA_Namespace_iterate(ns,checkZeroVisitor);
- // then
- ck_assert_int_eq(retval, UA_SUCCESS);
- ck_assert_int_eq(zeroCnt, 0);
- ck_assert_int_eq(visitCnt, 200);
- // finally
- UA_Namespace_delete(ns);
- #ifdef MULTITHREADING
- rcu_unregister_thread();
- #endif
- }
- END_TEST
- START_TEST(failToFindNonExistantNodeInUA_NamespaceWithSeveralEntries) {
- #ifdef MULTITHREADING
- rcu_register_thread();
- #endif
- // given
- UA_Namespace *ns;
- UA_Namespace_new(&ns);
- UA_Node* n1; createNode(&n1,0,2253); UA_Namespace_insert(ns, &n1, 0);
- UA_Node* n2; createNode(&n2,0,2255); UA_Namespace_insert(ns, &n2, 0);
- UA_Node* n3; createNode(&n3,0,2257); UA_Namespace_insert(ns, &n3, 0);
- UA_Node* n4; createNode(&n4,0,2200); UA_Namespace_insert(ns, &n4, 0);
- UA_Node* n5; createNode(&n5,0,1); UA_Namespace_insert(ns, &n5, 0);
- UA_Node* n6; createNode(&n6,0,12);
- const UA_Node* nr = UA_NULL;
- UA_Int32 retval;
- // when
- retval = UA_Namespace_get(ns, &(n6->nodeId), &nr);
- // then
- ck_assert_int_ne(retval, UA_SUCCESS);
- // finally
- UA_free((void *)n6);
- UA_Namespace_delete(ns);
- #ifdef MULTITHREADING
- rcu_unregister_thread();
- #endif
- }
- END_TEST
- /************************************/
- /* Performance Profiling Test Cases */
- /************************************/
- #ifdef MULTITHREADING
- struct UA_NamespaceProfileTest {
- UA_Namespace *ns;
- UA_Int32 min_val;
- UA_Int32 max_val;
- UA_Int32 rounds;
- };
- void *profileGetThread(void *arg) {
- rcu_register_thread();
- struct UA_NamespaceProfileTest *test = (struct UA_NamespaceProfileTest*) arg;
- UA_NodeId id = NS0NODEID(0);
- const UA_Node *cn;
- UA_Int32 max_val = test->max_val;
- UA_Namespace *ns = test->ns;
- for(UA_Int32 x = 0; x<test->rounds; x++) {
- for (UA_Int32 i=test->min_val; i<max_val; i++) {
- id.identifier.numeric = i;
- UA_Namespace_get(ns,&id, &cn);
- UA_Namespace_releaseManagedNode(cn);
- }
- }
- rcu_unregister_thread();
-
- return UA_NULL;
- }
- #endif
- START_TEST(profileGetDelete) {
- #ifdef MULTITHREADING
- rcu_register_thread();
- #endif
- #define N 1000000
- UA_Namespace *ns;
- UA_Namespace_new(&ns);
- UA_Int32 i=0;
- UA_Node *n;
- for (; i<N; i++) {
- createNode(&n,0,i); UA_Namespace_insert(ns, &n, 0);
- }
- clock_t begin, end;
- begin = clock();
- #ifdef MULTITHREADING
- #define THREADS 4
- pthread_t t[THREADS];
- struct UA_NamespaceProfileTest p[THREADS];
- for (int i = 0; i < THREADS; i++) {
- p[i] = (struct UA_NamespaceProfileTest){ns, i*(N/THREADS), (i+1)*(N/THREADS), 50};
- pthread_create(&t[i], NULL, profileGetThread, &p[i]);
- }
- for (int i = 0; i < THREADS; i++)
- pthread_join(t[i], NULL);
- end = clock();
- printf("Time for %d create/get/delete on %d threads in a namespace: %fs.\n", N, THREADS, (double)(end - begin) / CLOCKS_PER_SEC);
- #else
- const UA_Node *cn;
- UA_NodeId id = NS0NODEID(0);
- for(UA_Int32 x = 0; x<50; x++) {
- for(i=0; i<N; i++) {
- id.identifier.numeric = i;
- UA_Namespace_get(ns,&id, &cn);
- UA_Namespace_releaseManagedNode(cn);
- }
- }
- end = clock();
- printf("Time for single-threaded %d create/get/delete in a namespace: %fs.\n", N, (double)(end - begin) / CLOCKS_PER_SEC);
- #endif
- UA_Namespace_delete(ns);
- #ifdef MULTITHREADING
- rcu_unregister_thread();
- #endif
- }
- END_TEST
- Suite * namespace_suite (void) {
- Suite *s = suite_create ("UA_Namespace");
- TCase *tc_cd = tcase_create ("Create/Delete");
- tcase_add_test (tc_cd, test_UA_Namespace);
- suite_add_tcase (s, tc_cd);
- TCase* tc_find = tcase_create ("Find");
- tcase_add_test (tc_find, findNodeInUA_NamespaceWithSingleEntry);
- tcase_add_test (tc_find, findNodeInUA_NamespaceWithSeveralEntries);
- tcase_add_test (tc_find, findNodeInExpandedNamespace);
- tcase_add_test (tc_find, failToFindNonExistantNodeInUA_NamespaceWithSeveralEntries);
- tcase_add_test (tc_find, failToFindNodeInOtherUA_Namespace);
- suite_add_tcase (s, tc_find);
- TCase* tc_iterate = tcase_create ("Iterate");
- tcase_add_test (tc_iterate, iterateOverUA_NamespaceShallNotVisitEmptyNodes);
- tcase_add_test (tc_iterate, iterateOverExpandedNamespaceShallNotVisitEmptyNodes);
- suite_add_tcase (s, tc_iterate);
-
- /* TCase* tc_profile = tcase_create ("Profile"); */
- /* tcase_add_test (tc_profile, profileGetDelete); */
- /* suite_add_tcase (s, tc_profile); */
- return s;
- }
- int main (void) {
- int number_failed =0;
- Suite *s = namespace_suite ();
- SRunner *sr = srunner_create (s);
- //srunner_set_fork_status(sr,CK_NOFORK);
- srunner_run_all (sr, CK_NORMAL);
- number_failed += srunner_ntests_failed (sr);
- srunner_free (sr);
- return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
- }
|