check_subscription_events.c 24 KB

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