public ServerClientTests()
        {
            tcpClient = A.Fake <ITcpClient>();
            stream    = A.Fake <INetworkStream>();
            reader    = A.Fake <IMessageStreamReader>();
            sender    = A.Fake <IMessagesSender>();
            config    = A.Fake <ICsConfiguration>();
            mediator  = A.Fake <IMediator>();

            A.CallTo(() => config.Address).Returns(IPAddress.Loopback);
            A.CallTo(() => config.Port).Returns(1234);
            A.CallTo(() => tcpClient.GetStream()).Returns(stream);
            A.CallTo(() => reader.MayHaveMessages).ReturnsLazily(() => messagesRead = !messagesRead);
            A.CallTo(() => reader.ReadAvailable()).ReturnsLazily(() => A.CollectionOfFake <IMessage>(1).ToArray());

            Func <ITcpClient> clientFactory = () =>
            {
                tcpClientCreated = true;
                return(tcpClient);
            };
            Func <INetworkStream, IMessageStreamReader> readerFactory = s =>
            {
                if (s == stream)
                {
                    readerCreated = true;
                    return(reader);
                }
                throw new InvalidOperationException();
            };

            serverClient = new ServerClient(config, sender, mediator, clientFactory, readerFactory);
        }
Esempio n. 2
0
        /// <remarks>
        /// We don't have a way to reliably test for TCP/IP connection close or end-of-message, so we use this kind of
        /// hack. This class is rather untestable (data must be injected to the stream during the SpinWait).
        /// </remarks>
        public static IReadOnlyList <IMessage> ReadAll(this IMessageStreamReader stream)
        {
            var msgs     = new List <IMessage>();
            int waitTime = 1000;

            while (stream.MayHaveMessages)
            {
                var part = stream.ReadAvailable();
                if (part.Count == 0)
                {
                    Thread.SpinWait(waitTime);
                    waitTime *= 2;
                }
                else
                {
                    msgs.AddRange(part);
                }
            }
            return(msgs);
        }
Esempio n. 3
0
        public void Reads_data_from_the_stream()
        {
            PutData(1);

            parser.ReadAvailable();

            A.CallTo(() => stream.Read(null, 0, 0)).WithAnyArguments().MustHaveHappened();
        }