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);
            }
        }
Ejemplo n.º 2
0
        /// <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);
        }
 /// <summary>
 /// Add customer to private list of customers
 /// </summary>
 /// <param name="customer">Customer to add to list</param>
 public void AddCustomer(IStoreCustomer customer)
 {
     _customers.Add(customer);
 }
 public void PutCustomerInLine(IStoreCustomer customer)
 {
     _customersInLine.Enqueue(customer);
 }
        private void ServiceCustomer(IStoreCustomer customer, int timeOffset)
        {
            // perform register selection and get the selected register
            var selectedRegister = customer.RegisterSelection(_registers, _customers);

            // set the start time
            if (selectedRegister.GetNbrCustomersInLine().Equals(0))
                // start time is current time if line is empty
                customer.SetStartTime(timeOffset);
            else
                // otherwise start time is the end time of the last customer in line
                customer.SetStartTime(
                    selectedRegister.GetLastCustInLine()
                                    .GetCompletionTime(selectedRegister.IsTrainingRegister()));

            // put the customer in the queue
            selectedRegister.PutCustomerInLine(customer as StoreCustomer);
        }