Exemplo n.º 1
0
        public void Publish_NullGetMessage_ThrowsArgumentNullException()
        {
            var container = ContainerFactory.Bootstrap <EmptyServicesBootstrapper>();
            var bus       = new Bus(container);

            Assert.Throws <ArgumentNullException>(() => bus.PublishAsync((Func <string>)null));
        }
Exemplo n.º 2
0
        public void Publish_NullMessageReturnedFromGetMessageButNoSubscribers_DoesNotThrow()
        {
            var container = ContainerFactory.Bootstrap <EmptyServicesBootstrapper>();
            var bus       = new Bus(container);

            Assert.DoesNotThrow(() => bus.PublishAsync(() => (string)null));
        }
Exemplo n.º 3
0
        public void Publish_ActualMessageButNoSubscribers_DoesntThrownException()
        {
            var container = ContainerFactory.Bootstrap <EmptyServicesBootstrapper>();
            var bus       = new Bus(container);

            Assert.DoesNotThrow(() => bus.PublishAsync("test"));
        }
Exemplo n.º 4
0
        public void DeserializeJson_WhenResourceDoesNotExist_ThrowsMissingManifestResourceException()
        {
            IContainer container        = ContainerFactory.Bootstrap <ServicesBootstrapper>();
            var        resourcesFactory = container.Resolve <IResourcesFactory>();
            var        resources        = resourcesFactory.GetResources <ResourcesTests>();

            Assert.Throws <MissingManifestResourceException>(() => resources.DeserializeJson <TestObject>("Resources.MissingFile.json"));
        }
        public void Bootstrap_SameBootstrapperRegisteredTwice_OnlyCallsBootstrapOnce()
        {
            var container = ContainerFactory.Bootstrap <DummyBootstrapper>();

            container.Bootstrap <DummyBootstrapper>();

            Assert.That(DummyBootstrapper.BootstrapCallCounter, Is.EqualTo(1));
        }
Exemplo n.º 6
0
        public void Register_WithContainer_RegistersClock()
        {
            var bootstrapper = new ServicesBootstrapper();
            var container    = ContainerFactory.Bootstrap <EmptyServicesBootstrapper>();

            bootstrapper.Bootstrap(container);

            Assert.That(container.Resolve <IClock>(), Is.Not.Null);
        }
        public void TryGetValue_ExistingVariables_ReturnsSuccessTrue(string key)
        {
            var container = ContainerFactory.Bootstrap <ServicesBootstrapper>();
            var scv       = container.Resolve <IEnumerable <IConfigurationVariables> >().OfType <SystemConfigurationVariables>().First();

            var(success, _) = scv.TryGetValue(key);

            Assert.That(success, Is.True);
        }
Exemplo n.º 8
0
        public static async Task <int> RunAsync <T>()
            where T : class, IServicesBootstrapper
        {
            await Task.Yield();

            var container = ContainerFactory.Bootstrap <ServicesBootstrapper, T>();

            return(await container.New <TrayApp>().NotNull().RunAsync());
        }
Exemplo n.º 9
0
        public void NameOf_SmokeTests(Type type, NameOfTypeOptions options, string expected)
        {
            var container  = ContainerFactory.Bootstrap <ServicesBootstrapper>();
            var typeHelper = container.Resolve <ITypeHelper>();

            string output = typeHelper.TryGetNameOf(type, options);

            Assert.That(output, Is.EqualTo(expected));
        }
Exemplo n.º 10
0
        public void DeserializeJson_WhenResourceExists_ReturnsDeserializedObject()
        {
            IContainer container        = ContainerFactory.Bootstrap <ServicesBootstrapper>();
            var        resourcesFactory = container.Resolve <IResourcesFactory>();
            var        resources        = resourcesFactory.GetResources <ResourcesTests>();

            var obj = resources.DeserializeJson <TestObject>("Resources.ExistingFile.json");

            Assert.That(obj.Value, Is.EqualTo(42));
        }
Exemplo n.º 11
0
        public async Task Publish_GetMessageWhenNoSubscribers_IsNotCalled()
        {
            var container  = ContainerFactory.Bootstrap <EmptyServicesBootstrapper>();
            var bus        = new Bus(container);
            var getMessage = Substitute.For <Func <string> >();

            await bus.PublishAsync(getMessage);

            getMessage.DidNotReceive().Invoke();
        }
Exemplo n.º 12
0
        public void Publish_NullMessageReturnedFromGetMessage_ThrowsInvalidOperationException()
        {
            var container  = ContainerFactory.Bootstrap <EmptyServicesBootstrapper>();
            var bus        = new Bus(container);
            var subscriber = Substitute.For <ISubscriber <string> >();

            bus.Subscribe(subscriber);

            Assert.Throws <InvalidOperationException>(() => bus.PublishAsync(() => (string)null));
        }
Exemplo n.º 13
0
        protected void VerifyDependency(Type servicesBootstrapperType)
        {
            var container = ContainerFactory.Bootstrap <T>();

            var  register     = container.Resolve <IServicesBootstrapperRegister>();
            bool isRegistered = register.IsRegistered(servicesBootstrapperType);

            Assert.That(
                isRegistered, Is.True, $"Dependency on assembly {servicesBootstrapperType.Assembly.GetName().Name} but services bootstrapper not called");
        }
Exemplo n.º 14
0
        public IServiceContainer CreateApplicationContainer()
        {
            IServiceContainer container = new ContainerFactory().Create();

            container.Bootstrap <ServicesBootstrapper>()
            .Bootstrap <Services.Core.ServicesBootstrapper>()
            .Bootstrap <Framework.ECS.ServicesBootstrapper>()
            .Bootstrap <Game.Core.ServicesBootstrapper>();

            return(container);
        }
Exemplo n.º 15
0
        public static async Task <int> RunAsync <T>(bool useBackgroundServices)
            where T : class, IServicesBootstrapper
        {
            ConsoleAppConfigurator.Configure();

            IConsoleApplicationEntryPoint entryPoint;
            IBackgroundServicesManager    backgroundServicesManager;
            IApplicationLifetimeManager   applicationLifetimeManager;

            try
            {
                var container = ContainerFactory.Bootstrap <ServicesBootstrapper, T>();

                backgroundServicesManager = container.Resolve <IBackgroundServicesManager>().NotNull();
                entryPoint = container.Resolve <IConsoleApplicationEntryPoint>().NotNull();
                applicationLifetimeManager = container.Resolve <IApplicationLifetimeManager>().NotNull();
            }
            catch (Exception ex) when(!Debugger.IsAttached)
            {
                OutputExceptionToConsole(ex);
                return(1);
            }

            try
            {
                if (useBackgroundServices)
                {
                    backgroundServicesManager.StartBackgroundServices();
                }
                try
                {
                    return(await entryPoint.RunAsync().NotNull());
                }
                finally
                {
                    applicationLifetimeManager.SignalGracefulTermination();

                    if (useBackgroundServices)
                    {
                        await backgroundServicesManager.WaitForBackgroundServicesToStop();
                    }
                }
            }
            catch (TaskCanceledException)
            {
                throw;
            }
            catch (Exception ex) when(!Debugger.IsAttached)
            {
                OutputExceptionToConsole(ex);
                return(1);
            }
        }
Exemplo n.º 16
0
        public async Task Publish_OneSubscriberThatHasBeenUnsubscribed_DoesNotCallThatSubscriber()
        {
            var    container = ContainerFactory.Bootstrap <EmptyServicesBootstrapper>();
            var    bus       = new Bus(container);
            string message   = null;

            bus.Subscribe <string>(m => message = m).Dispose();

            await bus.PublishAsync("Test");

            Assert.That(message, Is.Null);
        }
Exemplo n.º 17
0
        public async Task Publish_OneSubscriber_CallsThatSubscriber()
        {
            var    container = ContainerFactory.Bootstrap <EmptyServicesBootstrapper>();
            var    bus       = new Bus(container);
            string message   = null;

            bus.Subscribe <string>(m => message = m);

            await bus.PublishAsync("Test");

            Assert.That(message, Is.EqualTo("Test"));
        }
Exemplo n.º 18
0
        public void Convert_SmokeTests()
        {
            var container = ContainerFactory.Bootstrap <ServicesBootstrapper>();

            var valueConverter = container.Resolve <IValueConverter>();

            assume(valueConverter != null);

            Assert.That(valueConverter.Convert <int, uint>(10), Is.EqualTo(10));
            Assert.That(valueConverter.Convert <uint, int>(10), Is.EqualTo(10));
            Assert.That(valueConverter.Convert <int, string>(10), Is.EqualTo("10"));
            Assert.That(valueConverter.Convert <int, int>(10), Is.EqualTo(10));
        }
Exemplo n.º 19
0
        public async Task Publish_GetMessageWhenSubscribers_IsCalled()
        {
            var container  = ContainerFactory.Bootstrap <EmptyServicesBootstrapper>();
            var bus        = new Bus(container);
            var getMessage = Substitute.For <Func <string> >();
            var subscriber = Substitute.For <ISubscriber <string> >();

            getMessage.Invoke().Returns("Message");
            bus.Subscribe(subscriber);

            await bus.PublishAsync(getMessage);

            getMessage.Received().Invoke();
            subscriber.Received().Notify("Message");
        }
Exemplo n.º 20
0
        public async Task Publish_SubscriberThatHasBeenCollected_DoesNotCallThatSubscriber()
        {
            var container = ContainerFactory.Bootstrap <EmptyServicesBootstrapper>();
            var bus       = new Bus(container);

            Subscriber.LastMessage = null;
            Subscribe(bus);

            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();

            await bus.PublishAsync("Test");

            Assert.That(Subscriber.LastMessage, Is.Null);
        }
Exemplo n.º 21
0
        public async Task Publish_SubscriberThatHasNotBeenCollectedThroughSubscription_CallsThatSubscriber()
        {
            var container = ContainerFactory.Bootstrap <EmptyServicesBootstrapper>();
            var bus       = new Bus(container);

            Subscriber.LastMessage = null;
            var subscription = SubscribeAndReturnSubscription(bus);

            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();

            await bus.PublishAsync("Test");

            Assert.That(Subscriber.LastMessage, Is.EqualTo("Test"));
            GC.KeepAlive(subscription);
        }
Exemplo n.º 22
0
        public static void RunWpfMainWindow <T>()
            where T : class, IServicesBootstrapper
        {
            var container = ContainerFactory.Bootstrap <ServicesBootstrapper <T> >();

            try
            {
                var lifetimeManager = container.Resolve <IWpfApplicationLifetimeManager>().NotNull();
                lifetimeManager.Start();

                var window = container.Resolve <IApplicationEntryPointWindow>().NotNull();
                window.Show();
            }
            catch (Exception) when(!Debugger.IsAttached)
            {
                Environment.ExitCode = 1;
                throw;
            }
        }
Exemplo n.º 23
0
        public static void RunWinFormsMainWindow <T>(bool useBackgroundServices)
            where T : class, IServicesBootstrapper
        {
            var container = ContainerFactory.Bootstrap <ServicesBootstrapper <T> >();

            IBackgroundServicesManager  backgroundServicesManager  = container.Resolve <IBackgroundServicesManager>().NotNull();
            IApplicationLifetimeManager applicationLifetimeManager = container.Resolve <IApplicationLifetimeManager>();

            if (useBackgroundServices)
            {
                backgroundServicesManager.StartBackgroundServices();
            }

            try
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);

                Form mainForm = container.Resolve <Form>(serviceKey: "MainForm");

                bool closingHasBeenHandled = true;
                applicationLifetimeManager.GracefulTerminationCancellationToken.Register(
                    () =>
                {
                    if (closingHasBeenHandled)
                    {
                        return;
                    }

                    closingHasBeenHandled = true;
                    var close             = new Action(() => mainForm.Close());
                    if (mainForm.InvokeRequired)
                    {
                        mainForm.BeginInvoke(close);
                    }
                    else
                    {
                        close();
                    }
                });

                mainForm.FormClosed += (s, e) =>
                {
                    closingHasBeenHandled = true;
                    applicationLifetimeManager.SignalGracefulTermination();
                };

                Application.Run(mainForm);
            }
            catch (Exception) when(!Debugger.IsAttached)
            {
                Environment.ExitCode = 1;
                throw;
            }
            finally
            {
                if (useBackgroundServices)
                {
                    backgroundServicesManager.WaitForBackgroundServicesToStop().GetAwaiter().GetResult();
                }
            }
        }