public void OpenChannel()
        {
            var id      = EndpointIdExtensions.CreateEndpointIdForCurrentProcess();
            var baseUri = new Uri("http://localhost/invalid");
            var versionedEndpointUri = new Uri("http://localhost/invalid/v1");
            var template             = new Mock <IDiscoveryChannelTemplate>();
            {
                template.Setup(
                    t => t.AttachDiscoveryEntryEndpoint(It.IsAny <ServiceHost>(), It.IsAny <Type>(), It.IsAny <EndpointId>(), It.IsAny <bool>()))
                .Returns(new ServiceEndpoint(new ContractDescription("a"), new NetNamedPipeBinding(), new EndpointAddress(baseUri)));
            }

            var versionedEndpointCounter = 0;
            var versionedEndpoint        = new Mock <IVersionedDiscoveryEndpoint>();
            Func <Version, ChannelTemplate, Tuple <Type, IVersionedDiscoveryEndpoint> > endpointBuilder =
                (v, t) =>
            {
                versionedEndpointCounter++;
                return(new Tuple <Type, IVersionedDiscoveryEndpoint>(versionedEndpoint.Object.GetType(), versionedEndpoint.Object));
            };

            BootstrapEndpoint baseEndpoint = null;
            var host = new Mock <IHoldServiceConnections>();
            {
                host.Setup(h => h.OpenChannel(It.IsAny <IReceiveInformationFromRemoteEndpoints>(), It.IsAny <Func <ServiceHost, ServiceEndpoint> >()))
                .Callback <IReceiveInformationFromRemoteEndpoints, Func <ServiceHost, ServiceEndpoint> >(
                    (h, e) =>
                {
                    e(null);
                    baseEndpoint = h as BootstrapEndpoint;
                })
                .Returns <IReceiveInformationFromRemoteEndpoints, Func <ServiceHost, ServiceEndpoint> >(
                    (h, e) => (h is IVersionedDiscoveryEndpoint) ? versionedEndpointUri : baseUri)
                .Verifiable();
            }

            Func <IHoldServiceConnections> hostBuilder = () => host.Object;

            Uri          entryAddress = null;
            Action <Uri> storage      = u => entryAddress = u;

            var channel = new BootstrapChannel(
                id,
                template.Object,
                endpointBuilder,
                hostBuilder,
                storage);

            channel.OpenChannel(true);

            host.Verify(
                h => h.OpenChannel(It.IsAny <IReceiveInformationFromRemoteEndpoints>(), It.IsAny <Func <ServiceHost, ServiceEndpoint> >()),
                Times.Exactly(2));
            Assert.AreEqual(1, versionedEndpointCounter);
            Assert.AreEqual(baseUri, entryAddress);
            Assert.IsNotNull(baseEndpoint);
            Assert.AreEqual(1, baseEndpoint.DiscoveryVersions().Length);
            Assert.AreEqual(versionedEndpointUri, baseEndpoint.UriForVersion(baseEndpoint.DiscoveryVersions()[0]));
        }
        public void UriForVersionWithNonExistingVersion()
        {
            var versionedEndpoints = new List <Tuple <Version, Uri> >
            {
                new Tuple <Version, Uri>(
                    new Version(2, 0, 0, 0),
                    new Uri("http://localhost/v2")),
                new Tuple <Version, Uri>(
                    new Version(1, 0, 0, 0),
                    new Uri("http://localhost/v1")),
                new Tuple <Version, Uri>(
                    new Version(3, 0, 0, 0),
                    new Uri("http://localhost/v3")),
            };

            var endpoint = new BootstrapEndpoint(versionedEndpoints);

            Assert.IsNull(endpoint.UriForVersion(new Version(4, 0, 0, 0)));
            Assert.IsNull(endpoint.UriForVersion(new Version(1, 1, 0, 0)));
            Assert.IsNull(endpoint.UriForVersion(new Version(1, 0, 1, 0)));
            Assert.IsNull(endpoint.UriForVersion(new Version(1, 0, 0, 1)));
        }
        public void UriForVersion()
        {
            var versionedEndpoints = new List <Tuple <Version, Uri> >
            {
                new Tuple <Version, Uri>(
                    new Version(2, 0, 0, 0),
                    new Uri("http://localhost/v2")),
                new Tuple <Version, Uri>(
                    new Version(1, 0, 0, 0),
                    new Uri("http://localhost/v1")),
                new Tuple <Version, Uri>(
                    new Version(3, 0, 0, 0),
                    new Uri("http://localhost/v3")),
            };

            var endpoint = new BootstrapEndpoint(versionedEndpoints);
            var address  = endpoint.UriForVersion(new Version(2, 0, 0, 0));

            Assert.AreEqual(versionedEndpoints[0].Item2, address);
        }