check_subscription_events.c 22 KB

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