コード例 #1
0
        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);
        }
コード例 #2
0
        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));
        }
コード例 #3
0
        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);
        }
コード例 #4
0
        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());
        }
コード例 #5
0
        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());
        }