Example #1
0
        public void BuildEnvironmentMetadata()
        {
            Mock.Arrange(() => _ipAddressByServerName.GetEnumerator()).Returns(_ipAddressByServerNameEnumerator);
            Mock.Arrange(() => _ipAddressByServerNameEnumerator.MoveNext()).Returns(true);
            Mock.Arrange(() => _ipAddressByServerNameEnumerator.Current).Returns(_keyValuePair);
            Mock.Arrange(() => _configurationMetadata.GetIpAddressByServerName()).Returns(_ipAddressByServerName);
            Mock.Arrange(() => _pingCreator.CreatePing()).Returns(_ping);
            Mock.Arrange(() => _ping.Send(_ipAddress, _timeout, _buffer, Arg.Matches <PingOptions>(pingOptions => pingOptions.Ttl == _pingOptions.Ttl && pingOptions.DontFragment == _pingOptions.DontFragment))).Returns(_pingReply);
            Mock.Arrange(() => _pingReplyEvaluator.IsIpStatusSuccess(_pingReply)).Returns(true);

            Mock.Arrange(() => ((IDisposable)_ping).Dispose()).When(() => true).DoInstead(() =>
            {
                Mock.Arrange(() => _ipAddressByServerNameEnumerator.MoveNext()).Returns(false);
                Mock.Arrange(() => _ipAddressByServerNameEnumerator.Dispose());
            });

            var result = _testObject.BuildEnvironmentMetadata();

            Assert.Equal(_localServerName, result.LocalServerName);
            Assert.Equal(_remoteServerNames, result.RemoteServerNames);
        }
        public IEnvironmentMetadata BuildEnvironmentMetadata()
        {
            const int routingNodes = 64;
            const int timeout      = 1;

            var buffer = new byte[0];

            var pingOptions = new PingOptions
            {
                Ttl          = routingNodes,
                DontFragment = true
            };

            var ipAddressByServerName = _configurationMetadata.GetIpAddressByServerName();
            var remoteServerNames     = new List <string>();

            foreach (var ipAddressByServerNamePair in ipAddressByServerName)
            {
                var ipAddress = ipAddressByServerNamePair.Value;

                using (var ping = _pingCreator.CreatePing())
                {
                    try
                    {
                        var pingReply = ping.Send(ipAddress, timeout, buffer, pingOptions);

                        if (pingReply == null)
                        {
                            continue;
                        }

                        var isIpStatusSuccess = _pingReplyEvaluator.IsIpStatusSuccess(pingReply);

                        if (isIpStatusSuccess)
                        {
                            var serverName = ipAddressByServerNamePair.Key;

                            remoteServerNames.Add(serverName);
                        }
                    }
                    catch (NetworkInformationException)
                    {
                        continue;
                    }
                }
            }

            var machineName         = Environment.MachineName;
            var localServerName     = machineName.ToLower();
            var environmentMetadata = new EnvironmentMetadata(localServerName, remoteServerNames);

            return(environmentMetadata);
        }