public void TryUpdateWithApprovalUnderWay() { var endpoint = new EndpointId("a"); var connection = new EndpointInformation( new EndpointId("a"), new DiscoveryInformation(new Uri("http://localhost/discovery/invalid")), new ProtocolInformation( new Version(), new Uri("http://localhost/protocol/invalid"))); var description = new ProtocolDescription(new List <CommunicationSubject>()); var storage = new EndpointInformationStorage(); Assert.IsTrue(storage.TryAdd(endpoint, connection)); Assert.IsTrue(storage.TryStartApproval(endpoint, description)); var newConnection = new EndpointInformation( endpoint, new DiscoveryInformation(new Uri("http://other/discovery/invalid")), new ProtocolInformation( new Version(), new Uri("http://other/protocol/invalid"))); Assert.IsTrue(storage.TryUpdate(newConnection)); EndpointInformation storedConnection; storage.TryGetConnectionFor(endpoint, out storedConnection); Assert.AreSame(newConnection, storedConnection); }
public void TryAddWithExistingEndpoint() { var storage = new EndpointInformationStorage(); var endpoint = new EndpointId("a"); var connection = new EndpointInformation( new EndpointId("a"), new DiscoveryInformation(new Uri("http://localhost/discovery/invalid")), new ProtocolInformation( new Version(), new Uri("http://localhost/protocol/invalid"))); Assert.IsTrue(storage.TryAdd(endpoint, connection)); Assert.IsFalse(storage.CanCommunicateWithEndpoint(endpoint)); Assert.IsTrue(storage.HasBeenContacted(endpoint)); Assert.IsFalse(storage.IsWaitingForApproval(endpoint)); var newConnection = new EndpointInformation( endpoint, new DiscoveryInformation(new Uri("http://other/discovery/invalid")), new ProtocolInformation( new Version(), new Uri("http://other/protocol/invalid"))); Assert.IsFalse(storage.TryAdd(endpoint, newConnection)); EndpointInformation otherConnection; var result = storage.TryGetConnectionFor(endpoint, out otherConnection); Assert.IsTrue(result); Assert.AreSame(connection, otherConnection); }
public void TryUpdateWithContactedEndpoint() { var endpoint = new EndpointId("a"); var connection = new EndpointInformation( new EndpointId("a"), new DiscoveryInformation(new Uri("http://localhost/discovery/invalid")), new ProtocolInformation( new Version(), new Uri("http://localhost/protocol/invalid"))); var storage = new EndpointInformationStorage(); Assert.IsTrue(storage.TryAdd(endpoint, connection)); var newConnection = new EndpointInformation( endpoint, new DiscoveryInformation(new Uri("http://other/discovery/invalid")), new ProtocolInformation( new Version(), new Uri("http://other/protocol/invalid"))); Assert.IsTrue(storage.TryUpdate(newConnection)); EndpointInformation storedConnection; storage.TryGetConnectionFor(endpoint, out storedConnection); Assert.AreSame(newConnection, storedConnection); }
public void HandshakeWithSuccessResponseWithLocalSendFirst() { var id = new EndpointId("a:10"); var subject = new CommunicationSubject("a"); var connection = new EndpointInformation( new EndpointId("a"), new DiscoveryInformation(new Uri("net.tcp://localhost/discovery/invalid")), new ProtocolInformation( new Version(1, 0), new Uri("net.tcp://localhost/protocol/invalid"))); var remoteEndpoint = new EndpointId("b:10"); var remoteMessageAddress = @"net.tcp://othermachine"; var remoteDataAddress = @"net.tcp://othermachine/data"; var communicationDescriptions = new Mock <IStoreProtocolSubjects>(); { communicationDescriptions.Setup(c => c.Subjects()) .Returns( new[] { subject }); communicationDescriptions.Setup(c => c.ToStorage()) .Returns( new ProtocolDescription( new[] { subject })); } var endpointApprover = new Mock <IApproveEndpointConnections>(); { endpointApprover.Setup(e => e.ProtocolVersion) .Returns(new Version(1, 0)); endpointApprover.Setup(e => e.IsEndpointAllowedToConnect(It.IsAny <ProtocolDescription>())) .Returns(true); } Func <ChannelTemplate, Uri> discoveryUri = t => new Uri("net.tcp://localhost/discovery/invalid"); var discoveryInformation = new LocalConnectionInformation(id, discoveryUri); var storage = new EndpointInformationStorage(); var endpointConnected = false; storage.OnEndpointConnected += (s, e) => { endpointConnected = true; Assert.AreEqual(remoteEndpoint, e.Endpoint); }; var discovery = new Mock <IDiscoverOtherServices>(); var protocolLayer = new Mock <IProtocolLayer>(); { protocolLayer.Setup(l => l.Id) .Returns(id); protocolLayer.Setup(l => l.LocalConnectionFor(It.IsAny <Version>(), It.IsAny <ChannelTemplate>())) .Returns( new Tuple <EndpointId, Uri, Uri>( connection.Id, connection.ProtocolInformation.MessageAddress, connection.ProtocolInformation.DataAddress)); protocolLayer.Setup(l => l.SendMessageToUnregisteredEndpoint( It.IsAny <EndpointInformation>(), It.IsAny <ICommunicationMessage>(), It.IsAny <int>())) .Verifiable(); protocolLayer.Setup(l => l.SendMessageToUnregisteredEndpointAndWaitForResponse( It.IsAny <EndpointInformation>(), It.IsAny <ICommunicationMessage>(), It.IsAny <int>(), It.IsAny <TimeSpan>())) .Returns(Task <ICommunicationMessage> .Factory.StartNew( () => new SuccessMessage(remoteEndpoint, new MessageId()), new CancellationTokenSource().Token, TaskCreationOptions.None, new CurrentThreadTaskScheduler())) .Verifiable(); } var configuration = new Mock <IConfiguration>(); { configuration.Setup(c => c.HasValueFor(It.IsAny <ConfigurationKey>())) .Returns(false); } var layer = new ProtocolHandshakeConductor( storage, discoveryInformation, new[] { discovery.Object }, protocolLayer.Object, communicationDescriptions.Object, new[] { endpointApprover.Object }, new[] { ChannelTemplate.NamedPipe, ChannelTemplate.TcpIP, }, configuration.Object, new SystemDiagnostics((l, m) => { }, null)); discovery.Raise( d => d.OnEndpointBecomingAvailable += null, new EndpointDiscoveredEventArgs( new EndpointInformation( remoteEndpoint, new DiscoveryInformation(new Uri("net.tcp://localhost/discovery/invalid")), new ProtocolInformation(new Version(1, 0), new Uri(remoteMessageAddress))))); Assert.IsTrue(storage.HasBeenContacted(remoteEndpoint)); Assert.IsFalse(storage.IsWaitingForApproval(remoteEndpoint)); Assert.IsFalse(storage.CanCommunicateWithEndpoint(remoteEndpoint)); var remoteInfo = new EndpointInformation( remoteEndpoint, new DiscoveryInformation(new Uri("net.tcp://localhost/discovery/invalid")), new ProtocolInformation(new Version(1, 0), new Uri(remoteMessageAddress), new Uri(remoteDataAddress))); layer.ContinueHandshakeWith(remoteInfo, communicationDescriptions.Object.ToStorage(), new MessageId()); Assert.IsFalse(storage.HasBeenContacted(remoteEndpoint)); Assert.IsFalse(storage.IsWaitingForApproval(remoteEndpoint)); Assert.IsTrue(storage.CanCommunicateWithEndpoint(remoteEndpoint)); Assert.IsTrue(endpointConnected); EndpointInformation info; var tryGet = storage.TryGetConnectionFor(remoteEndpoint, out info); Assert.IsTrue(tryGet); Assert.AreEqual(remoteInfo.DiscoveryInformation.Address, info.DiscoveryInformation.Address); Assert.AreEqual(remoteInfo.ProtocolInformation.Version, info.ProtocolInformation.Version); Assert.AreEqual(remoteInfo.ProtocolInformation.MessageAddress, info.ProtocolInformation.MessageAddress); Assert.AreEqual(remoteInfo.ProtocolInformation.DataAddress, info.ProtocolInformation.DataAddress); protocolLayer.Verify( l => l.SendMessageToUnregisteredEndpoint(It.IsAny <EndpointInformation>(), It.IsAny <ICommunicationMessage>(), It.IsAny <int>()), Times.Once()); protocolLayer.Verify( l => l.SendMessageToUnregisteredEndpointAndWaitForResponse( It.IsAny <EndpointInformation>(), It.IsAny <ICommunicationMessage>(), It.IsAny <int>(), It.IsAny <TimeSpan>()), Times.Once()); }