示例#1
0
        private TestKitBase(TestKitAssertions assertions, ActorSystem system, Config config, string actorSystemName = null)
        {
            if (assertions == null)
            {
                throw new ArgumentNullException("assertions");
            }
            if (system == null)
            {
                var configWithDefaultFallback = config.SafeWithFallback(_defaultConfig);
                system = ActorSystem.Create(actorSystemName ?? "test", configWithDefaultFallback);
            }

            _assertions = assertions;
            _system     = system;
            system.RegisterExtension(new TestKitExtension());
            system.RegisterExtension(new TestKitAssertionsExtension(assertions));
            _testKitSettings = TestKitExtension.For(_system);
            _queue           = new BlockingQueue <MessageEnvelope>();
            _log             = Logging.GetLogger(system, GetType());


            var testActor = CreateTestActor(system, "testActor" + _testActorId.IncrementAndGet());

            _testActor = testActor;
            //Wait for the testactor to start
            AwaitCondition(() =>
            {
                var repRef = _testActor as RepointableRef;
                return(repRef == null || repRef.IsStarted);
            }, TimeSpan.FromSeconds(1), TimeSpan.FromMilliseconds(10));
        }
示例#2
0
        public T Intercept <T>(ActorSystem system, Func <T> func)
        {
            system.EventStream.Publish(new Mute(this));
            var testKitSettings = TestKitExtension.For(system);
            var leeway          = testKitSettings.TestEventFilterLeeway;

            try
            {
                var result = func();

                if (!AwaitDone(leeway))
                {
                    var msg = Occurrences > 0
                        ? string.Format("Timeout ({0}) waiting for {1} messages on {2}", leeway, Occurrences, this)
                        : string.Format("Received {0} messages too many on {1}", -Occurrences, this);

                    var testKitAssertionsProvider = TestKitAssertionsExtension.For(system);
                    testKitAssertionsProvider.Assertions.Fail(msg);
                }
                return(result);
            }
            finally
            {
                system.EventStream.Publish(new Unmute(this));
            }
        }
示例#3
0
        /// <summary>
        /// Initializes the <see cref="TestState"/> for a new spec.
        /// </summary>
        /// <param name="system">The actor system this test will use. Can be null.</param>
        /// <param name="config">The configuration that <paramref name="system"/> will use if it's null.</param>
        /// <param name="actorSystemName">The name that <paramref name="system"/> will use if it's null.</param>
        /// <param name="testActorName">The name of the test actor. Can be null.</param>
        protected void InitializeTest(ActorSystem system, Config config, string actorSystemName, string testActorName)
        {
            _testState = new TestState();

            if (system == null)
            {
                var configWithDefaultFallback = config.SafeWithFallback(_defaultConfig);
                system = ActorSystem.Create(actorSystemName ?? "test", configWithDefaultFallback);
            }

            _testState.System = system;

            system.RegisterExtension(new TestKitExtension());
            system.RegisterExtension(new TestKitAssertionsExtension(_assertions));

            _testState.TestKitSettings    = TestKitExtension.For(_testState.System);
            _testState.Queue              = new BlockingQueue <MessageEnvelope>();
            _testState.Log                = Logging.GetLogger(system, GetType());
            _testState.EventFilterFactory = new EventFilterFactory(this);

            //register the CallingThreadDispatcherConfigurator
            _testState.System.Dispatchers.RegisterConfigurator(CallingThreadDispatcher.Id,
                                                               new CallingThreadDispatcherConfigurator(_testState.System.Settings.Config, _testState.System.Dispatchers.Prerequisites));

            if (string.IsNullOrEmpty(testActorName))
            {
                testActorName = "testActor" + _testActorId.IncrementAndGet();
            }

            var testActor = CreateTestActor(system, testActorName);

            //Wait for the testactor to start
            // Calling sync version here, since .Wait() causes deadlock
            AwaitCondition(() =>
            {
                var repRef = testActor as IRepointableRef;
                return(repRef == null || repRef.IsStarted);
            }, TimeSpan.FromSeconds(5), TimeSpan.FromMilliseconds(10));

            if (!(this is INoImplicitSender))
            {
                InternalCurrentActorCellKeeper.Current = (ActorCell)((ActorRefWithCell)testActor).Underlying;
            }
            else if (!(this is TestProbe))
            //HACK: we need to clear the current context when running a No Implicit Sender test as sender from an async test may leak
            //but we should not clear the current context when creating a testprobe from a test
            {
                InternalCurrentActorCellKeeper.Current = null;
            }
            SynchronizationContext.SetSynchronizationContext(
                new ActorCellKeepingSynchronizationContext(InternalCurrentActorCellKeeper.Current));

            _testState.TestActor = testActor;
        }
示例#4
0
        private TestKitBase(ITestKitAssertions assertions, ActorSystem system, Config config, string actorSystemName, string testActorName)
        {
            if (assertions == null)
            {
                throw new ArgumentNullException("assertions");
            }
            if (system == null)
            {
                var configWithDefaultFallback = config.SafeWithFallback(_defaultConfig);
                system = ActorSystem.Create(actorSystemName ?? "test", configWithDefaultFallback);
            }

            _assertions = assertions;
            _system     = system;
            system.RegisterExtension(new TestKitExtension());
            system.RegisterExtension(new TestKitAssertionsExtension(assertions));
            _testKitSettings    = TestKitExtension.For(_system);
            _queue              = new BlockingQueue <MessageEnvelope>();
            _log                = Logging.GetLogger(system, GetType());
            _eventFilterFactory = new EventFilterFactory(this);

            //register the CallingThreadDispatcherConfigurator
            _system.Dispatchers.RegisterConfigurator(CallingThreadDispatcher.Id,
                                                     new CallingThreadDispatcherConfigurator(_system.Settings.Config, _system.Dispatchers.Prerequisites));

            if (string.IsNullOrEmpty(testActorName))
            {
                testActorName = "testActor" + _testActorId.IncrementAndGet();
            }

            var testActor = CreateTestActor(system, testActorName);

            //Wait for the testactor to start
            AwaitCondition(() =>
            {
                var repRef = testActor as IRepointableRef;
                return(repRef == null || repRef.IsStarted);
            }, TimeSpan.FromSeconds(5), TimeSpan.FromMilliseconds(10));

            if (!(this is INoImplicitSender))
            {
                InternalCurrentActorCellKeeper.Current = (ActorCell)((ActorRefWithCell)testActor).Underlying;
            }
            SynchronizationContext.SetSynchronizationContext(
                new ActorCellKeepingSynchronizationContext(InternalCurrentActorCellKeeper.Current));
            _testActor = testActor;
        }