Exemplo n.º 1
0
        /// <summary>
        /// Deletes 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 DeleteAndAssert(IStoreCustomer handler, EtpUri uri, EtpErrorCodes?errorCode = null)
        {
            // Register event handler for Acknowledge response
            var onAcknowledge = HandleAsync <IAcknowledge>(x => handler.OnAcknowledge += x);

            // Register exception hanlder
            var onProtocolException = HandleAsync <IProtocolException>(x => handler.OnProtocolException += x);

            // Send GetObject for non-existant URI
            var messageId = handler.DeleteObject(uri);

            var tokenSource = new CancellationTokenSource();

            var taskList = new List <Task>
            {
                WaitFor(onAcknowledge, 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 (onAcknowledge.Status == TaskStatus.RanToCompletion)
            {
                var acknowledge = onAcknowledge.Result;

                // Assert acknowledgement and messageId
                VerifyCorrelationId(acknowledge, messageId);
            }
            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);
            }
        }