Exemple #1
0
        /// <summary>
        /// Waits for the given event and using given handler to check its contents.
        /// An assertion failure is raised on test site if expected event does not occur after timeout.
        /// </summary>
        /// <typeparam name="T">The type of the event handler.</typeparam>
        /// <param name="eventInfo">The event info of the expected event.</param>
        /// <param name="handler">The event handler</param>
        public void Expect <T>(EventInfo eventInfo, T handler)
        {
            if (eventInfo.EventHandlerType != typeof(T))
            {
                host.Assume(false, string.Format("passing wrong event handler type '{0}' to Expect method; expected type '{1}'",
                                                 typeof(T), eventInfo.EventHandlerType));
            }

            AvailableEvent entry;

            if (!queue.TryGet(timeout, false, out entry) || entry.Event != eventInfo)
            {
                host.Assert(false, string.Format("did not receive expected event '{0}' after waiting '{1}'; queue: {2}",
                                                 eventInfo.Name, timeout, DumpQueue()));
            }

            // call handler
            Delegate del = handler as Delegate;

            del.DynamicInvoke(entry.Parameters);

            // consume event
            queue.TryGet(timeout, true, out entry);
        }
        private int Expect <T, V>(TimeSpan timeout, bool failIfNone, Action <TimeSpan, V[]> FailAction, Func <T, V, bool> CompareAction, Action <V, T> ExpectCheckerAction, params V[] expected) where T : class
        {
            T availableObject = TryGetNext <T>(timeout, false);

            if (availableObject == null)
            {
                if (failIfNone)
                {
                    FailAction(timeout, expected);
                }
                return(-1);
            }

            List <List <TransactionEvent> > failedTransactions;

            if (failIfNone)
            {
                failedTransactions = new List <List <TransactionEvent> >();
            }
            else
            {
                failedTransactions = null;
            }
            int index = 0;

            foreach (V expectedObject in expected)
            {
                if (CompareAction(availableObject, expectedObject))
                {
                    BeginTransaction();
                    try
                    {
                        ExpectCheckerAction(expectedObject, availableObject);

                        EndTransaction(true);

                        Type serviceInterface = typeof(T);
                        if (serviceInterface.Equals(typeof(AvailableEvent)))
                        {
                            AvailableEvent dummy;
                            eventQueue.TryGet(TimeSpan.FromSeconds(0), true, out dummy);
                        }

                        if (serviceInterface.Equals(typeof(AvailableReturn)))
                        {
                            AvailableReturn dummy;
                            returnQueue.TryGet(TimeSpan.FromSeconds(0), true, out dummy);
                        }

                        return(index);
                    }
                    catch (TransactionFailedException)
                    {
                        if (failIfNone)
                        {
                            failedTransactions.Add(transactionEvents);
                        }
                        EndTransaction(false);
                    }
                }

                index++;
            }

            if (!failIfNone)
            {
                return(-1);
            }

            // build diagnosis
            StringBuilder diagnosis = new StringBuilder();

            index = 0;
            foreach (V expectedObject in expected)
            {
                if (!CompareAction(availableObject, expectedObject))
                {
                    diagnosis.AppendLine(String.Format("  {0}. {1} is not matching", index + 1, expectedObject.ToString()));
                }
                else
                {
                    List <TransactionEvent> t = failedTransactions[index++];
                    diagnosis.AppendLine(String.Format("  {0}. outputs do not match", index + 1));
                    Describe(diagnosis, "    ", t);
                }
                index++;
            }
            InternalAssert(false, String.Format("expected matching event, found '{0}'. Diagnosis:\r\n{1}", availableObject.ToString(), diagnosis.ToString()));
            return(-1);
        }