check_subscription_events.c 24 KB

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