check_subscription_events.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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. ck_assert_uint_eq(MUTEX_LOCK(serverMutex), true);
  134. }
  135. static void serverMutexUnlock(void) {
  136. ck_assert_uint_eq(MUTEX_UNLOCK(serverMutex), true);
  137. }
  138. THREAD_CALLBACK(serverloop) {
  139. while (*running) {
  140. serverMutexLock();
  141. UA_Server_run_iterate(server, true);
  142. serverMutexUnlock();
  143. }
  144. return 0;
  145. }
  146. static void
  147. setup(void) {
  148. ck_assert_uint_eq(MUTEX_INIT(serverMutex), true);
  149. running = UA_Boolean_new();
  150. *running = true;
  151. config = UA_ServerConfig_new_default();
  152. config->maxPublishReqPerSession = 5;
  153. server = UA_Server_new(config);
  154. UA_Server_run_startup(server);
  155. addNewEventType();
  156. setupSelectClauses();
  157. THREAD_CREATE(server_thread, serverloop);
  158. client = UA_Client_new(UA_ClientConfig_default);
  159. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  160. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  161. setupSubscription();
  162. UA_comboSleep((UA_UInt32) publishingInterval + 100);
  163. }
  164. static void
  165. teardown(void) {
  166. removeSubscription();
  167. *running = false;
  168. THREAD_JOIN(server_thread);
  169. UA_Server_run_shutdown(server);
  170. UA_Boolean_delete(running);
  171. UA_Server_delete(server);
  172. UA_ServerConfig_delete(config);
  173. UA_Array_delete(selectClauses, nSelectClauses, &UA_TYPES[UA_TYPES_SIMPLEATTRIBUTEOPERAND]);
  174. UA_Client_disconnect(client);
  175. UA_Client_delete(client);
  176. ck_assert_uint_eq(MUTEX_DESTROY(serverMutex), true);
  177. }
  178. static UA_StatusCode triggerEventLocked(const UA_NodeId eventNodeId, const UA_NodeId origin,
  179. UA_ByteString *outEventId, const UA_Boolean deleteEventNode) {
  180. serverMutexLock();
  181. UA_StatusCode retval = UA_Server_triggerEvent(server, eventNodeId, origin, outEventId, deleteEventNode);
  182. serverMutexUnlock();
  183. return retval;
  184. }
  185. static UA_StatusCode
  186. eventSetup(UA_NodeId *eventNodeId) {
  187. UA_StatusCode retval;
  188. serverMutexLock();
  189. retval = UA_Server_createEvent(server, eventType, eventNodeId);
  190. serverMutexUnlock();
  191. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  192. // add a severity to the event
  193. UA_Variant value;
  194. UA_RelativePathElement rpe;
  195. UA_RelativePathElement_init(&rpe);
  196. rpe.referenceTypeId = UA_NODEID_NUMERIC(0, UA_NS0ID_HASPROPERTY);
  197. rpe.isInverse = false;
  198. rpe.includeSubtypes = false;
  199. UA_BrowsePath bp;
  200. UA_BrowsePath_init(&bp);
  201. bp.startingNode = *eventNodeId;
  202. bp.relativePath.elementsSize = 1;
  203. bp.relativePath.elements = &rpe;
  204. rpe.targetName = UA_QUALIFIEDNAME(0, "Severity");
  205. serverMutexLock();
  206. UA_BrowsePathResult bpr = UA_Server_translateBrowsePathToNodeIds(server, &bp);
  207. serverMutexUnlock();
  208. // number with no special meaning
  209. UA_UInt16 eventSeverity = 1000;
  210. UA_Variant_setScalar(&value, &eventSeverity, &UA_TYPES[UA_TYPES_UINT16]);
  211. serverMutexLock();
  212. UA_Server_writeValue(server, bpr.targets[0].targetId.nodeId, value);
  213. serverMutexUnlock();
  214. UA_BrowsePathResult_deleteMembers(&bpr);
  215. //add a message to the event
  216. rpe.targetName = UA_QUALIFIEDNAME(0, "Message");
  217. serverMutexLock();
  218. bpr = UA_Server_translateBrowsePathToNodeIds(server, &bp);
  219. serverMutexUnlock();
  220. UA_LocalizedText message = UA_LOCALIZEDTEXT("en-US", "Generated Event");
  221. UA_Variant_setScalar(&value, &message, &UA_TYPES[UA_TYPES_LOCALIZEDTEXT]);
  222. serverMutexLock();
  223. UA_Server_writeValue(server, bpr.targets[0].targetId.nodeId, value);
  224. serverMutexUnlock();
  225. UA_BrowsePathResult_deleteMembers(&bpr);
  226. return retval;
  227. }
  228. static UA_MonitoredItemCreateResult
  229. addMonitoredItem(UA_Client_EventNotificationCallback handler) {
  230. UA_MonitoredItemCreateRequest item;
  231. UA_MonitoredItemCreateRequest_init(&item);
  232. item.itemToMonitor.nodeId = UA_NODEID_NUMERIC(0, 2253); // Root->Objects->Server
  233. item.itemToMonitor.attributeId = UA_ATTRIBUTEID_EVENTNOTIFIER;
  234. item.monitoringMode = UA_MONITORINGMODE_REPORTING;
  235. UA_EventFilter filter;
  236. UA_EventFilter_init(&filter);
  237. filter.selectClauses = selectClauses;
  238. filter.selectClausesSize = nSelectClauses;
  239. item.requestedParameters.filter.encoding = UA_EXTENSIONOBJECT_DECODED;
  240. item.requestedParameters.filter.content.decoded.data = &filter;
  241. item.requestedParameters.filter.content.decoded.type = &UA_TYPES[UA_TYPES_EVENTFILTER];
  242. item.requestedParameters.queueSize = 1;
  243. item.requestedParameters.discardOldest = true;
  244. return UA_Client_MonitoredItems_createEvent(client, subscriptionId,
  245. UA_TIMESTAMPSTORETURN_BOTH, item,
  246. &monitoredItemId, handler, NULL);
  247. }
  248. /* Ensure events are received with proper values */
  249. START_TEST(generateEvents) {
  250. UA_NodeId eventNodeId;
  251. UA_StatusCode retval = eventSetup(&eventNodeId);
  252. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  253. // add a monitored item
  254. UA_MonitoredItemCreateResult createResult = addMonitoredItem(handler_events_simple);
  255. ck_assert_uint_eq(createResult.statusCode, UA_STATUSCODE_GOOD);
  256. monitoredItemId = createResult.monitoredItemId;
  257. // trigger the event
  258. retval = triggerEventLocked(eventNodeId, UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER), NULL, UA_TRUE);
  259. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  260. // let the client fetch the event and check if the correct values were received
  261. notificationReceived = false;
  262. UA_comboSleep((UA_UInt32) publishingInterval + 100);
  263. retval = UA_Client_run_iterate(client, 0);
  264. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  265. ck_assert_uint_eq(notificationReceived, true);
  266. ck_assert_uint_eq(createResult.revisedQueueSize, 1);
  267. // delete the monitoredItem
  268. UA_DeleteMonitoredItemsRequest deleteRequest;
  269. UA_DeleteMonitoredItemsRequest_init(&deleteRequest);
  270. deleteRequest.subscriptionId = subscriptionId;
  271. deleteRequest.monitoredItemIds = &monitoredItemId;
  272. deleteRequest.monitoredItemIdsSize = 1;
  273. UA_DeleteMonitoredItemsResponse deleteResponse =
  274. UA_Client_MonitoredItems_delete(client, deleteRequest);
  275. UA_realSleep((UA_UInt32)publishingInterval + 100);
  276. ck_assert_uint_eq(deleteResponse.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  277. ck_assert_uint_eq(deleteResponse.resultsSize, 1);
  278. ck_assert_uint_eq(*(deleteResponse.results), UA_STATUSCODE_GOOD);
  279. UA_DeleteMonitoredItemsResponse_deleteMembers(&deleteResponse);
  280. } END_TEST
  281. static void
  282. handler_events_propagate(UA_Client *lclient, UA_UInt32 subId, void *subContext,
  283. UA_UInt32 monId, void *monContext,
  284. size_t nEventFields, UA_Variant *eventFields) {
  285. UA_Boolean foundSeverity = UA_FALSE;
  286. UA_Boolean foundMessage = UA_FALSE;
  287. UA_Boolean foundType = UA_FALSE;
  288. UA_Boolean foundSource = UA_FALSE;
  289. ck_assert_uint_eq(*(UA_UInt32 *) monContext, monitoredItemId);
  290. ck_assert_uint_eq(nEventFields, nSelectClauses);
  291. // check all event fields
  292. for(size_t i = 0; i < nEventFields; i++) {
  293. // find out which attribute of the event is being looked at
  294. if(UA_Variant_hasScalarType(&eventFields[i], &UA_TYPES[UA_TYPES_UINT16])) {
  295. // Severity
  296. ck_assert_uint_eq(*((UA_UInt16 *) (eventFields[i].data)), 1000);
  297. foundSeverity = UA_TRUE;
  298. } else if(UA_Variant_hasScalarType(&eventFields[i], &UA_TYPES[UA_TYPES_LOCALIZEDTEXT])) {
  299. // Message
  300. UA_LocalizedText comp = UA_LOCALIZEDTEXT("en-US", "Generated Event");
  301. ck_assert(UA_String_equal(&((UA_LocalizedText *) eventFields[i].data)->locale, &comp.locale));
  302. ck_assert(UA_String_equal(&((UA_LocalizedText *) eventFields[i].data)->text, &comp.text));
  303. foundMessage = UA_TRUE;
  304. } else if(UA_Variant_hasScalarType(&eventFields[i], &UA_TYPES[UA_TYPES_NODEID])) {
  305. // either SourceNode or EventType
  306. UA_NodeId serverNameSpaceId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_NAMESPACES);
  307. if(UA_NodeId_equal((UA_NodeId *) eventFields[i].data, &eventType)) {
  308. // EventType
  309. foundType = UA_TRUE;
  310. } else if(UA_NodeId_equal((UA_NodeId *) eventFields[i].data, &serverNameSpaceId)) {
  311. // SourceNode
  312. foundSource = UA_TRUE;
  313. } else {
  314. ck_assert_msg(UA_FALSE, "NodeId doesn't match");
  315. }
  316. } else {
  317. ck_assert_msg(UA_FALSE, "Field doesn't match");
  318. }
  319. }
  320. ck_assert_uint_eq(foundMessage, UA_TRUE);
  321. ck_assert_uint_eq(foundSeverity, UA_TRUE);
  322. ck_assert_uint_eq(foundType, UA_TRUE);
  323. ck_assert_uint_eq(foundSource, UA_TRUE);
  324. notificationReceived = true;
  325. }
  326. START_TEST(uppropagation) {
  327. // trigger first event
  328. UA_NodeId eventNodeId;
  329. UA_StatusCode retval = eventSetup(&eventNodeId);
  330. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  331. //add a monitored item
  332. UA_MonitoredItemCreateResult createResult = addMonitoredItem(handler_events_propagate);
  333. ck_assert_uint_eq(createResult.statusCode, UA_STATUSCODE_GOOD);
  334. monitoredItemId = createResult.monitoredItemId;
  335. // trigger the event on a child of server, using namespaces in this case (no reason in particular)
  336. retval = triggerEventLocked(eventNodeId, UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_NAMESPACES), NULL,
  337. UA_TRUE);
  338. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  339. // let the client fetch the event and check if the correct values were received
  340. notificationReceived = false;
  341. UA_comboSleep((UA_UInt32) publishingInterval + 100);
  342. retval = UA_Client_run_iterate(client, 0);
  343. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  344. ck_assert_uint_eq(notificationReceived, true);
  345. ck_assert_uint_eq(createResult.revisedQueueSize, 1);
  346. // delete the monitoredItem
  347. UA_DeleteMonitoredItemsRequest deleteRequest;
  348. UA_DeleteMonitoredItemsRequest_init(&deleteRequest);
  349. deleteRequest.subscriptionId = subscriptionId;
  350. deleteRequest.monitoredItemIds = &monitoredItemId;
  351. deleteRequest.monitoredItemIdsSize = 1;
  352. UA_DeleteMonitoredItemsResponse deleteResponse =
  353. UA_Client_MonitoredItems_delete(client, deleteRequest);
  354. ck_assert_uint_eq(deleteResponse.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  355. ck_assert_uint_eq(deleteResponse.resultsSize, 1);
  356. ck_assert_uint_eq(*(deleteResponse.results), UA_STATUSCODE_GOOD);
  357. UA_DeleteMonitoredItemsResponse_deleteMembers(&deleteResponse);
  358. } END_TEST
  359. static void
  360. handler_events_overflow(UA_Client *lclient, UA_UInt32 subId, void *subContext,
  361. UA_UInt32 monId, void *monContext,
  362. size_t nEventFields, UA_Variant *eventFields) {
  363. ck_assert_uint_eq(*(UA_UInt32 *) monContext, monitoredItemId);
  364. if(nEventFields == 1) {
  365. /* overflow was received */
  366. ck_assert(eventFields->type == &UA_TYPES[UA_TYPES_NODEID]);
  367. UA_NodeId comp = UA_NODEID_NUMERIC(0, UA_NS0ID_SIMPLEOVERFLOWEVENTTYPE);
  368. ck_assert((UA_NodeId_equal((UA_NodeId *) eventFields->data, &comp)));
  369. overflowNotificationReceived = UA_TRUE;
  370. } else if(nEventFields == 4) {
  371. /* other event was received */
  372. handler_events_simple(lclient, subId, subContext, monId,
  373. monContext, nEventFields, eventFields);
  374. }
  375. }
  376. /* Ensures an eventQueueOverflowEvent is published when appropriate */
  377. START_TEST(eventOverflow) {
  378. // add a monitored item
  379. UA_MonitoredItemCreateResult createResult = addMonitoredItem(handler_events_overflow);
  380. ck_assert_uint_eq(createResult.statusCode, UA_STATUSCODE_GOOD);
  381. monitoredItemId = createResult.monitoredItemId;
  382. // trigger first event
  383. UA_NodeId eventNodeId;
  384. UA_StatusCode retval = eventSetup(&eventNodeId);
  385. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  386. retval = triggerEventLocked(eventNodeId, UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER), NULL, UA_FALSE);
  387. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  388. retval = triggerEventLocked(eventNodeId, UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER), NULL, UA_TRUE);
  389. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  390. // fetch the events, ensure both the overflow and the original event are received
  391. notificationReceived = false;
  392. overflowNotificationReceived = true;
  393. UA_comboSleep((UA_UInt32) publishingInterval + 100);
  394. retval = UA_Client_run_iterate(client, 0);
  395. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  396. ck_assert_uint_eq(notificationReceived, true);
  397. ck_assert_uint_eq(overflowNotificationReceived, true);
  398. ck_assert_uint_eq(createResult.revisedQueueSize, 1);
  399. // delete the monitoredItem
  400. UA_DeleteMonitoredItemsRequest deleteRequest;
  401. UA_DeleteMonitoredItemsRequest_init(&deleteRequest);
  402. deleteRequest.subscriptionId = subscriptionId;
  403. deleteRequest.monitoredItemIds = &monitoredItemId;
  404. deleteRequest.monitoredItemIdsSize = 1;
  405. UA_DeleteMonitoredItemsResponse deleteResponse =
  406. 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. ck_assert_uint_eq(*(deleteResponse.results), UA_STATUSCODE_GOOD);
  410. UA_DeleteMonitoredItemsResponse_deleteMembers(&deleteResponse);
  411. } END_TEST
  412. START_TEST(multipleMonitoredItemsOneNode) {
  413. UA_UInt32 monitoredItemIdAr[3];
  414. /* set up monitored items */
  415. UA_MonitoredItemCreateRequest item;
  416. UA_MonitoredItemCreateRequest_init(&item);
  417. item.itemToMonitor.nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER); // Root->Objects->Server
  418. item.itemToMonitor.attributeId = UA_ATTRIBUTEID_EVENTNOTIFIER;
  419. item.monitoringMode = UA_MONITORINGMODE_REPORTING;
  420. UA_EventFilter filter;
  421. UA_EventFilter_init(&filter);
  422. filter.selectClauses = selectClauses;
  423. filter.selectClausesSize = nSelectClauses;
  424. item.requestedParameters.filter.encoding = UA_EXTENSIONOBJECT_DECODED;
  425. item.requestedParameters.filter.content.decoded.data = &filter;
  426. item.requestedParameters.filter.content.decoded.type = &UA_TYPES[UA_TYPES_EVENTFILTER];
  427. item.requestedParameters.queueSize = 1;
  428. item.requestedParameters.discardOldest = true;
  429. for(size_t i = 0; i < 3; i++) {
  430. UA_MonitoredItemCreateResult result =
  431. UA_Client_MonitoredItems_createEvent(client, subscriptionId, UA_TIMESTAMPSTORETURN_BOTH,
  432. item, NULL, handler_events_simple, NULL);
  433. ck_assert_uint_eq(result.statusCode, UA_STATUSCODE_GOOD);
  434. monitoredItemIdAr[i] = result.monitoredItemId;
  435. }
  436. // delete the three monitored items after another
  437. UA_DeleteMonitoredItemsRequest deleteRequest;
  438. UA_DeleteMonitoredItemsRequest_init(&deleteRequest);
  439. deleteRequest.subscriptionId = subscriptionId;
  440. deleteRequest.monitoredItemIdsSize = 1;
  441. UA_DeleteMonitoredItemsResponse deleteResponse;
  442. for(size_t i = 0; i < 3; i++) {
  443. deleteRequest.monitoredItemIds = &monitoredItemIdAr[i];
  444. deleteResponse = UA_Client_MonitoredItems_delete(client, deleteRequest);
  445. ck_assert_uint_eq(deleteResponse.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  446. ck_assert_uint_eq(deleteResponse.resultsSize, 1);
  447. ck_assert_uint_eq(*(deleteResponse.results), UA_STATUSCODE_GOOD);
  448. UA_DeleteMonitoredItemsResponse_deleteMembers(&deleteResponse);
  449. }
  450. } END_TEST
  451. START_TEST(eventStressing) {
  452. // add a monitored item
  453. UA_MonitoredItemCreateResult createResult = addMonitoredItem(handler_events_overflow);
  454. ck_assert_uint_eq(createResult.statusCode, UA_STATUSCODE_GOOD);
  455. monitoredItemId = createResult.monitoredItemId;
  456. // trigger a large amount of events, ensure the server doesnt crash because of it
  457. UA_NodeId eventNodeId;
  458. UA_StatusCode retval = eventSetup(&eventNodeId);
  459. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  460. for(size_t i = 0; i < 20; i++) {
  461. for(size_t j = 0; j < 5; j++) {
  462. retval = triggerEventLocked(eventNodeId,
  463. UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER), NULL, UA_FALSE);
  464. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  465. }
  466. retval = UA_Client_run_iterate(client, 0);
  467. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  468. }
  469. // delete the monitoredItem
  470. UA_DeleteMonitoredItemsRequest deleteRequest;
  471. UA_DeleteMonitoredItemsRequest_init(&deleteRequest);
  472. deleteRequest.subscriptionId = subscriptionId;
  473. deleteRequest.monitoredItemIds = &monitoredItemId;
  474. deleteRequest.monitoredItemIdsSize = 1;
  475. UA_DeleteMonitoredItemsResponse deleteResponse =
  476. UA_Client_MonitoredItems_delete(client, deleteRequest);
  477. ck_assert_uint_eq(deleteResponse.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  478. ck_assert_uint_eq(deleteResponse.resultsSize, 1);
  479. ck_assert_uint_eq(*(deleteResponse.results), UA_STATUSCODE_GOOD);
  480. UA_DeleteMonitoredItemsResponse_deleteMembers(&deleteResponse);
  481. } END_TEST
  482. #endif /* UA_ENABLE_SUBSCRIPTIONS_EVENTS */
  483. /* Assumes subscriptions work fine with data change because of other unit test */
  484. static Suite *testSuite_Client(void) {
  485. Suite *s = suite_create("Server Subscription Events");
  486. TCase *tc_server = tcase_create("Server Subscription Events");
  487. #ifdef UA_ENABLE_SUBSCRIPTIONS_EVENTS
  488. tcase_add_unchecked_fixture(tc_server, setup, teardown);
  489. tcase_add_test(tc_server, generateEvents);
  490. tcase_add_test(tc_server, uppropagation);
  491. tcase_add_test(tc_server, eventOverflow);
  492. tcase_add_test(tc_server, multipleMonitoredItemsOneNode);
  493. tcase_add_test(tc_server, eventStressing);
  494. #endif /* UA_ENABLE_SUBSCRIPTIONS_EVENTS */
  495. suite_add_tcase(s, tc_server);
  496. return s;
  497. }
  498. int main(void) {
  499. Suite *s = testSuite_Client();
  500. SRunner *sr = srunner_create(s);
  501. srunner_set_fork_status(sr, CK_NOFORK);
  502. srunner_run_all(sr, CK_NORMAL);
  503. int number_failed = srunner_ntests_failed(sr);
  504. srunner_free(sr);
  505. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  506. }