Exemplo n.º 1
0
        /// <summary>
        /// Shortcut for creating a new actor system with the specified name and settings.
        /// </summary>
        /// <param name="name">The name of the actor system to create. The name must be uri friendly.
        /// <remarks>Must contain only word characters (i.e. [a-zA-Z0-9] plus non-leading '-'</remarks>
        /// </param>
        /// <param name="setup">The bootstrap setup used to help programmatically initialize the <see cref="ActorSystem"/>.</param>
        /// <returns>A newly created actor system with the given name and configuration.</returns>
        public static ActorSystem Create(string name, ActorSystemSetup setup)
        {
            var bootstrapSetup = setup.Get <BootstrapSetup>();
            var appConfig      = bootstrapSetup.FlatSelect(_ => _.Config).GetOrElse(ConfigurationFactory.Load());

            return(CreateAndStartSystem(name, appConfig, setup));
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Loads from embedded resources actor system persistence configuration with <see cref="TestJournal"/> and
        ///     <see cref="TestSnapshotStore"/> configured as default persistence plugins.
        /// </summary>
        /// <param name="customConfig">Custom configuration that was passed in the constructor.</param>
        /// <returns>Actor system configuration object.</returns>
        /// <seealso cref="Config"/>
        private static ActorSystemSetup GetConfig(ActorSystemSetup customConfig)
        {
            var bootstrapSetup = customConfig.Get <BootstrapSetup>();
            var config         = bootstrapSetup.FlatSelect(x => x.Config);
            var actorProvider  = bootstrapSetup.FlatSelect(x => x.ActorRefProvider);
            var newSetup       = BootstrapSetup.Create();

            if (config.HasValue)
            {
                newSetup = newSetup.WithConfig(GetConfig(config.Value));
            }
            else
            {
                newSetup = newSetup.WithConfig(GetConfig(Config.Empty));
            }

            if (actorProvider.HasValue)
            {
                newSetup = newSetup.WithActorRefProvider(actorProvider.Value);
            }

            return(customConfig.WithSetup(newSetup));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Settings" /> class.
        /// </summary>
        /// <param name="system">The system.</param>
        /// <param name="config">The configuration.</param>
        /// <param name="setup">The setup class used to help bootstrap the <see cref="ActorSystem"/></param>
        /// <exception cref="ConfigurationException">
        /// This exception is thrown if the 'akka.actor.provider' configuration item is not a valid type name or a valid actor ref provider.
        /// </exception>
        public Settings(ActorSystem system, Config config, ActorSystemSetup setup)
        {
            Setup           = setup;
            _userConfig     = config;
            _fallbackConfig = ConfigurationFactory.Default();
            RebuildConfig();

            System = system;

            var providerSelectionSetup = Setup.Get <BootstrapSetup>()
                                         .FlatSelect(_ => _.ActorRefProvider)
                                         .Select(_ => _.Fqn)
                                         .GetOrElse(Config.GetString("akka.actor.provider", null));

            ProviderSelectionType = ProviderSelection.GetProvider(providerSelectionSetup);

            ConfigVersion = Config.GetString("akka.version", null);
            ProviderClass = ProviderSelectionType.Fqn;
            HasCluster    = ProviderSelectionType.HasCluster;

            var providerType = Type.GetType(ProviderClass);

            if (providerType == null)
            {
                throw new ConfigurationException($"'akka.actor.provider' is not a valid type name : '{ProviderClass}'");
            }
            if (!typeof(IActorRefProvider).IsAssignableFrom(providerType))
            {
                throw new ConfigurationException($"'akka.actor.provider' is not a valid actor ref provider: '{ProviderClass}'");
            }

            SupervisorStrategyClass = Config.GetString("akka.actor.guardian-supervisor-strategy", null);

            AskTimeout           = Config.GetTimeSpan("akka.actor.ask-timeout", null, allowInfinite: true);
            CreationTimeout      = Config.GetTimeSpan("akka.actor.creation-timeout", null);
            UnstartedPushTimeout = Config.GetTimeSpan("akka.actor.unstarted-push-timeout", null);

            SerializeAllMessages = Config.GetBoolean("akka.actor.serialize-messages", false);
            SerializeAllCreators = Config.GetBoolean("akka.actor.serialize-creators", false);

            LogLevel           = Config.GetString("akka.loglevel", null);
            StdoutLogLevel     = Config.GetString("akka.stdout-loglevel", null);
            Loggers            = Config.GetStringList("akka.loggers", new string[] { });
            LoggersDispatcher  = Config.GetString("akka.loggers-dispatcher", null);
            LoggerStartTimeout = Config.GetTimeSpan("akka.logger-startup-timeout", null);
            LoggerAsyncStart   = Config.GetBoolean("akka.logger-async-start", false);

            //handled
            LogConfigOnStart = Config.GetBoolean("akka.log-config-on-start", false);
            LogDeadLetters   = 0;
            switch (Config.GetString("akka.log-dead-letters", null))
            {
            case "on":
            case "true":
            case "yes":
                LogDeadLetters = int.MaxValue;
                break;

            case "off":
            case "false":
            case "no":
                LogDeadLetters = 0;
                break;

            default:
                LogDeadLetters = Config.GetInt("akka.log-dead-letters", 0);
                break;
            }
            LogDeadLettersDuringShutdown = Config.GetBoolean("akka.log-dead-letters-during-shutdown", false);
            AddLoggingReceive            = Config.GetBoolean("akka.actor.debug.receive", false);
            DebugAutoReceive             = Config.GetBoolean("akka.actor.debug.autoreceive", false);
            DebugLifecycle              = Config.GetBoolean("akka.actor.debug.lifecycle", false);
            FsmDebugEvent               = Config.GetBoolean("akka.actor.debug.fsm", false);
            DebugEventStream            = Config.GetBoolean("akka.actor.debug.event-stream", false);
            DebugUnhandledMessage       = Config.GetBoolean("akka.actor.debug.unhandled", false);
            DebugRouterMisconfiguration = Config.GetBoolean("akka.actor.debug.router-misconfiguration", false);
            Home = Config.GetString("akka.home", "");
            DefaultVirtualNodesFactor = Config.GetInt("akka.actor.deployment.default.virtual-nodes-factor", 0);

            SchedulerClass           = Config.GetString("akka.scheduler.implementation", null);
            SchedulerShutdownTimeout = Config.GetTimeSpan("akka.scheduler.shutdown-timeout", null);

            CoordinatedShutdownTerminateActorSystem      = Config.GetBoolean("akka.coordinated-shutdown.terminate-actor-system");
            CoordinatedShutdownRunByActorSystemTerminate = Config.GetBoolean("akka.coordinated-shutdown.run-by-actor-system-terminate");

            if (CoordinatedShutdownRunByActorSystemTerminate && !CoordinatedShutdownTerminateActorSystem)
            {
                throw new ConfigurationException(
                          "akka.coordinated-shutdown.run-by-actor-system-terminate=on and " +
                          "akka.coordinated-shutdown.terminate-actor-system=off is not a supported configuration combination.");
            }
        }
Exemplo n.º 4
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, ActorSystemSetup config, string actorSystemName, string testActorName)
        {
            _testState = new TestState();

            if (system == null)
            {
                var bootstrap = config.Get <BootstrapSetup>();
                var configWithDefaultFallback = bootstrap.HasValue
                    ? bootstrap.Value.Config.Select(c => c == _defaultConfig ? c : c.WithFallback(_defaultConfig))
                    : _defaultConfig;

                var newBootstrap = BootstrapSetup.Create().WithConfig(
                    configWithDefaultFallback.HasValue
                        ? configWithDefaultFallback.Value
                        : _defaultConfig);
                if (bootstrap.FlatSelect(x => x.ActorRefProvider).HasValue)
                {
                    newBootstrap =
                        newBootstrap.WithActorRefProvider(bootstrap.FlatSelect(x => x.ActorRefProvider).Value);
                }
                system = ActorSystem.Create(actorSystemName ?? "test", config.WithSetup(newBootstrap));
            }

            _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(() =>
            {
                return(!(testActor is IRepointableRef repRef) || 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;
        }