Exemple #1
0
        public void AddAddress_WithAlreadyAddedAddress_DoesNothing()
        {
            // Arrange
            var socket = new DealerSocket();
            var sender = new NetMQSender(socket, messageFactory);

            sender.AddAddress(address);

            // Act
            sender.AddAddress(address);

            // Assert
            Assert.That(sender.Addresses, Has.Count.EqualTo(1));
            Assert.That(sender.Addresses, Has.Exactly(1).EqualTo(address));

            // Cleanup
            sender.Dispose();
        }
Exemple #2
0
        /// <summary>
        /// Constructs a new instance of an <see cref="INetMQSender"/> connected to the supplied <see cref="IAddress"/>
        /// </summary>
        /// <param name="address">Address of the remote the sender will connect to</param>
        /// <returns>Sender connected to the remote address</returns>
        protected override INetMQSender CreateNewSender(IAddress address)
        {
            var socket = new DealerSocket();
            var sender = new NetMQSender(socket, messageFactory);

            sender.AddAddress(address);

            return(sender);
        }
Exemple #3
0
        public async Task SenderReceiver_WhenBoundAndConnected_PassesMessage()
        {
            // Arrange
            var    responseStr     = "Hello, World!";
            var    requestStr      = "Hello";
            var    receivedRequest = string.Empty;
            ushort port            = 6555;
            var    called          = false;

            mockRequestDispatcher
            .Setup(m => m.Handle(ref It.Ref <RequestTask> .IsAny))
            .Callback(new Handler((ref RequestTask task) => {
                called          = true;
                receivedRequest = (string)task.Request;
                task.ResponseSender(responseStr);
            }));

            var packageFactory  = new PackageFactory();
            var serializer      = new DotNetSerializer();
            var serializerCache = new SerializerCache();
            var messageFactory  = new NetMQMessageFactory(serializerCache, packageFactory);
            var sender          = new NetMQSender(new DealerSocket(), messageFactory);
            var receiver        = new NetMQReceiver(new RouterSocket(), messageFactory, requestDispatcher);
            var poller          = new NetMQPoller();

            serializerCache.AddSerializer(serializer);

            sender.AddAddress(TcpAddress.Localhost(port));
            sender.InitializeConnection();

            receiver.AddAddress(TcpAddress.Wildcard(port));
            receiver.InitializeConnection();

            poller.Add(sender.PollableSocket);
            poller.Add(receiver.PollableSocket);
            poller.RunAsync();

            var package = new DataPackage <string>(new GuidPackageId(), requestStr);

            // Act
            var response = await sender.SendAndReceive(package, TimeSpan.FromSeconds(5));

            // Cleanup
            poller.StopAsync();
            sender.TerminateConnection();
            receiver.TerminateConnection();

            // Assert
            Assert.That(called, Is.True);
            Assert.That(receivedRequest, Is.EqualTo(requestStr));
            Assert.That((string)response, Is.EqualTo(responseStr));
        }
Exemple #4
0
        public void Remove_WithAddedAddress_IsConnectedTrue()
        {
            // Arrange
            var socket   = new DealerSocket();
            var sender   = new NetMQSender(socket, messageFactory);
            var address  = TcpAddress.Wildcard(5555);
            var address2 = TcpAddress.Wildcard(5556);

            sender.AddAddress(address);
            sender.AddAddress(address2);
            sender.InitializeConnection();

            // Act
            sender.RemoveAddress(address);

            // Assert
            Assert.That(sender.IsConnected, Is.True);

            // Cleanup
            sender.TerminateConnection();
            sender.Dispose();
        }
Exemple #5
0
        public void RemoveAddress_WithAddedAddress_RemovesFromAddressList()
        {
            // Arrange
            var socket = new DealerSocket();
            var sender = new NetMQSender(socket, messageFactory);

            sender.AddAddress(address);

            // Act
            sender.RemoveAddress(address);

            // Assert
            CollectionAssert.IsEmpty(sender.Addresses);

            // Cleanup
            sender.Dispose();
        }
Exemple #6
0
        public void Remove_WithAddedAddress_IsConnectedFalse()
        {
            // Arrange
            var socket  = new DealerSocket();
            var sender  = new NetMQSender(socket, messageFactory);
            var address = TcpAddress.Wildcard(5555);

            sender.AddAddress(address);

            // Act
            sender.RemoveAddress(address);

            // Assert
            Assert.That(sender.IsConnected, Is.False);

            // Cleanup
            sender.Dispose();
        }
Exemple #7
0
        public void Addresses_WhenDisposed_ThrowsInvalidOperationException()
        {
            // Arrange
            var socket  = new DealerSocket();
            var sender  = new NetMQSender(socket, messageFactory);
            var address = TcpAddress.Wildcard(5555);

            sender.Dispose();

            // Act
            void AddAddress() => sender.AddAddress(address);

            // Assert
            Assert.That(AddAddress, Throws.TypeOf <InvalidOperationException>());

            // Cleanup
            sender.Dispose();
        }