public void LocalConnectionPointForVersionWithUnknownVersion() { var id = new EndpointId("a"); var template = new Mock <IProtocolChannelTemplate>(); var host = new Mock <IHoldServiceConnections>(); Func <IHoldServiceConnections> hostBuilder = () => host.Object; Func <Version, Tuple <Type, IMessagePipe> > messageReceiverBuilder = version => new Tuple <Type, IMessagePipe>(null, null); Func <Version, Tuple <Type, IDataPipe> > dataReceiverBuilder = version => new Tuple <Type, IDataPipe>(null, null); var sendingEndpoint = new Mock <ISendingEndpoint>(); BuildSendingEndpoint senderBuilder = (endpoint, builder, endpointBuilder) => sendingEndpoint.Object; var messageEndpoint = new Mock <IMessageSendingEndpoint>(); Func <Version, Uri, IMessageSendingEndpoint> versionedMessageSenderBuilder = (version, uri) => messageEndpoint.Object; var dataEndpoint = new Mock <IDataTransferingEndpoint>(); Func <Version, Uri, IDataTransferingEndpoint> versionedDataSenderBuilder = (version, uri) => dataEndpoint.Object; var channel = new ProtocolChannel( id, template.Object, hostBuilder, messageReceiverBuilder, dataReceiverBuilder, senderBuilder, versionedMessageSenderBuilder, versionedDataSenderBuilder); var connection = channel.LocalConnectionPointForVersion(ProtocolVersions.V1); Assert.IsNull(connection); }
/// <summary> /// Initializes a new instance of the <see cref="ProtocolChannel"/> class. /// </summary> /// <param name="id">The ID number of the current endpoint.</param> /// <param name="channelTemplate">The type of channel, e.g. TCP.</param> /// <param name="hostBuilder"> /// The function that returns an object which handles the <see cref="ServiceHost"/> for the channel used to communicate with. /// </param> /// <param name="messageReceiverBuilder">The function that builds message receiving endpoints.</param> /// <param name="dataReceiverBuilder">The function that builds data receiving endpoints.</param> /// <param name="senderBuilder">The function that builds sending endpoints.</param> /// <param name="versionedMessageSenderBuilder"> /// The function that creates a specific version of the message sender used to connect to a given URL. /// </param> /// <param name="versionedDataSenderBuilder"> /// The function that creates a specific version of the data sender used to connect to a given URL. /// </param> /// <exception cref="ArgumentNullException"> /// Thrown if <paramref name="id"/> is <see langword="null" />. /// </exception> /// <exception cref="ArgumentNullException"> /// Thrown if <paramref name="channelTemplate"/> is <see langword="null" />. /// </exception> /// <exception cref="ArgumentNullException"> /// Thrown if <paramref name="hostBuilder"/> is <see langword="null" />. /// </exception> /// <exception cref="ArgumentNullException"> /// Thrown if <paramref name="messageReceiverBuilder"/> is <see langword="null" />. /// </exception> /// <exception cref="ArgumentNullException"> /// Thrown if <paramref name="dataReceiverBuilder"/> is <see langword="null" />. /// </exception> /// <exception cref="ArgumentNullException"> /// Thrown if <paramref name="senderBuilder"/> is <see langword="null" />. /// </exception> /// <exception cref="ArgumentNullException"> /// Thrown if <paramref name="versionedMessageSenderBuilder"/> is <see langword="null" />. /// </exception> /// <exception cref="ArgumentNullException"> /// Thrown if <paramref name="versionedDataSenderBuilder"/> is <see langword="null" />. /// </exception> public ProtocolChannel( EndpointId id, IProtocolChannelTemplate channelTemplate, Func <IHoldServiceConnections> hostBuilder, Func <Version, Tuple <Type, IMessagePipe> > messageReceiverBuilder, Func <Version, Tuple <Type, IDataPipe> > dataReceiverBuilder, BuildSendingEndpoint senderBuilder, Func <Version, Uri, IMessageSendingEndpoint> versionedMessageSenderBuilder, Func <Version, Uri, IDataTransferingEndpoint> versionedDataSenderBuilder) { { Lokad.Enforce.Argument(() => id); Lokad.Enforce.Argument(() => channelTemplate); Lokad.Enforce.Argument(() => hostBuilder); Lokad.Enforce.Argument(() => messageReceiverBuilder); Lokad.Enforce.Argument(() => dataReceiverBuilder); Lokad.Enforce.Argument(() => senderBuilder); Lokad.Enforce.Argument(() => versionedMessageSenderBuilder); Lokad.Enforce.Argument(() => versionedDataSenderBuilder); } m_Id = id; m_Template = channelTemplate; m_HostBuilder = hostBuilder; m_MessageMessageReceiverBuilder = messageReceiverBuilder; m_DataReceiverBuilder = dataReceiverBuilder; m_SenderBuilder = senderBuilder; m_VersionedMessageSenderBuilder = versionedMessageSenderBuilder; m_VersionedDataSenderBuilder = versionedDataSenderBuilder; }
public void OpenChannel() { var id = new EndpointId("a"); var template = new Mock <IProtocolChannelTemplate>(); var messageUri = new Uri("http://localhost/messages/invalid"); var dataUri = new Uri("http://localhost/data/invalid"); var host = new Mock <IHoldServiceConnections>(); { host.Setup( h => h.OpenChannel( It.IsAny <IReceiveInformationFromRemoteEndpoints>(), It.IsAny <Func <ServiceHost, ServiceEndpoint> >())) .Returns <IReceiveInformationFromRemoteEndpoints, Func <ServiceHost, ServiceEndpoint> >( (r, f) => r is IMessagePipe ? messageUri : dataUri) .Verifiable(); } Func <IHoldServiceConnections> hostBuilder = () => host.Object; var messagePipe = new Mock <IMessagePipe>(); Func <Version, Tuple <Type, IMessagePipe> > messageReceiverBuilder = version => new Tuple <Type, IMessagePipe>(typeof(IMessagePipe), messagePipe.Object); var dataPipe = new Mock <IDataPipe>(); Func <Version, Tuple <Type, IDataPipe> > dataReceiverBuilder = version => new Tuple <Type, IDataPipe>(typeof(IDataPipe), dataPipe.Object); var sendingEndpoint = new Mock <ISendingEndpoint>(); BuildSendingEndpoint senderBuilder = (endpoint, builder, endpointBuilder) => sendingEndpoint.Object; var messageEndpoint = new Mock <IMessageSendingEndpoint>(); Func <Version, Uri, IMessageSendingEndpoint> versionedMessageSenderBuilder = (version, uri) => messageEndpoint.Object; var dataEndpoint = new Mock <IDataTransferingEndpoint>(); Func <Version, Uri, IDataTransferingEndpoint> versionedDataSenderBuilder = (version, uri) => dataEndpoint.Object; var channel = new ProtocolChannel( id, template.Object, hostBuilder, messageReceiverBuilder, dataReceiverBuilder, senderBuilder, versionedMessageSenderBuilder, versionedDataSenderBuilder); channel.OpenChannel(); var protocols = channel.LocalConnectionPoints().ToList(); Assert.AreEqual(1, protocols.Count()); Assert.That(protocols.Select(p => p.Version), Is.EquivalentTo(ProtocolVersions.SupportedVersions())); var connection = channel.LocalConnectionPointForVersion(ProtocolVersions.V1); Assert.AreEqual(ProtocolVersions.V1, connection.Version); Assert.AreEqual(messageUri, connection.MessageAddress); Assert.AreEqual(dataUri, connection.DataAddress); host.Verify( h => h.OpenChannel(It.IsAny <IReceiveInformationFromRemoteEndpoints>(), It.IsAny <Func <ServiceHost, ServiceEndpoint> >()), Times.Exactly(2)); }
public void OnDataReception() { var id = new EndpointId("a"); var template = new Mock <IProtocolChannelTemplate>(); var messageUri = new Uri("http://localhost/messages/invalid"); var dataUri = new Uri("http://localhost/data/invalid"); var host = new Mock <IHoldServiceConnections>(); { host.Setup( h => h.OpenChannel( It.IsAny <IReceiveInformationFromRemoteEndpoints>(), It.IsAny <Func <ServiceHost, ServiceEndpoint> >())) .Returns <IReceiveInformationFromRemoteEndpoints, Func <ServiceHost, ServiceEndpoint> >( (r, f) => r is IMessagePipe ? messageUri : dataUri) .Verifiable(); } Func <IHoldServiceConnections> hostBuilder = () => host.Object; var messagePipe = new Mock <IMessagePipe>(); Func <Version, Tuple <Type, IMessagePipe> > messageReceiverBuilder = version => new Tuple <Type, IMessagePipe>(typeof(IMessagePipe), messagePipe.Object); var dataPipe = new Mock <IDataPipe>(); Func <Version, Tuple <Type, IDataPipe> > dataReceiverBuilder = version => new Tuple <Type, IDataPipe>(typeof(IDataPipe), dataPipe.Object); var sendingEndpoint = new Mock <ISendingEndpoint>(); BuildSendingEndpoint senderBuilder = (endpoint, builder, endpointBuilder) => sendingEndpoint.Object; var messageEndpoint = new Mock <IMessageSendingEndpoint>(); Func <Version, Uri, IMessageSendingEndpoint> versionedMessageSenderBuilder = (version, uri) => messageEndpoint.Object; var dataEndpoint = new Mock <IDataTransferingEndpoint>(); Func <Version, Uri, IDataTransferingEndpoint> versionedDataSenderBuilder = (version, uri) => dataEndpoint.Object; var channel = new ProtocolChannel( id, template.Object, hostBuilder, messageReceiverBuilder, dataReceiverBuilder, senderBuilder, versionedMessageSenderBuilder, versionedDataSenderBuilder); var eventWasRaised = false; var msg = new DataTransferMessage(); channel.OnDataReception += (s, e) => { eventWasRaised = true; Assert.AreEqual(msg, e.Data); }; channel.OpenChannel(); var protocols = channel.LocalConnectionPoints().ToList(); Assert.AreEqual(1, protocols.Count()); dataPipe.Raise(m => m.OnNewData += null, new DataTransferEventArgs(msg)); Assert.IsTrue(eventWasRaised); }
public void TransferData() { var path = TempFile(); var text = "Some random text"; using (var writer = new StreamWriter(path, false)) { writer.Write(text); } var id = new EndpointId("a"); var endpointInfo = new ProtocolInformation( ProtocolVersions.V1, new Uri("http://localhost/messages"), new Uri("http://localhost/data")); var template = new Mock <IProtocolChannelTemplate>(); var messageUri = new Uri("http://localhost/messages/invalid"); var dataUri = new Uri("http://localhost/data/invalid"); var host = new Mock <IHoldServiceConnections>(); { host.Setup( h => h.OpenChannel( It.IsAny <IReceiveInformationFromRemoteEndpoints>(), It.IsAny <Func <ServiceHost, ServiceEndpoint> >())) .Returns <IReceiveInformationFromRemoteEndpoints, Func <ServiceHost, ServiceEndpoint> >( (r, f) => r is IMessagePipe ? messageUri : dataUri) .Verifiable(); host.Setup(h => h.CloseConnection()) .Verifiable(); } Func <IHoldServiceConnections> hostBuilder = () => host.Object; var messagePipe = new Mock <IMessagePipe>(); Func <Version, Tuple <Type, IMessagePipe> > messageReceiverBuilder = version => new Tuple <Type, IMessagePipe>(typeof(IMessagePipe), messagePipe.Object); var dataPipe = new Mock <IDataPipe>(); Func <Version, Tuple <Type, IDataPipe> > dataReceiverBuilder = version => new Tuple <Type, IDataPipe>(typeof(IDataPipe), dataPipe.Object); var sendingEndpoint = new Mock <ISendingEndpoint>(); { sendingEndpoint.Setup(e => e.KnownEndpoints()) .Returns(new[] { endpointInfo }); sendingEndpoint.Setup(e => e.CloseChannelTo(It.IsAny <ProtocolInformation>())) .Verifiable(); sendingEndpoint.Setup(e => e.Send(It.IsAny <ProtocolInformation>(), It.IsAny <Stream>(), It.IsAny <int>())) .Callback <ProtocolInformation, Stream, int>( (e, m, r) => { string fileText; using (var reader = new StreamReader(m)) { fileText = reader.ReadToEnd(); } Assert.AreEqual(text, fileText); }) .Verifiable(); } BuildSendingEndpoint senderBuilder = (endpoint, builder, endpointBuilder) => sendingEndpoint.Object; var messageEndpoint = new Mock <IMessageSendingEndpoint>(); Func <Version, Uri, IMessageSendingEndpoint> versionedMessageSenderBuilder = (version, uri) => messageEndpoint.Object; var dataEndpoint = new Mock <IDataTransferingEndpoint>(); Func <Version, Uri, IDataTransferingEndpoint> versionedDataSenderBuilder = (version, uri) => dataEndpoint.Object; var channel = new ProtocolChannel( id, template.Object, hostBuilder, messageReceiverBuilder, dataReceiverBuilder, senderBuilder, versionedMessageSenderBuilder, versionedDataSenderBuilder); channel.OpenChannel(); var protocols = channel.LocalConnectionPoints().ToList(); Assert.AreEqual(1, protocols.Count()); var task = channel.TransferData(endpointInfo, path, new CancellationToken(), new CurrentThreadTaskScheduler(), 1); task.Wait(); sendingEndpoint.Verify(e => e.Send(It.IsAny <ProtocolInformation>(), It.IsAny <Stream>(), It.IsAny <int>()), Times.Once()); }
public void Send() { var id = new EndpointId("a"); var endpointInfo = new ProtocolInformation( ProtocolVersions.V1, new Uri("http://localhost/messages"), new Uri("http://localhost/data")); var template = new Mock <IProtocolChannelTemplate>(); var messageUri = new Uri("http://localhost/messages/invalid"); var dataUri = new Uri("http://localhost/data/invalid"); var host = new Mock <IHoldServiceConnections>(); { host.Setup( h => h.OpenChannel( It.IsAny <IReceiveInformationFromRemoteEndpoints>(), It.IsAny <Func <ServiceHost, ServiceEndpoint> >())) .Returns <IReceiveInformationFromRemoteEndpoints, Func <ServiceHost, ServiceEndpoint> >( (r, f) => r is IMessagePipe ? messageUri : dataUri) .Verifiable(); host.Setup(h => h.CloseConnection()) .Verifiable(); } Func <IHoldServiceConnections> hostBuilder = () => host.Object; var messagePipe = new Mock <IMessagePipe>(); Func <Version, Tuple <Type, IMessagePipe> > messageReceiverBuilder = version => new Tuple <Type, IMessagePipe>(typeof(IMessagePipe), messagePipe.Object); var dataPipe = new Mock <IDataPipe>(); Func <Version, Tuple <Type, IDataPipe> > dataReceiverBuilder = version => new Tuple <Type, IDataPipe>(typeof(IDataPipe), dataPipe.Object); var msg = new SuccessMessage(id, new MessageId()); var sendingEndpoint = new Mock <ISendingEndpoint>(); { sendingEndpoint.Setup(e => e.KnownEndpoints()) .Returns(new[] { endpointInfo }); sendingEndpoint.Setup(e => e.CloseChannelTo(It.IsAny <ProtocolInformation>())) .Verifiable(); sendingEndpoint.Setup(e => e.Send(It.IsAny <ProtocolInformation>(), It.IsAny <ICommunicationMessage>(), It.IsAny <int>())) .Callback <ProtocolInformation, ICommunicationMessage, int>((e, m, r) => Assert.AreSame(msg, m)) .Verifiable(); } BuildSendingEndpoint senderBuilder = (endpoint, builder, endpointBuilder) => sendingEndpoint.Object; var messageEndpoint = new Mock <IMessageSendingEndpoint>(); Func <Version, Uri, IMessageSendingEndpoint> versionedMessageSenderBuilder = (version, uri) => messageEndpoint.Object; var dataEndpoint = new Mock <IDataTransferingEndpoint>(); Func <Version, Uri, IDataTransferingEndpoint> versionedDataSenderBuilder = (version, uri) => dataEndpoint.Object; var channel = new ProtocolChannel( id, template.Object, hostBuilder, messageReceiverBuilder, dataReceiverBuilder, senderBuilder, versionedMessageSenderBuilder, versionedDataSenderBuilder); channel.OpenChannel(); var protocols = channel.LocalConnectionPoints().ToList(); Assert.AreEqual(1, protocols.Count()); channel.Send(endpointInfo, msg, 1); sendingEndpoint.Verify(e => e.Send(It.IsAny <ProtocolInformation>(), It.IsAny <ICommunicationMessage>(), It.IsAny <int>()), Times.Once()); }