/// <summary> /// Gets the dataObject from the URI and asserts. /// </summary> /// <param name="handler">The handler.</param> /// <param name="uri">The URI.</param> /// <param name="errorCode">The error code.</param> /// <returns>The ProtocolEventArgs.</returns> protected async Task <ProtocolEventArgs <Energistics.Etp.v11.Protocol.Store.Object> > GetAndAssert(IStoreCustomer handler, EtpUri uri, EtpErrorCodes?errorCode = null) { // Register event handler for root URI var onObject = HandleAsync <Energistics.Etp.v11.Protocol.Store.Object>(x => handler.OnObject += x); // Register exception hanlder var onProtocolException = HandleAsync <IProtocolException>(x => handler.OnProtocolException += x); // Send GetObject for non-existant URI handler.GetObject(uri); ProtocolEventArgs <Energistics.Etp.v11.Protocol.Store.Object> args = null; var tokenSource = new CancellationTokenSource(); var taskList = new List <Task>() { WaitFor(onObject, tokenSource.Token), WaitFor(onProtocolException, tokenSource.Token) }; // Start each event taskList.ForEach(task => task.Start()); // Wait for a task to finish await Task.WhenAny(taskList); // Cancel the rest of the task tokenSource.Cancel(); // Wait for the rest to be finished await Task.WhenAll(taskList); if (onObject.Status == TaskStatus.RanToCompletion) { args = onObject.Result; // Check for DataObject Assert.IsNotNull(args?.Message.DataObject); } if (onProtocolException.Status == TaskStatus.RanToCompletion) { var exceptionArgs = onProtocolException.Result; // Assert exception details Assert.IsNotNull(errorCode); Assert.IsNotNull(exceptionArgs?.Message); Assert.AreEqual((int)errorCode, exceptionArgs.Message.ErrorCode); } return(args); }