/// <summary>
 /// TBD
 /// </summary>
 /// <param name="timeout">TBD</param>
 /// <param name="expectedOccurrences">TBD</param>
 /// <param name="matchedEventHandler">TBD</param>
 /// <returns>TBD</returns>
 protected bool AwaitDone(TimeSpan timeout, int?expectedOccurrences, MatchedEventHandler matchedEventHandler)
 {
     if (expectedOccurrences.HasValue)
     {
         var expected = expectedOccurrences.GetValueOrDefault();
         _testkit.AwaitConditionNoThrow(() => matchedEventHandler.ReceivedCount >= expected, timeout);
         return(matchedEventHandler.ReceivedCount == expected);
     }
     return(true);
 }
Exemple #2
0
 /// <summary>
 /// TBD
 /// </summary>
 /// <param name="timeout">TBD</param>
 /// <param name="expectedOccurrences">TBD</param>
 /// <param name="matchedEventHandler">TBD</param>
 /// <returns>TBD</returns>
 protected bool AwaitDone(TimeSpan timeout, int?expectedOccurrences, MatchedEventHandler matchedEventHandler)
 {
     if (expectedOccurrences.HasValue)
     {
         var expected = expectedOccurrences.GetValueOrDefault();
         if (expected > 0)
         {
             _testkit.AwaitConditionNoThrow(() => matchedEventHandler.ReceivedCount >= expected, timeout);
             return(matchedEventHandler.ReceivedCount == expected);
         }
         else
         {
             // if expecting no events to arrive - assert that given condition will never match
             var foundEvent = _testkit.AwaitConditionNoThrow(() => matchedEventHandler.ReceivedCount > 0, timeout);
             return(foundEvent == false);
         }
     }
     return(true);
 }
Exemple #3
0
        /// <summary>
        /// Async version of <see cref="Intercept{T}"/>
        /// </summary>
        protected async Task <T> InterceptAsync <T>(Func <Task <T> > func, ActorSystem system, TimeSpan?timeout, int?expectedOccurrences, MatchedEventHandler matchedEventHandler = null)
        {
            var leeway = system.HasExtension <TestKitSettings>()
                ? TestKitExtension.For(system).TestEventFilterLeeway
                : _testkit.TestKitSettings.TestEventFilterLeeway;

            var timeoutValue = timeout.HasValue ? _testkit.Dilated(timeout.Value) : leeway;

            matchedEventHandler = matchedEventHandler ?? new MatchedEventHandler();
            system.EventStream.Publish(new Mute(_filters));
            try
            {
                foreach (var filter in _filters)
                {
                    filter.EventMatched += matchedEventHandler.HandleEvent;
                }
                var result = await func();

                if (!await AwaitDoneAsync(timeoutValue, expectedOccurrences, matchedEventHandler))
                {
                    var    actualNumberOfEvents = matchedEventHandler.ReceivedCount;
                    string msg;
                    if (expectedOccurrences.HasValue)
                    {
                        var expectedNumberOfEvents = expectedOccurrences.Value;
                        if (actualNumberOfEvents < expectedNumberOfEvents)
                        {
                            msg = string.Format("Timeout ({0}) while waiting for messages. Only received {1}/{2} messages that matched filter [{3}]", timeoutValue, actualNumberOfEvents, expectedNumberOfEvents, string.Join(",", _filters));
                        }
                        else
                        {
                            var tooMany = actualNumberOfEvents - expectedNumberOfEvents;
                            msg = string.Format("Received {0} {1} too many. Expected {2} {3} but received {4} that matched filter [{5}]", tooMany, GetMessageString(tooMany), expectedNumberOfEvents, GetMessageString(expectedNumberOfEvents), actualNumberOfEvents, string.Join(",", _filters));
                        }
                    }
                    else
                    {
                        msg = string.Format("Timeout ({0}) while waiting for messages that matched filter [{1}]", timeoutValue, _filters);
                    }

                    var assertionsProvider = system.HasExtension <TestKitAssertionsProvider>()
                        ? TestKitAssertionsExtension.For(system)
                        : TestKitAssertionsExtension.For(_testkit.Sys);
                    assertionsProvider.Assertions.Fail(msg);
                }
                return(result);
            }
            finally
            {
                foreach (var filter in _filters)
                {
                    filter.EventMatched -= matchedEventHandler.HandleEvent;
                }
                system.EventStream.Publish(new Unmute(_filters));
            }
        }
Exemple #4
0
 /// <summary>
 /// Async version of <see cref="Intercept{T}"/>
 /// </summary>
 protected Task <T> InterceptAsync <T>(Func <T> func, ActorSystem system, TimeSpan?timeout, int?expectedOccurrences, MatchedEventHandler matchedEventHandler = null)
 {
     return(InterceptAsync(() => Task.FromResult(func()), system, timeout, expectedOccurrences, matchedEventHandler));
 }