check_subscription_events.c 22 KB

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