public void OnlyConnectToTcpClientOnce()
        {
            ITcpClient tcpClient = A.Fake<ITcpClient>();

            IscpStream subject = new IscpStream("localhost", 60128, UnitType.Receiver, tcpClient);

            subject.Connect();
            subject.Connect();
            subject.Connect();

            A.CallTo(() => tcpClient.GetDataReader("localhost", 60128)).MustHaveHappened(Repeated.Exactly.Once);
            A.CallTo(() => tcpClient.GetDataWriter("localhost", 60128)).MustHaveHappened(Repeated.Exactly.Once);
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            CommandLine.ParserResult<Options> arguments = CommandLine.Parser.Default.ParseArguments(() => new Options(), args);

            if (!arguments.Errors.Any())
            {
                IscpStream stream = new IscpStream(arguments.Value.HostName, (ushort)arguments.Value.Port, arguments.Value.UnitType);

                Observable
                    .Generate(string.Empty, value => true, value => Input.ReadLine(), value => value)
                    .Where(command => !string.IsNullOrWhiteSpace(command))
                    .Select(command => PacketFactory.Default.CreatePacket(arguments.Value.UnitType, command))
                    .SubscribeOn(TaskPoolScheduler.Default)
                    .Subscribe(packet => stream.Send(packet));

                stream.Received.Subscribe(packet => Output.WriteLine(Encoding.UTF8.GetString(packet.Data)));

                Output.WriteLine("Connecting...");

                using (stream.Connect().Result)
                {
                    Output.WriteLine("Connected");

                    stream.Received.Wait();
                }
            }
            else
            {
                Input.ReadLine();
            }
        }
        public void DisconnectTcpClientWhenConnectionIsDisposed()
        {
            IDataReader dataReader = A.Fake<IDataReader>();
            A.CallTo(() => dataReader.Read(A<byte[]>.Ignored)).Returns(new TaskCompletionSource<object>().Task);
            IDataWriter dataWriter = A.Fake<IDataWriter>();
            A.CallTo(() => dataWriter.Write(A<byte[]>.Ignored)).Returns(new TaskCompletionSource<object>().Task);
            ITcpClient tcpClient = A.Fake<ITcpClient>();
            A.CallTo(() => tcpClient.GetDataReader("localhost", 60128)).Returns(dataReader);
            A.CallTo(() => tcpClient.GetDataWriter("localhost", 60128)).Returns(dataWriter);

            IscpStream subject = new IscpStream("localhost", 60128, UnitType.Receiver, tcpClient);

            IDisposable connection = subject.Connect();

            A.CallTo(() => tcpClient.GetDataReader("localhost", 60128)).MustHaveHappened(Repeated.Exactly.Once);
            A.CallTo(() => tcpClient.GetDataWriter("localhost", 60128)).MustHaveHappened(Repeated.Exactly.Once);

            A.CallTo(() => dataReader.Dispose()).MustNotHaveHappened();
            A.CallTo(() => dataWriter.Dispose()).MustNotHaveHappened();

            connection.Dispose();

            A.CallTo(() => dataReader.Dispose()).MustHaveHappened(Repeated.Exactly.Once);
            A.CallTo(() => dataWriter.Dispose()).MustHaveHappened(Repeated.Exactly.Once);
        }
        public async Task ConnectToTcpClientWhenConnectIsCalled()
        {
            ITcpClient tcpClient = A.Fake<ITcpClient>();

            IscpStream subject = new IscpStream("localhost", 60128, UnitType.Receiver, tcpClient);

            await subject.Connect();

            A.CallTo(() => tcpClient.Connect("localhost", 60128, A<CancellationToken>.Ignored)).MustHaveHappened(Repeated.Exactly.Once);
        }
        public async Task CorrectlyParseAReceivedPacket()
        {
            Stream source = CreateIscpStream("!1PWR01");

            ITcpClient tcpClient = A.Fake<ITcpClient>();
            A.CallTo(() => tcpClient.Read(A<byte[]>.Ignored, A<CancellationToken>.Ignored)).ReturnsLazily(call => FromStream(call.GetArgument<byte[]>(0), source));
            A.CallTo(() => tcpClient.Write(A<byte[]>.Ignored, A<CancellationToken>.Ignored)).Returns(new TaskCompletionSource<object>().Task);

            IscpStream subject = new IscpStream("localhost", 60128, UnitType.Receiver, tcpClient);

            List<IPacket> packets = new List<IPacket>();
            subject.Received.Subscribe(packets.Add);

            await subject.Connect();

            Assert.That(packets.Count, Is.EqualTo(1));
        }
        public void CorrectlyParseMultipleReceivedPackets()
        {
            Stream source = CreateIscpStream(new [] { "!1PWR01", "!1PWR02", "!1PWR03" });

            IDataReader dataReader = A.Fake<IDataReader>();
            A.CallTo(() => dataReader.Read(A<byte[]>.Ignored)).ReturnsLazily(call => FromStream(call.GetArgument<byte[]>(0), source));
            IDataWriter dataWriter = A.Fake<IDataWriter>();
            A.CallTo(() => dataWriter.Write(A<byte[]>.Ignored)).Returns(new TaskCompletionSource<object>().Task);

            ITcpClient tcpClient = A.Fake<ITcpClient>();
            A.CallTo(() => tcpClient.GetDataReader("localhost", 60128)).Returns(dataReader);
            A.CallTo(() => tcpClient.GetDataWriter("localhost", 60128)).Returns(dataWriter);

            IscpStream subject = new IscpStream("localhost", 60128, UnitType.Receiver, tcpClient);

            List<IPacket> packets = new List<IPacket>();
            subject.Subscribe(packets.Add);

            subject.Connect();

            Assert.That(packets.Count, Is.EqualTo(3));
        }