コード例 #1
0
        public void DoubleStartDoubleStop()
        {
            var checker = new TestHealthCheck();

            checker.CurrentHealthy = true;

            var  probe             = new TestHealthProbe(IPAddress.Any, 10034, TimeSpan.FromMilliseconds(1), checker);
            var  stopSuccess       = false;
            var  secondStopSuccess = false;
            bool secondStart       = false;
            var  startSuccess      = probe.StartAsync(CancellationToken.None).GetAwaiter().GetResult();

            if (startSuccess)
            {
                secondStart = probe.StartAsync(CancellationToken.None).GetAwaiter().GetResult();
                Task.Delay(2000).GetAwaiter().GetResult();
                stopSuccess       = probe.StopAsync(CancellationToken.None).GetAwaiter().GetResult();
                secondStopSuccess = probe.StopAsync(CancellationToken.None).GetAwaiter().GetResult();
            }

            Assert.IsTrue(startSuccess, "Start was not successful");
            Assert.IsTrue(stopSuccess, "Stop was not successful");
            Assert.IsFalse(secondStart, "Second start should be false");
            Assert.IsFalse(secondStopSuccess, "Second stop should be false");
        }
コード例 #2
0
        public void ClientConnect(int connectionCount, int interval)
        {
            const int Port = 10034;

            var checker = new TestHealthCheck();

            checker.CurrentHealthy = true;

            var probe        = new TestHealthProbe(IPAddress.Any, Port, TimeSpan.FromMilliseconds(1), checker);
            var stopSuccess  = false;
            var startSuccess = probe.StartAsync(CancellationToken.None).GetAwaiter().GetResult();
            var statusList   = new List <bool>();

            if (startSuccess)
            {
                var continueListening = true;
                var maximumTime       = connectionCount * (interval + 10);
                var timeoutAt         = DateTime.UtcNow.Add(TimeSpan.FromMilliseconds(maximumTime + 1000));

                var collectionThread = new Thread(new ThreadStart(() =>
                {
                    while (continueListening && statusList.Count < connectionCount)
                    {
                        var success = false;

                        try
                        {
                            using (var client = new TcpClient())
                            {
                                client.Connect(IPAddress.Loopback, Port);
                                success = true;
                                client.Close();
                            }
                        }
                        catch (Exception)
                        {
                        }

                        statusList.Add(success);

                        if (statusList.Count < connectionCount)
                        {
                            Thread.Sleep(interval);
                        }
                    }
                }));

                collectionThread.Start();

                while (DateTime.UtcNow < timeoutAt && collectionThread.IsAlive)
                {
                    Task.Delay(1000).GetAwaiter().GetResult();
                }

                continueListening = false;

                stopSuccess = probe.StopAsync(CancellationToken.None).GetAwaiter().GetResult();
            }

            Assert.IsTrue(startSuccess, "Start was not successful");
            Assert.IsTrue(stopSuccess, "Stop was not successful");
            Assert.IsTrue(connectionCount == statusList.Count(), "Not enough connections were performed");
            Assert.IsFalse(statusList.Any(item => !item), "Failure to connect found");
        }