check_subscription_events.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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. ck_assert_uint_eq(bpr.statusCode, UA_STATUSCODE_GOOD);
  226. // number with no special meaning
  227. UA_UInt16 eventSeverity = 1000;
  228. UA_Variant_setScalar(&value, &eventSeverity, &UA_TYPES[UA_TYPES_UINT16]);
  229. serverMutexLock();
  230. retval = UA_Server_writeValue(server, bpr.targets[0].targetId.nodeId, value);
  231. serverMutexUnlock();
  232. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  233. UA_BrowsePathResult_deleteMembers(&bpr);
  234. //add a message to the event
  235. rpe.targetName = UA_QUALIFIEDNAME(0, "Message");
  236. serverMutexLock();
  237. bpr = UA_Server_translateBrowsePathToNodeIds(server, &bp);
  238. serverMutexUnlock();
  239. ck_assert_uint_eq(bpr.statusCode, UA_STATUSCODE_GOOD);
  240. UA_LocalizedText message = UA_LOCALIZEDTEXT("en-US", "Generated Event");
  241. UA_Variant_setScalar(&value, &message, &UA_TYPES[UA_TYPES_LOCALIZEDTEXT]);
  242. serverMutexLock();
  243. retval = UA_Server_writeValue(server, bpr.targets[0].targetId.nodeId, value);
  244. serverMutexUnlock();
  245. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  246. UA_BrowsePathResult_deleteMembers(&bpr);
  247. return retval;
  248. }
  249. static UA_MonitoredItemCreateResult
  250. addMonitoredItem(UA_Client_EventNotificationCallback handler) {
  251. UA_MonitoredItemCreateRequest item;
  252. UA_MonitoredItemCreateRequest_init(&item);
  253. item.itemToMonitor.nodeId = UA_NODEID_NUMERIC(0, 2253); // Root->Objects->Server
  254. item.itemToMonitor.attributeId = UA_ATTRIBUTEID_EVENTNOTIFIER;
  255. item.monitoringMode = UA_MONITORINGMODE_REPORTING;
  256. UA_EventFilter filter;
  257. UA_EventFilter_init(&filter);
  258. filter.selectClauses = selectClauses;
  259. filter.selectClausesSize = nSelectClauses;
  260. item.requestedParameters.filter.encoding = UA_EXTENSIONOBJECT_DECODED;
  261. item.requestedParameters.filter.content.decoded.data = &filter;
  262. item.requestedParameters.filter.content.decoded.type = &UA_TYPES[UA_TYPES_EVENTFILTER];
  263. item.requestedParameters.queueSize = 1;
  264. item.requestedParameters.discardOldest = true;
  265. return UA_Client_MonitoredItems_createEvent(client, subscriptionId,
  266. UA_TIMESTAMPSTORETURN_BOTH, item,
  267. &monitoredItemId, handler, NULL);
  268. }
  269. /* Ensure events are received with proper values */
  270. START_TEST(generateEvents) {
  271. UA_NodeId eventNodeId;
  272. UA_StatusCode retval = eventSetup(&eventNodeId);
  273. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  274. // add a monitored item
  275. UA_MonitoredItemCreateResult createResult = addMonitoredItem(handler_events_simple);
  276. ck_assert_uint_eq(createResult.statusCode, UA_STATUSCODE_GOOD);
  277. monitoredItemId = createResult.monitoredItemId;
  278. // trigger the event
  279. retval = triggerEventLocked(eventNodeId, UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER), NULL, UA_TRUE);
  280. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  281. // let the client fetch the event and check if the correct values were received
  282. notificationReceived = false;
  283. UA_comboSleep((UA_UInt32) publishingInterval + 100);
  284. retval = UA_Client_run_iterate(client, 0);
  285. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  286. ck_assert_uint_eq(notificationReceived, true);
  287. ck_assert_uint_eq(createResult.revisedQueueSize, 1);
  288. // delete the monitoredItem
  289. UA_DeleteMonitoredItemsRequest deleteRequest;
  290. UA_DeleteMonitoredItemsRequest_init(&deleteRequest);
  291. deleteRequest.subscriptionId = subscriptionId;
  292. deleteRequest.monitoredItemIds = &monitoredItemId;
  293. deleteRequest.monitoredItemIdsSize = 1;
  294. UA_DeleteMonitoredItemsResponse deleteResponse =
  295. UA_Client_MonitoredItems_delete(client, deleteRequest);
  296. UA_realSleep((UA_UInt32)publishingInterval + 100);
  297. ck_assert_uint_eq(deleteResponse.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  298. ck_assert_uint_eq(deleteResponse.resultsSize, 1);
  299. ck_assert_uint_eq(*(deleteResponse.results), UA_STATUSCODE_GOOD);
  300. UA_DeleteMonitoredItemsResponse_deleteMembers(&deleteResponse);
  301. } END_TEST
  302. static void
  303. handler_events_propagate(UA_Client *lclient, UA_UInt32 subId, void *subContext,
  304. UA_UInt32 monId, void *monContext,
  305. size_t nEventFields, UA_Variant *eventFields) {
  306. UA_Boolean foundSeverity = UA_FALSE;
  307. UA_Boolean foundMessage = UA_FALSE;
  308. UA_Boolean foundType = UA_FALSE;
  309. UA_Boolean foundSource = UA_FALSE;
  310. ck_assert_uint_eq(*(UA_UInt32 *) monContext, monitoredItemId);
  311. ck_assert_uint_eq(nEventFields, nSelectClauses);
  312. // check all event fields
  313. for(size_t i = 0; i < nEventFields; i++) {
  314. // find out which attribute of the event is being looked at
  315. if(UA_Variant_hasScalarType(&eventFields[i], &UA_TYPES[UA_TYPES_UINT16])) {
  316. // Severity
  317. ck_assert_uint_eq(*((UA_UInt16 *) (eventFields[i].data)), 1000);
  318. foundSeverity = UA_TRUE;
  319. } else if(UA_Variant_hasScalarType(&eventFields[i], &UA_TYPES[UA_TYPES_LOCALIZEDTEXT])) {
  320. // Message
  321. UA_LocalizedText comp = UA_LOCALIZEDTEXT("en-US", "Generated Event");
  322. ck_assert(UA_String_equal(&((UA_LocalizedText *) eventFields[i].data)->locale, &comp.locale));
  323. ck_assert(UA_String_equal(&((UA_LocalizedText *) eventFields[i].data)->text, &comp.text));
  324. foundMessage = UA_TRUE;
  325. } else if(UA_Variant_hasScalarType(&eventFields[i], &UA_TYPES[UA_TYPES_NODEID])) {
  326. // either SourceNode or EventType
  327. UA_NodeId serverNameSpaceId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_VENDORSERVERINFO);
  328. if(UA_NodeId_equal((UA_NodeId *) eventFields[i].data, &eventType)) {
  329. // EventType
  330. foundType = UA_TRUE;
  331. } else if(UA_NodeId_equal((UA_NodeId *) eventFields[i].data, &serverNameSpaceId)) {
  332. // SourceNode
  333. foundSource = UA_TRUE;
  334. } else {
  335. ck_assert_msg(UA_FALSE, "NodeId doesn't match");
  336. }
  337. } else {
  338. ck_assert_msg(UA_FALSE, "Field doesn't match");
  339. }
  340. }
  341. ck_assert_uint_eq(foundMessage, UA_TRUE);
  342. ck_assert_uint_eq(foundSeverity, UA_TRUE);
  343. ck_assert_uint_eq(foundType, UA_TRUE);
  344. ck_assert_uint_eq(foundSource, UA_TRUE);
  345. notificationReceived = true;
  346. }
  347. START_TEST(uppropagation) {
  348. // trigger first event
  349. UA_NodeId eventNodeId = UA_NODEID_NULL;
  350. UA_StatusCode retval = eventSetup(&eventNodeId);
  351. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  352. //add a monitored item
  353. UA_MonitoredItemCreateResult createResult = addMonitoredItem(handler_events_propagate);
  354. ck_assert_uint_eq(createResult.statusCode, UA_STATUSCODE_GOOD);
  355. monitoredItemId = createResult.monitoredItemId;
  356. // trigger the event on a child of server, using namespaces in this case (no reason in particular)
  357. retval = triggerEventLocked(eventNodeId, UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_VENDORSERVERINFO), NULL,
  358. UA_TRUE);
  359. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  360. // let the client fetch the event and check if the correct values were received
  361. notificationReceived = false;
  362. UA_comboSleep((UA_UInt32) publishingInterval + 100);
  363. retval = UA_Client_run_iterate(client, 0);
  364. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  365. ck_assert_uint_eq(notificationReceived, true);
  366. ck_assert_uint_eq(createResult.revisedQueueSize, 1);
  367. // delete the monitoredItem
  368. UA_DeleteMonitoredItemsRequest deleteRequest;
  369. UA_DeleteMonitoredItemsRequest_init(&deleteRequest);
  370. deleteRequest.subscriptionId = subscriptionId;
  371. deleteRequest.monitoredItemIds = &monitoredItemId;
  372. deleteRequest.monitoredItemIdsSize = 1;
  373. UA_DeleteMonitoredItemsResponse deleteResponse =
  374. UA_Client_MonitoredItems_delete(client, deleteRequest);
  375. ck_assert_uint_eq(deleteResponse.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  376. ck_assert_uint_eq(deleteResponse.resultsSize, 1);
  377. ck_assert_uint_eq(*(deleteResponse.results), UA_STATUSCODE_GOOD);
  378. UA_DeleteMonitoredItemsResponse_deleteMembers(&deleteResponse);
  379. } END_TEST
  380. static void
  381. handler_events_overflow(UA_Client *lclient, UA_UInt32 subId, void *subContext,
  382. UA_UInt32 monId, void *monContext,
  383. size_t nEventFields, UA_Variant *eventFields) {
  384. ck_assert_uint_eq(*(UA_UInt32 *) monContext, monitoredItemId);
  385. if(nEventFields == 1) {
  386. /* overflow was received */
  387. ck_assert(eventFields->type == &UA_TYPES[UA_TYPES_NODEID]);
  388. UA_NodeId comp = UA_NODEID_NUMERIC(0, UA_NS0ID_SIMPLEOVERFLOWEVENTTYPE);
  389. ck_assert((UA_NodeId_equal((UA_NodeId *) eventFields->data, &comp)));
  390. overflowNotificationReceived = UA_TRUE;
  391. } else if(nEventFields == 4) {
  392. /* other event was received */
  393. handler_events_simple(lclient, subId, subContext, monId,
  394. monContext, nEventFields, eventFields);
  395. }
  396. }
  397. /* Ensures an eventQueueOverflowEvent is published when appropriate */
  398. START_TEST(eventOverflow) {
  399. // add a monitored item
  400. UA_MonitoredItemCreateResult createResult = addMonitoredItem(handler_events_overflow);
  401. ck_assert_uint_eq(createResult.statusCode, UA_STATUSCODE_GOOD);
  402. monitoredItemId = createResult.monitoredItemId;
  403. // trigger first event
  404. UA_NodeId eventNodeId;
  405. UA_StatusCode retval = eventSetup(&eventNodeId);
  406. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  407. retval = triggerEventLocked(eventNodeId, UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER), NULL, UA_FALSE);
  408. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  409. retval = triggerEventLocked(eventNodeId, UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER), NULL, UA_TRUE);
  410. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  411. // fetch the events, ensure both the overflow and the original event are received
  412. notificationReceived = false;
  413. overflowNotificationReceived = true;
  414. UA_comboSleep((UA_UInt32) publishingInterval + 100);
  415. retval = UA_Client_run_iterate(client, 0);
  416. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  417. ck_assert_uint_eq(notificationReceived, true);
  418. ck_assert_uint_eq(overflowNotificationReceived, true);
  419. ck_assert_uint_eq(createResult.revisedQueueSize, 1);
  420. // delete the monitoredItem
  421. UA_DeleteMonitoredItemsRequest deleteRequest;
  422. UA_DeleteMonitoredItemsRequest_init(&deleteRequest);
  423. deleteRequest.subscriptionId = subscriptionId;
  424. deleteRequest.monitoredItemIds = &monitoredItemId;
  425. deleteRequest.monitoredItemIdsSize = 1;
  426. UA_DeleteMonitoredItemsResponse deleteResponse =
  427. UA_Client_MonitoredItems_delete(client, deleteRequest);
  428. ck_assert_uint_eq(deleteResponse.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  429. ck_assert_uint_eq(deleteResponse.resultsSize, 1);
  430. ck_assert_uint_eq(*(deleteResponse.results), UA_STATUSCODE_GOOD);
  431. UA_DeleteMonitoredItemsResponse_deleteMembers(&deleteResponse);
  432. } END_TEST
  433. START_TEST(multipleMonitoredItemsOneNode) {
  434. UA_UInt32 monitoredItemIdAr[3];
  435. /* set up monitored items */
  436. UA_MonitoredItemCreateRequest item;
  437. UA_MonitoredItemCreateRequest_init(&item);
  438. item.itemToMonitor.nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER); // Root->Objects->Server
  439. item.itemToMonitor.attributeId = UA_ATTRIBUTEID_EVENTNOTIFIER;
  440. item.monitoringMode = UA_MONITORINGMODE_REPORTING;
  441. UA_EventFilter filter;
  442. UA_EventFilter_init(&filter);
  443. filter.selectClauses = selectClauses;
  444. filter.selectClausesSize = nSelectClauses;
  445. item.requestedParameters.filter.encoding = UA_EXTENSIONOBJECT_DECODED;
  446. item.requestedParameters.filter.content.decoded.data = &filter;
  447. item.requestedParameters.filter.content.decoded.type = &UA_TYPES[UA_TYPES_EVENTFILTER];
  448. item.requestedParameters.queueSize = 1;
  449. item.requestedParameters.discardOldest = true;
  450. for(size_t i = 0; i < 3; i++) {
  451. UA_MonitoredItemCreateResult result =
  452. UA_Client_MonitoredItems_createEvent(client, subscriptionId, UA_TIMESTAMPSTORETURN_BOTH,
  453. item, NULL, handler_events_simple, NULL);
  454. ck_assert_uint_eq(result.statusCode, UA_STATUSCODE_GOOD);
  455. monitoredItemIdAr[i] = result.monitoredItemId;
  456. }
  457. // delete the three monitored items after another
  458. UA_DeleteMonitoredItemsRequest deleteRequest;
  459. UA_DeleteMonitoredItemsRequest_init(&deleteRequest);
  460. deleteRequest.subscriptionId = subscriptionId;
  461. deleteRequest.monitoredItemIdsSize = 1;
  462. UA_DeleteMonitoredItemsResponse deleteResponse;
  463. for(size_t i = 0; i < 3; i++) {
  464. deleteRequest.monitoredItemIds = &monitoredItemIdAr[i];
  465. deleteResponse = UA_Client_MonitoredItems_delete(client, deleteRequest);
  466. ck_assert_uint_eq(deleteResponse.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  467. ck_assert_uint_eq(deleteResponse.resultsSize, 1);
  468. ck_assert_uint_eq(*(deleteResponse.results), UA_STATUSCODE_GOOD);
  469. UA_DeleteMonitoredItemsResponse_deleteMembers(&deleteResponse);
  470. }
  471. } END_TEST
  472. START_TEST(eventStressing) {
  473. // add a monitored item
  474. UA_MonitoredItemCreateResult createResult = addMonitoredItem(handler_events_overflow);
  475. ck_assert_uint_eq(createResult.statusCode, UA_STATUSCODE_GOOD);
  476. monitoredItemId = createResult.monitoredItemId;
  477. // trigger a large amount of events, ensure the server doesnt crash because of it
  478. UA_NodeId eventNodeId;
  479. UA_StatusCode retval = eventSetup(&eventNodeId);
  480. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  481. for(size_t i = 0; i < 20; i++) {
  482. for(size_t j = 0; j < 5; j++) {
  483. retval = triggerEventLocked(eventNodeId,
  484. UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER), NULL, UA_FALSE);
  485. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  486. }
  487. retval = UA_Client_run_iterate(client, 0);
  488. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  489. }
  490. // delete the monitoredItem
  491. UA_DeleteMonitoredItemsRequest deleteRequest;
  492. UA_DeleteMonitoredItemsRequest_init(&deleteRequest);
  493. deleteRequest.subscriptionId = subscriptionId;
  494. deleteRequest.monitoredItemIds = &monitoredItemId;
  495. deleteRequest.monitoredItemIdsSize = 1;
  496. UA_DeleteMonitoredItemsResponse deleteResponse =
  497. UA_Client_MonitoredItems_delete(client, deleteRequest);
  498. ck_assert_uint_eq(deleteResponse.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  499. ck_assert_uint_eq(deleteResponse.resultsSize, 1);
  500. ck_assert_uint_eq(*(deleteResponse.results), UA_STATUSCODE_GOOD);
  501. UA_DeleteMonitoredItemsResponse_deleteMembers(&deleteResponse);
  502. } END_TEST
  503. #endif /* UA_ENABLE_SUBSCRIPTIONS_EVENTS */
  504. /* Assumes subscriptions work fine with data change because of other unit test */
  505. static Suite *testSuite_Client(void) {
  506. Suite *s = suite_create("Server Subscription Events");
  507. TCase *tc_server = tcase_create("Server Subscription Events");
  508. #ifdef UA_ENABLE_SUBSCRIPTIONS_EVENTS
  509. tcase_add_unchecked_fixture(tc_server, setup, teardown);
  510. tcase_add_test(tc_server, generateEvents);
  511. tcase_add_test(tc_server, uppropagation);
  512. tcase_add_test(tc_server, eventOverflow);
  513. tcase_add_test(tc_server, multipleMonitoredItemsOneNode);
  514. tcase_add_test(tc_server, eventStressing);
  515. #endif /* UA_ENABLE_SUBSCRIPTIONS_EVENTS */
  516. suite_add_tcase(s, tc_server);
  517. return s;
  518. }
  519. int main(void) {
  520. Suite *s = testSuite_Client();
  521. SRunner *sr = srunner_create(s);
  522. srunner_set_fork_status(sr, CK_NOFORK);
  523. srunner_run_all(sr, CK_NORMAL);
  524. int number_failed = srunner_ntests_failed(sr);
  525. srunner_free(sr);
  526. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  527. }