Example #1
0
        public void WhenDnsFeatureStopped_ThenDnsServerSuccessfullyStops()
        {
            // Arrange.
            Mock <IDnsServer> dnsServer            = new Mock <IDnsServer>();
            Action <int, CancellationToken> action = (port, token) =>
            {
                while (true)
                {
                    token.ThrowIfCancellationRequested();
                    Thread.Sleep(50);
                }
            };

            dnsServer.Setup(s => s.ListenAsync(It.IsAny <int>(), It.IsAny <CancellationToken>())).Callback(action);

            Mock <IWhitelistManager> mockWhitelistManager = new Mock <IWhitelistManager>();
            IWhitelistManager        whitelistManager     = mockWhitelistManager.Object;

            CancellationTokenSource source       = new CancellationTokenSource();
            Mock <INodeLifetime>    nodeLifetime = new Mock <INodeLifetime>();

            nodeLifetime.Setup(n => n.StopApplication()).Callback(() => source.Cancel());
            nodeLifetime.Setup(n => n.ApplicationStopping).Returns(source.Token);
            INodeLifetime nodeLifetimeObject = nodeLifetime.Object;

            NodeSettings nodeSettings = NodeSettings.Default();

            nodeSettings.DataDir = Directory.GetCurrentDirectory();
            DataFolder dataFolder = CreateDataFolder(this);

            Mock <ILogger>        logger        = new Mock <ILogger>(MockBehavior.Loose);
            Mock <ILoggerFactory> loggerFactory = new Mock <ILoggerFactory>();

            loggerFactory.Setup <ILogger>(f => f.CreateLogger(It.IsAny <string>())).Returns(logger.Object);

            IAsyncLoopFactory asyncLoopFactory = new AsyncLoopFactory(loggerFactory.Object);

            // Act.
            DnsFeature feature = new DnsFeature(dnsServer.Object, whitelistManager, loggerFactory.Object, nodeLifetimeObject, DnsSettings.Load(nodeSettings), nodeSettings, dataFolder, asyncLoopFactory);

            feature.Initialize();
            nodeLifetimeObject.StopApplication();
            bool waited = source.Token.WaitHandle.WaitOne(5000);

            // Assert.
            feature.Should().NotBeNull();
            waited.Should().BeTrue();
            dnsServer.Verify(s => s.ListenAsync(It.IsAny <int>(), It.IsAny <CancellationToken>()), Times.Once);
        }
Example #2
0
        public void WhenDnsServerFailsToStart_ThenDnsFeatureRetries()
        {
            // Arrange.
            Mock <IDnsServer> dnsServer            = new Mock <IDnsServer>();
            Action <int, CancellationToken> action = (port, token) =>
            {
                throw new ArgumentException("Bad port");
            };

            dnsServer.Setup(s => s.ListenAsync(It.IsAny <int>(), It.IsAny <CancellationToken>())).Callback(action);

            Mock <IWhitelistManager> mockWhitelistManager = new Mock <IWhitelistManager>();
            IWhitelistManager        whitelistManager     = mockWhitelistManager.Object;

            CancellationTokenSource source       = new CancellationTokenSource(3000);
            Mock <INodeLifetime>    nodeLifetime = new Mock <INodeLifetime>();

            nodeLifetime.Setup(n => n.StopApplication()).Callback(() => source.Cancel());
            nodeLifetime.Setup(n => n.ApplicationStopping).Returns(source.Token);
            INodeLifetime nodeLifetimeObject = nodeLifetime.Object;

            NodeSettings nodeSettings = NodeSettings.Default();

            nodeSettings.DataDir = Directory.GetCurrentDirectory();
            DataFolder dataFolder = CreateDataFolder(this);

            Mock <ILogger> logger      = new Mock <ILogger>();
            bool           serverError = false;

            logger.Setup(l => l.Log(LogLevel.Error, It.IsAny <EventId>(), It.IsAny <FormattedLogValues>(), It.IsAny <Exception>(), It.IsAny <Func <object, Exception, string> >())).Callback <LogLevel, EventId, object, Exception, Func <object, Exception, string> >((level, id, state, e, f) => serverError = state.ToString().StartsWith("Failed whilst running the DNS server"));
            Mock <ILoggerFactory> loggerFactory = new Mock <ILoggerFactory>();

            loggerFactory.Setup <ILogger>(f => f.CreateLogger(It.IsAny <string>())).Returns(logger.Object);

            IAsyncLoopFactory asyncLoopFactory = new AsyncLoopFactory(loggerFactory.Object);

            // Act.
            DnsFeature feature = new DnsFeature(dnsServer.Object, whitelistManager, loggerFactory.Object, nodeLifetimeObject, DnsSettings.Load(nodeSettings), nodeSettings, dataFolder, asyncLoopFactory);

            feature.Initialize();
            bool waited = source.Token.WaitHandle.WaitOne(5000);

            // Assert.
            feature.Should().NotBeNull();
            waited.Should().BeTrue();
            dnsServer.Verify(s => s.ListenAsync(It.IsAny <int>(), It.IsAny <CancellationToken>()), Times.AtLeastOnce);
            serverError.Should().BeTrue();
        }
Example #3
0
        public void WhenConstructorCalled_AndAllParametersValid_ThenTypeCreated()
        {
            // Arrange.
            IDnsServer        dnsServer        = new Mock <IDnsServer>().Object;
            IWhitelistManager whitelistManager = new Mock <IWhitelistManager>().Object;
            INodeLifetime     nodeLifetime     = new Mock <INodeLifetime>().Object;
            NodeSettings      nodeSettings     = NodeSettings.Default();
            DataFolder        dataFolder       = CreateDataFolder(this);
            ILoggerFactory    loggerFactory    = new Mock <ILoggerFactory>().Object;
            IAsyncLoopFactory asyncLoopFactory = new Mock <IAsyncLoopFactory>().Object;

            // Act.
            DnsFeature feature = new DnsFeature(dnsServer, whitelistManager, loggerFactory, nodeLifetime, DnsSettings.Load(nodeSettings), nodeSettings, dataFolder, asyncLoopFactory);

            // Assert.
            feature.Should().NotBeNull();
        }
Example #4
0
        public void WhenInitialize_ThenRefreshLoopIsStarted()
        {
            // Arrange.
            Mock <IWhitelistManager> mockWhitelistManager = new Mock <IWhitelistManager>();

            mockWhitelistManager.Setup(w => w.RefreshWhitelist()).Verifiable("the RefreshWhitelist method should be called on the WhitelistManager");

            IWhitelistManager whitelistManager = mockWhitelistManager.Object;

            Mock <ILogger>        mockLogger        = new Mock <ILogger>();
            Mock <ILoggerFactory> mockLoggerFactory = new Mock <ILoggerFactory>();

            mockLoggerFactory.Setup(l => l.CreateLogger(It.IsAny <string>())).Returns(mockLogger.Object);
            ILoggerFactory loggerFactory = mockLoggerFactory.Object;

            IAsyncLoopFactory asyncLoopFactory = new AsyncLoopFactory(loggerFactory);
            INodeLifetime     nodeLifeTime     = new NodeLifetime();

            IDnsServer dnsServer = new Mock <IDnsServer>().Object;

            CancellationTokenSource source       = new CancellationTokenSource(3000);
            Mock <INodeLifetime>    nodeLifetime = new Mock <INodeLifetime>();

            nodeLifetime.Setup(n => n.StopApplication()).Callback(() => source.Cancel());
            nodeLifetime.Setup(n => n.ApplicationStopping).Returns(source.Token);
            INodeLifetime nodeLifetimeObject = nodeLifetime.Object;

            NodeSettings nodeSettings = NodeSettings.Default();

            nodeSettings.DataDir = Directory.GetCurrentDirectory();
            DataFolder dataFolder = CreateDataFolder(this);

            using (DnsFeature feature = new DnsFeature(dnsServer, whitelistManager, loggerFactory, nodeLifetimeObject, DnsSettings.Load(nodeSettings), nodeSettings, dataFolder, asyncLoopFactory))
            {
                // Act.
                feature.Initialize();
                bool waited = source.Token.WaitHandle.WaitOne(5000);

                // Assert.
                feature.Should().NotBeNull();
                waited.Should().BeTrue();
                mockWhitelistManager.Verify();
            }
        }
Example #5
0
        public void WhenDnsFeatureInitialized_ThenDnsServerSuccessfullyStarts()
        {
            // Arrange.
            Mock <IDnsServer>               dnsServer  = new Mock <IDnsServer>();
            ManualResetEventSlim            waitObject = new ManualResetEventSlim(false);
            Action <int, CancellationToken> action     = (port, token) =>
            {
                waitObject.Set();
                throw new OperationCanceledException();
            };

            dnsServer.Setup(s => s.ListenAsync(It.IsAny <int>(), It.IsAny <CancellationToken>())).Callback(action);

            Mock <IWhitelistManager> whitelistManager = new Mock <IWhitelistManager>();

            Mock <INodeLifetime> nodeLifetime = new Mock <INodeLifetime>();
            NodeSettings         nodeSettings = NodeSettings.Default();

            nodeSettings.DataDir = Directory.GetCurrentDirectory();
            DataFolder dataFolder = CreateDataFolder(this);

            Mock <ILogger>        logger        = new Mock <ILogger>(MockBehavior.Loose);
            Mock <ILoggerFactory> loggerFactory = new Mock <ILoggerFactory>();

            loggerFactory.Setup <ILogger>(f => f.CreateLogger(It.IsAny <string>())).Returns(logger.Object);

            IAsyncLoopFactory asyncLoopFactory = new AsyncLoopFactory(loggerFactory.Object);

            // Act.
            DnsFeature feature = new DnsFeature(dnsServer.Object, whitelistManager.Object, loggerFactory.Object, nodeLifetime.Object, DnsSettings.Load(nodeSettings), nodeSettings, dataFolder, asyncLoopFactory);

            feature.Initialize();
            bool waited = waitObject.Wait(5000);

            // Assert.
            feature.Should().NotBeNull();
            waited.Should().BeTrue();
            dnsServer.Verify(s => s.ListenAsync(It.IsAny <int>(), It.IsAny <CancellationToken>()), Times.Once);
        }