check_subscription_events.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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 "ua_client_subscriptions.h"
  5. #include "ua_server.h"
  6. #include "server/ua_services.h"
  7. #include "server/ua_server_internal.h"
  8. #include "server/ua_subscription.h"
  9. #include "ua_config_default.h"
  10. #include "thread_wrapper.h"
  11. #include "check.h"
  12. #include "testing_clock.h"
  13. #ifdef UA_ENABLE_SUBSCRIPTIONS_EVENTS
  14. static UA_Server *server;
  15. static UA_ServerConfig *config;
  16. static UA_Boolean running;
  17. static THREAD_HANDLE server_thread;
  18. static MUTEX_HANDLE serverMutex;
  19. UA_Client *client;
  20. static UA_UInt32 subscriptionId;
  21. static UA_UInt32 monitoredItemId;
  22. static UA_NodeId eventType;
  23. static size_t nSelectClauses = 4;
  24. static UA_Boolean notificationReceived;
  25. static UA_Boolean overflowNotificationReceived;
  26. static UA_SimpleAttributeOperand *selectClauses;
  27. UA_Double publishingInterval = 500.0;
  28. static void
  29. addNewEventType(void) {
  30. UA_ObjectTypeAttributes attr = UA_ObjectTypeAttributes_default;
  31. attr.displayName = UA_LOCALIZEDTEXT_ALLOC("en-US", "SimpleEventType");
  32. attr.description = UA_LOCALIZEDTEXT_ALLOC("en-US", "The simple event type we created");
  33. UA_Server_addObjectTypeNode(server, UA_NODEID_NULL,
  34. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEEVENTTYPE),
  35. UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  36. UA_QUALIFIEDNAME(0, "SimpleEventType"),
  37. attr, NULL, &eventType);
  38. UA_LocalizedText_deleteMembers(&attr.displayName);
  39. UA_LocalizedText_deleteMembers(&attr.description);
  40. }
  41. static void
  42. setupSelectClauses(void) {
  43. /* Check for severity (set manually), message (set manually), eventType
  44. * (automatic) and sourceNode (automatic) */
  45. selectClauses = (UA_SimpleAttributeOperand *)
  46. UA_Array_new(nSelectClauses, &UA_TYPES[UA_TYPES_SIMPLEATTRIBUTEOPERAND]);
  47. if(!selectClauses)
  48. return;
  49. for(size_t i = 0; i < nSelectClauses; ++i) {
  50. UA_SimpleAttributeOperand_init(&selectClauses[i]);
  51. selectClauses[i].typeDefinitionId = UA_NODEID_NUMERIC(0, UA_NS0ID_BASEEVENTTYPE);
  52. selectClauses[i].browsePathSize = 1;
  53. selectClauses[i].attributeId = UA_ATTRIBUTEID_VALUE;
  54. selectClauses[i].browsePath = (UA_QualifiedName *)
  55. UA_Array_new(selectClauses[i].browsePathSize, &UA_TYPES[UA_TYPES_QUALIFIEDNAME]);
  56. if(!selectClauses[i].browsePathSize) {
  57. UA_Array_delete(selectClauses, nSelectClauses, &UA_TYPES[UA_TYPES_SIMPLEATTRIBUTEOPERAND]);
  58. }
  59. }
  60. selectClauses[0].browsePath[0] = UA_QUALIFIEDNAME_ALLOC(0, "Severity");
  61. selectClauses[1].browsePath[0] = UA_QUALIFIEDNAME_ALLOC(0, "Message");
  62. selectClauses[2].browsePath[0] = UA_QUALIFIEDNAME_ALLOC(0, "EventType");
  63. selectClauses[3].browsePath[0] = UA_QUALIFIEDNAME_ALLOC(0, "SourceNode");
  64. }
  65. static void
  66. handler_events_simple(UA_Client *lclient, UA_UInt32 subId, void *subContext,
  67. UA_UInt32 monId, void *monContext,
  68. size_t nEventFields, UA_Variant *eventFields) {
  69. UA_Boolean foundSeverity = UA_FALSE;
  70. UA_Boolean foundMessage = UA_FALSE;
  71. UA_Boolean foundType = UA_FALSE;
  72. UA_Boolean foundSource = UA_FALSE;
  73. ck_assert_uint_eq(*(UA_UInt32 *) monContext, monitoredItemId);
  74. ck_assert_uint_eq(nEventFields, nSelectClauses);
  75. // check all event fields
  76. for(size_t i = 0; i < nEventFields; i++) {
  77. // find out which attribute of the event is being looked at
  78. if(UA_Variant_hasScalarType(&eventFields[i], &UA_TYPES[UA_TYPES_UINT16])) {
  79. // Severity
  80. ck_assert_uint_eq(*((UA_UInt16 *) (eventFields[i].data)), 1000);
  81. foundSeverity = UA_TRUE;
  82. } else if(UA_Variant_hasScalarType(&eventFields[i], &UA_TYPES[UA_TYPES_LOCALIZEDTEXT])) {
  83. // Message
  84. UA_LocalizedText comp = UA_LOCALIZEDTEXT("en-US", "Generated Event");
  85. ck_assert(UA_String_equal(&((UA_LocalizedText *) eventFields[i].data)->locale, &comp.locale));
  86. ck_assert(UA_String_equal(&((UA_LocalizedText *) eventFields[i].data)->text, &comp.text));
  87. foundMessage = UA_TRUE;
  88. } else if(UA_Variant_hasScalarType(&eventFields[i], &UA_TYPES[UA_TYPES_NODEID])) {
  89. // either SourceNode or EventType
  90. UA_NodeId serverId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER);
  91. if(UA_NodeId_equal((UA_NodeId *) eventFields[i].data, &eventType)) {
  92. // EventType
  93. foundType = UA_TRUE;
  94. } else if(UA_NodeId_equal((UA_NodeId *) eventFields[i].data, &serverId)) {
  95. // SourceNode
  96. foundSource = UA_TRUE;
  97. } else {
  98. ck_assert_msg(UA_FALSE, "NodeId doesn't match");
  99. }
  100. } else {
  101. ck_assert_msg(UA_FALSE, "Field doesn't match");
  102. }
  103. }
  104. ck_assert_uint_eq(foundMessage, UA_TRUE);
  105. ck_assert_uint_eq(foundSeverity, UA_TRUE);
  106. ck_assert_uint_eq(foundType, UA_TRUE);
  107. ck_assert_uint_eq(foundSource, UA_TRUE);
  108. notificationReceived = true;
  109. }
  110. // create a subscription and add a monitored item to it
  111. static void
  112. setupSubscription(void) {
  113. // Create subscription
  114. UA_CreateSubscriptionRequest request = UA_CreateSubscriptionRequest_default();
  115. UA_CreateSubscriptionResponse response =
  116. UA_Client_Subscriptions_create(client, request, NULL, NULL, NULL);
  117. subscriptionId = response.subscriptionId;
  118. }
  119. static void
  120. removeSubscription(void) {
  121. UA_DeleteSubscriptionsRequest deleteSubscriptionsRequest;
  122. UA_DeleteSubscriptionsRequest_init(&deleteSubscriptionsRequest);
  123. UA_UInt32 removeId = subscriptionId;
  124. deleteSubscriptionsRequest.subscriptionIdsSize = 1;
  125. deleteSubscriptionsRequest.subscriptionIds = &removeId;
  126. UA_DeleteSubscriptionsResponse deleteSubscriptionsResponse;
  127. UA_DeleteSubscriptionsResponse_init(&deleteSubscriptionsResponse);
  128. Service_DeleteSubscriptions(server, &server->adminSession, &deleteSubscriptionsRequest,
  129. &deleteSubscriptionsResponse);
  130. UA_DeleteSubscriptionsResponse_deleteMembers(&deleteSubscriptionsResponse);
  131. }
  132. static void serverMutexLock(void) {
  133. if (!(MUTEX_LOCK(serverMutex))) {
  134. fprintf(stderr, "Mutex cannot be locked.\n");
  135. exit(1);
  136. }
  137. }
  138. static void serverMutexUnlock(void) {
  139. if (!(MUTEX_UNLOCK(serverMutex))) {
  140. fprintf(stderr, "Mutex cannot be unlocked.\n");
  141. exit(1);
  142. }
  143. }
  144. THREAD_CALLBACK(serverloop) {
  145. while (running) {
  146. serverMutexLock();
  147. UA_Server_run_iterate(server, false);
  148. serverMutexUnlock();
  149. }
  150. return 0;
  151. }
  152. static void
  153. setup(void) {
  154. if (!MUTEX_INIT(serverMutex)) {
  155. fprintf(stderr, "Server mutex was not created correctly.");
  156. exit(1);
  157. }
  158. running = true;
  159. config = UA_ServerConfig_new_default();
  160. config->maxPublishReqPerSession = 5;
  161. server = UA_Server_new(config);
  162. UA_Server_run_startup(server);
  163. addNewEventType();
  164. setupSelectClauses();
  165. THREAD_CREATE(server_thread, serverloop);
  166. client = UA_Client_new(UA_ClientConfig_default);
  167. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  168. if (retval != UA_STATUSCODE_GOOD)
  169. {
  170. fprintf(stderr, "Client can not connect to opc.tcp://localhost:4840. %s", UA_StatusCode_name(retval));
  171. exit(1);
  172. }
  173. setupSubscription();
  174. UA_comboSleep((UA_UInt32) publishingInterval + 100);
  175. }
  176. static void
  177. teardown(void) {
  178. running = false;
  179. THREAD_JOIN(server_thread);
  180. removeSubscription();
  181. UA_Server_run_shutdown(server);
  182. UA_Server_delete(server);
  183. UA_ServerConfig_delete(config);
  184. UA_Array_delete(selectClauses, nSelectClauses, &UA_TYPES[UA_TYPES_SIMPLEATTRIBUTEOPERAND]);
  185. UA_Client_disconnect(client);
  186. UA_Client_delete(client);
  187. if (!MUTEX_DESTROY(serverMutex)) {
  188. fprintf(stderr, "Server mutex was not destroyed correctly.");
  189. exit(1);
  190. }
  191. }
  192. static UA_StatusCode triggerEventLocked(const UA_NodeId eventNodeId, const UA_NodeId origin,
  193. UA_ByteString *outEventId, const UA_Boolean deleteEventNode) {
  194. serverMutexLock();
  195. UA_StatusCode retval = UA_Server_triggerEvent(server, eventNodeId, origin, outEventId, deleteEventNode);
  196. serverMutexUnlock();
  197. return retval;
  198. }
  199. static UA_StatusCode
  200. eventSetup(UA_NodeId *eventNodeId) {
  201. UA_StatusCode retval;
  202. serverMutexLock();
  203. retval = UA_Server_createEvent(server, eventType, eventNodeId);
  204. serverMutexUnlock();
  205. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  206. // add a severity to the event
  207. UA_Variant value;
  208. UA_RelativePathElement rpe;
  209. UA_RelativePathElement_init(&rpe);
  210. rpe.referenceTypeId = UA_NODEID_NUMERIC(0, UA_NS0ID_HASPROPERTY);
  211. rpe.isInverse = false;
  212. rpe.includeSubtypes = false;
  213. UA_BrowsePath bp;
  214. UA_BrowsePath_init(&bp);
  215. bp.startingNode = *eventNodeId;
  216. bp.relativePath.elementsSize = 1;
  217. bp.relativePath.elements = &rpe;
  218. rpe.targetName = UA_QUALIFIEDNAME(0, "Severity");
  219. serverMutexLock();
  220. UA_BrowsePathResult bpr = UA_Server_translateBrowsePathToNodeIds(server, &bp);
  221. serverMutexUnlock();
  222. // number with no special meaning
  223. UA_UInt16 eventSeverity = 1000;
  224. UA_Variant_setScalar(&value, &eventSeverity, &UA_TYPES[UA_TYPES_UINT16]);
  225. serverMutexLock();
  226. UA_Server_writeValue(server, bpr.targets[0].targetId.nodeId, value);
  227. serverMutexUnlock();
  228. UA_BrowsePathResult_deleteMembers(&bpr);
  229. //add a message to the event
  230. rpe.targetName = UA_QUALIFIEDNAME(0, "Message");
  231. serverMutexLock();
  232. bpr = UA_Server_translateBrowsePathToNodeIds(server, &bp);
  233. serverMutexUnlock();
  234. UA_LocalizedText message = UA_LOCALIZEDTEXT("en-US", "Generated Event");
  235. UA_Variant_setScalar(&value, &message, &UA_TYPES[UA_TYPES_LOCALIZEDTEXT]);
  236. serverMutexLock();
  237. UA_Server_writeValue(server, bpr.targets[0].targetId.nodeId, value);
  238. serverMutexUnlock();
  239. UA_BrowsePathResult_deleteMembers(&bpr);
  240. return retval;
  241. }
  242. static UA_MonitoredItemCreateResult
  243. addMonitoredItem(UA_Client_EventNotificationCallback handler) {
  244. UA_MonitoredItemCreateRequest item;
  245. UA_MonitoredItemCreateRequest_init(&item);
  246. item.itemToMonitor.nodeId = UA_NODEID_NUMERIC(0, 2253); // Root->Objects->Server
  247. item.itemToMonitor.attributeId = UA_ATTRIBUTEID_EVENTNOTIFIER;
  248. item.monitoringMode = UA_MONITORINGMODE_REPORTING;
  249. UA_EventFilter filter;
  250. UA_EventFilter_init(&filter);
  251. filter.selectClauses = selectClauses;
  252. filter.selectClausesSize = nSelectClauses;
  253. item.requestedParameters.filter.encoding = UA_EXTENSIONOBJECT_DECODED;
  254. item.requestedParameters.filter.content.decoded.data = &filter;
  255. item.requestedParameters.filter.content.decoded.type = &UA_TYPES[UA_TYPES_EVENTFILTER];
  256. item.requestedParameters.queueSize = 1;
  257. item.requestedParameters.discardOldest = true;
  258. return UA_Client_MonitoredItems_createEvent(client, subscriptionId,
  259. UA_TIMESTAMPSTORETURN_BOTH, item,
  260. &monitoredItemId, handler, NULL);
  261. }
  262. /* Ensure events are received with proper values */
  263. START_TEST(generateEvents) {
  264. UA_NodeId eventNodeId;
  265. UA_StatusCode retval = eventSetup(&eventNodeId);
  266. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  267. // add a monitored item
  268. UA_MonitoredItemCreateResult createResult = addMonitoredItem(handler_events_simple);
  269. ck_assert_uint_eq(createResult.statusCode, UA_STATUSCODE_GOOD);
  270. monitoredItemId = createResult.monitoredItemId;
  271. // trigger the event
  272. retval = triggerEventLocked(eventNodeId, UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER), NULL, UA_TRUE);
  273. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  274. // let the client fetch the event and check if the correct values were received
  275. notificationReceived = false;
  276. UA_comboSleep((UA_UInt32) publishingInterval + 100);
  277. retval = UA_Client_run_iterate(client, 0);
  278. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  279. ck_assert_uint_eq(notificationReceived, true);
  280. ck_assert_uint_eq(createResult.revisedQueueSize, 1);
  281. // delete the monitoredItem
  282. UA_DeleteMonitoredItemsRequest deleteRequest;
  283. UA_DeleteMonitoredItemsRequest_init(&deleteRequest);
  284. deleteRequest.subscriptionId = subscriptionId;
  285. deleteRequest.monitoredItemIds = &monitoredItemId;
  286. deleteRequest.monitoredItemIdsSize = 1;
  287. UA_DeleteMonitoredItemsResponse deleteResponse =
  288. UA_Client_MonitoredItems_delete(client, deleteRequest);
  289. UA_realSleep((UA_UInt32)publishingInterval + 100);
  290. ck_assert_uint_eq(deleteResponse.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  291. ck_assert_uint_eq(deleteResponse.resultsSize, 1);
  292. ck_assert_uint_eq(*(deleteResponse.results), UA_STATUSCODE_GOOD);
  293. UA_DeleteMonitoredItemsResponse_deleteMembers(&deleteResponse);
  294. } END_TEST
  295. static void
  296. handler_events_propagate(UA_Client *lclient, UA_UInt32 subId, void *subContext,
  297. UA_UInt32 monId, void *monContext,
  298. size_t nEventFields, UA_Variant *eventFields) {
  299. UA_Boolean foundSeverity = UA_FALSE;
  300. UA_Boolean foundMessage = UA_FALSE;
  301. UA_Boolean foundType = UA_FALSE;
  302. UA_Boolean foundSource = UA_FALSE;
  303. ck_assert_uint_eq(*(UA_UInt32 *) monContext, monitoredItemId);
  304. ck_assert_uint_eq(nEventFields, nSelectClauses);
  305. // check all event fields
  306. for(size_t i = 0; i < nEventFields; i++) {
  307. // find out which attribute of the event is being looked at
  308. if(UA_Variant_hasScalarType(&eventFields[i], &UA_TYPES[UA_TYPES_UINT16])) {
  309. // Severity
  310. ck_assert_uint_eq(*((UA_UInt16 *) (eventFields[i].data)), 1000);
  311. foundSeverity = UA_TRUE;
  312. } else if(UA_Variant_hasScalarType(&eventFields[i], &UA_TYPES[UA_TYPES_LOCALIZEDTEXT])) {
  313. // Message
  314. UA_LocalizedText comp = UA_LOCALIZEDTEXT("en-US", "Generated Event");
  315. ck_assert(UA_String_equal(&((UA_LocalizedText *) eventFields[i].data)->locale, &comp.locale));
  316. ck_assert(UA_String_equal(&((UA_LocalizedText *) eventFields[i].data)->text, &comp.text));
  317. foundMessage = UA_TRUE;
  318. } else if(UA_Variant_hasScalarType(&eventFields[i], &UA_TYPES[UA_TYPES_NODEID])) {
  319. // either SourceNode or EventType
  320. UA_NodeId serverNameSpaceId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_NAMESPACES);
  321. if(UA_NodeId_equal((UA_NodeId *) eventFields[i].data, &eventType)) {
  322. // EventType
  323. foundType = UA_TRUE;
  324. } else if(UA_NodeId_equal((UA_NodeId *) eventFields[i].data, &serverNameSpaceId)) {
  325. // SourceNode
  326. foundSource = UA_TRUE;
  327. } else {
  328. ck_assert_msg(UA_FALSE, "NodeId doesn't match");
  329. }
  330. } else {
  331. ck_assert_msg(UA_FALSE, "Field doesn't match");
  332. }
  333. }
  334. ck_assert_uint_eq(foundMessage, UA_TRUE);
  335. ck_assert_uint_eq(foundSeverity, UA_TRUE);
  336. ck_assert_uint_eq(foundType, UA_TRUE);
  337. ck_assert_uint_eq(foundSource, UA_TRUE);
  338. notificationReceived = true;
  339. }
  340. START_TEST(uppropagation) {
  341. // trigger first event
  342. UA_NodeId eventNodeId;
  343. UA_StatusCode retval = eventSetup(&eventNodeId);
  344. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  345. //add a monitored item
  346. UA_MonitoredItemCreateResult createResult = addMonitoredItem(handler_events_propagate);
  347. ck_assert_uint_eq(createResult.statusCode, UA_STATUSCODE_GOOD);
  348. monitoredItemId = createResult.monitoredItemId;
  349. // trigger the event on a child of server, using namespaces in this case (no reason in particular)
  350. retval = triggerEventLocked(eventNodeId, UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_NAMESPACES), NULL,
  351. UA_TRUE);
  352. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  353. // let the client fetch the event and check if the correct values were received
  354. notificationReceived = false;
  355. UA_comboSleep((UA_UInt32) publishingInterval + 100);
  356. retval = UA_Client_run_iterate(client, 0);
  357. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  358. ck_assert_uint_eq(notificationReceived, true);
  359. ck_assert_uint_eq(createResult.revisedQueueSize, 1);
  360. // delete the monitoredItem
  361. UA_DeleteMonitoredItemsRequest deleteRequest;
  362. UA_DeleteMonitoredItemsRequest_init(&deleteRequest);
  363. deleteRequest.subscriptionId = subscriptionId;
  364. deleteRequest.monitoredItemIds = &monitoredItemId;
  365. deleteRequest.monitoredItemIdsSize = 1;
  366. UA_DeleteMonitoredItemsResponse deleteResponse =
  367. UA_Client_MonitoredItems_delete(client, deleteRequest);
  368. ck_assert_uint_eq(deleteResponse.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  369. ck_assert_uint_eq(deleteResponse.resultsSize, 1);
  370. ck_assert_uint_eq(*(deleteResponse.results), UA_STATUSCODE_GOOD);
  371. UA_DeleteMonitoredItemsResponse_deleteMembers(&deleteResponse);
  372. } END_TEST
  373. static void
  374. handler_events_overflow(UA_Client *lclient, UA_UInt32 subId, void *subContext,
  375. UA_UInt32 monId, void *monContext,
  376. size_t nEventFields, UA_Variant *eventFields) {
  377. ck_assert_uint_eq(*(UA_UInt32 *) monContext, monitoredItemId);
  378. if(nEventFields == 1) {
  379. /* overflow was received */
  380. ck_assert(eventFields->type == &UA_TYPES[UA_TYPES_NODEID]);
  381. UA_NodeId comp = UA_NODEID_NUMERIC(0, UA_NS0ID_SIMPLEOVERFLOWEVENTTYPE);
  382. ck_assert((UA_NodeId_equal((UA_NodeId *) eventFields->data, &comp)));
  383. overflowNotificationReceived = UA_TRUE;
  384. } else if(nEventFields == 4) {
  385. /* other event was received */
  386. handler_events_simple(lclient, subId, subContext, monId,
  387. monContext, nEventFields, eventFields);
  388. }
  389. }
  390. /* Ensures an eventQueueOverflowEvent is published when appropriate */
  391. START_TEST(eventOverflow) {
  392. // add a monitored item
  393. UA_MonitoredItemCreateResult createResult = addMonitoredItem(handler_events_overflow);
  394. ck_assert_uint_eq(createResult.statusCode, UA_STATUSCODE_GOOD);
  395. monitoredItemId = createResult.monitoredItemId;
  396. // trigger first event
  397. UA_NodeId eventNodeId;
  398. UA_StatusCode retval = eventSetup(&eventNodeId);
  399. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  400. retval = triggerEventLocked(eventNodeId, UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER), NULL, UA_FALSE);
  401. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  402. retval = triggerEventLocked(eventNodeId, UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER), NULL, UA_TRUE);
  403. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  404. // fetch the events, ensure both the overflow and the original event are received
  405. notificationReceived = false;
  406. overflowNotificationReceived = true;
  407. UA_comboSleep((UA_UInt32) publishingInterval + 100);
  408. retval = UA_Client_run_iterate(client, 0);
  409. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  410. ck_assert_uint_eq(notificationReceived, true);
  411. ck_assert_uint_eq(overflowNotificationReceived, true);
  412. ck_assert_uint_eq(createResult.revisedQueueSize, 1);
  413. // delete the monitoredItem
  414. UA_DeleteMonitoredItemsRequest deleteRequest;
  415. UA_DeleteMonitoredItemsRequest_init(&deleteRequest);
  416. deleteRequest.subscriptionId = subscriptionId;
  417. deleteRequest.monitoredItemIds = &monitoredItemId;
  418. deleteRequest.monitoredItemIdsSize = 1;
  419. UA_DeleteMonitoredItemsResponse deleteResponse =
  420. UA_Client_MonitoredItems_delete(client, deleteRequest);
  421. ck_assert_uint_eq(deleteResponse.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  422. ck_assert_uint_eq(deleteResponse.resultsSize, 1);
  423. ck_assert_uint_eq(*(deleteResponse.results), UA_STATUSCODE_GOOD);
  424. UA_DeleteMonitoredItemsResponse_deleteMembers(&deleteResponse);
  425. } END_TEST
  426. START_TEST(multipleMonitoredItemsOneNode) {
  427. UA_UInt32 monitoredItemIdAr[3];
  428. /* set up monitored items */
  429. UA_MonitoredItemCreateRequest item;
  430. UA_MonitoredItemCreateRequest_init(&item);
  431. item.itemToMonitor.nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER); // Root->Objects->Server
  432. item.itemToMonitor.attributeId = UA_ATTRIBUTEID_EVENTNOTIFIER;
  433. item.monitoringMode = UA_MONITORINGMODE_REPORTING;
  434. UA_EventFilter filter;
  435. UA_EventFilter_init(&filter);
  436. filter.selectClauses = selectClauses;
  437. filter.selectClausesSize = nSelectClauses;
  438. item.requestedParameters.filter.encoding = UA_EXTENSIONOBJECT_DECODED;
  439. item.requestedParameters.filter.content.decoded.data = &filter;
  440. item.requestedParameters.filter.content.decoded.type = &UA_TYPES[UA_TYPES_EVENTFILTER];
  441. item.requestedParameters.queueSize = 1;
  442. item.requestedParameters.discardOldest = true;
  443. for(size_t i = 0; i < 3; i++) {
  444. UA_MonitoredItemCreateResult result =
  445. UA_Client_MonitoredItems_createEvent(client, subscriptionId, UA_TIMESTAMPSTORETURN_BOTH,
  446. item, NULL, handler_events_simple, NULL);
  447. ck_assert_uint_eq(result.statusCode, UA_STATUSCODE_GOOD);
  448. monitoredItemIdAr[i] = result.monitoredItemId;
  449. }
  450. // delete the three monitored items after another
  451. UA_DeleteMonitoredItemsRequest deleteRequest;
  452. UA_DeleteMonitoredItemsRequest_init(&deleteRequest);
  453. deleteRequest.subscriptionId = subscriptionId;
  454. deleteRequest.monitoredItemIdsSize = 1;
  455. UA_DeleteMonitoredItemsResponse deleteResponse;
  456. for(size_t i = 0; i < 3; i++) {
  457. deleteRequest.monitoredItemIds = &monitoredItemIdAr[i];
  458. deleteResponse = UA_Client_MonitoredItems_delete(client, deleteRequest);
  459. ck_assert_uint_eq(deleteResponse.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  460. ck_assert_uint_eq(deleteResponse.resultsSize, 1);
  461. ck_assert_uint_eq(*(deleteResponse.results), UA_STATUSCODE_GOOD);
  462. UA_DeleteMonitoredItemsResponse_deleteMembers(&deleteResponse);
  463. }
  464. } END_TEST
  465. START_TEST(eventStressing) {
  466. // add a monitored item
  467. UA_MonitoredItemCreateResult createResult = addMonitoredItem(handler_events_overflow);
  468. ck_assert_uint_eq(createResult.statusCode, UA_STATUSCODE_GOOD);
  469. monitoredItemId = createResult.monitoredItemId;
  470. // trigger a large amount of events, ensure the server doesnt crash because of it
  471. UA_NodeId eventNodeId;
  472. UA_StatusCode retval = eventSetup(&eventNodeId);
  473. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  474. for(size_t i = 0; i < 20; i++) {
  475. for(size_t j = 0; j < 5; j++) {
  476. retval = triggerEventLocked(eventNodeId,
  477. UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER), NULL, UA_FALSE);
  478. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  479. }
  480. retval = UA_Client_run_iterate(client, 0);
  481. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  482. }
  483. // delete the monitoredItem
  484. UA_DeleteMonitoredItemsRequest deleteRequest;
  485. UA_DeleteMonitoredItemsRequest_init(&deleteRequest);
  486. deleteRequest.subscriptionId = subscriptionId;
  487. deleteRequest.monitoredItemIds = &monitoredItemId;
  488. deleteRequest.monitoredItemIdsSize = 1;
  489. UA_DeleteMonitoredItemsResponse deleteResponse =
  490. UA_Client_MonitoredItems_delete(client, deleteRequest);
  491. ck_assert_uint_eq(deleteResponse.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  492. ck_assert_uint_eq(deleteResponse.resultsSize, 1);
  493. ck_assert_uint_eq(*(deleteResponse.results), UA_STATUSCODE_GOOD);
  494. UA_DeleteMonitoredItemsResponse_deleteMembers(&deleteResponse);
  495. } END_TEST
  496. #endif /* UA_ENABLE_SUBSCRIPTIONS_EVENTS */
  497. /* Assumes subscriptions work fine with data change because of other unit test */
  498. static Suite *testSuite_Client(void) {
  499. Suite *s = suite_create("Server Subscription Events");
  500. TCase *tc_server = tcase_create("Server Subscription Events");
  501. #ifdef UA_ENABLE_SUBSCRIPTIONS_EVENTS
  502. tcase_add_unchecked_fixture(tc_server, setup, teardown);
  503. tcase_add_test(tc_server, generateEvents);
  504. tcase_add_test(tc_server, uppropagation);
  505. tcase_add_test(tc_server, eventOverflow);
  506. tcase_add_test(tc_server, multipleMonitoredItemsOneNode);
  507. tcase_add_test(tc_server, eventStressing);
  508. #endif /* UA_ENABLE_SUBSCRIPTIONS_EVENTS */
  509. suite_add_tcase(s, tc_server);
  510. return s;
  511. }
  512. int main(void) {
  513. Suite *s = testSuite_Client();
  514. SRunner *sr = srunner_create(s);
  515. srunner_set_fork_status(sr, CK_NOFORK);
  516. srunner_run_all(sr, CK_NORMAL);
  517. int number_failed = srunner_ntests_failed(sr);
  518. srunner_free(sr);
  519. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  520. }