Ejemplo n.º 1
0
        /// <summary>
        /// Puts the dataObject and asserts.
        /// </summary>
        /// <param name="handler">The handler.</param>
        /// <param name="dataObject">The data object.</param>
        /// <param name="errorCode">The error code.</param>
        /// <returns>The task.</returns>
        protected async Task PutAndAssert(IStoreCustomer handler, DataObject dataObject, 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 PutObject message for new data object
            var messageId = handler.PutObject(dataObject);

            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);

            // Check error code
            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);
            }
            // Check for valid acknowledgement
            else
            {
                var acknowledge = onAcknowledge.Result;

                // Assert acknowledgement and messageId
                VerifyCorrelationId(acknowledge, messageId);
            }
        }