示例#1
0
        public async Task ConnectionIsClosedOnDispose()
        {
            // Arrange
            var stream = new Mock <IStreaming>();

            stream.Setup(s => s.CloseStream()).Verifiable();

            var streamingList = new List <IStreaming>
            {
                stream.Object
            };

            var userStream = new Mock <IStreamingConnection>();

            userStream.Setup(s => s.Start(It.IsAny <Func <IStreamContent, Task> >())).Returns(Task.FromResult(streamingList));

            var parser = StreamParser.Create(userStream.Object, null);

            parser.StartStreaming();

            // Act
            await parser.StreamingTask;

            parser.Dispose();

            // Assert
            stream.Verify(s => s.CloseStream(), Times.Once());
        }
示例#2
0
        public void ReceivingFavoriteRaisesEvent()
        {
            // Arrange
            string strContent =
                "{ \"event\":\"favorite\", \"created_at\": \"Sat Sep 04 16:10:54 +0000 2010\", \"target\": { \"id\": 123 }, \"source\": { \"id\": 456 }, \"target_object\": { \"created_at\": \"Wed Jun 06 20:07:10 +0000 2012\", \"id_str\": \"210462857140252672\" } }";
            var           execute = new Mock <ITwitterExecute>();
            StreamContent content = new StreamContent(execute.Object, strContent);

            var stream = new Mock <IStreamingConnection>();

            stream.Setup(s => s.Start(It.IsAny <Func <IStreamContent, Task> >()))
            .Callback <Func <StreamContent, Task> >(func => func(content))
            .Returns(Task.FromResult(new List <IStreaming>()));

            var parser = StreamParser.Create(stream.Object);
            EventStreamEventArgs receivedData = null;
            ManualResetEventSlim waitHandle   = new ManualResetEventSlim(false);

            parser.FavoriteEventReceived += (s, e) =>
            {
                receivedData = e;
                waitHandle.Set();
            };

            // Act
            parser.StartStreaming();
            waitHandle.Wait(1000);

            // Assert
            Assert.IsNotNull(receivedData);
            Assert.AreEqual(StreamEventType.Favorite, receivedData.Event);
            Assert.AreEqual(456ul, receivedData.Source.GetUserId());
            Assert.AreEqual(123ul, receivedData.Target.GetUserId());
        }
示例#3
0
        public void ReceivingUnknownDataRaisesEvent()
        {
            // Arrange
            string        strContent = "{\"test\":[1,2,3]}";
            var           execute    = new Mock <ITwitterExecute>();
            StreamContent content    = new StreamContent(execute.Object, strContent);

            var stream = new Mock <IStreamingConnection>();

            stream.Setup(s => s.Start(It.IsAny <Func <IStreamContent, Task> >()))
            .Callback <Func <StreamContent, Task> >(func => func(content))
            .Returns(Task.FromResult(new List <IStreaming>()));

            var                  parser       = StreamParser.Create(stream.Object);
            StreamEventArgs      receivedData = null;
            ManualResetEventSlim waitHandle   = new ManualResetEventSlim(false);

            parser.UnknownDataReceived += (s, e) =>
            {
                receivedData = e;
                waitHandle.Set();
            };

            // Act
            parser.StartStreaming();
            waitHandle.Wait(1000);

            // Assert
            Assert.IsNotNull(receivedData);
            Assert.AreEqual(strContent, receivedData.Json);
        }
示例#4
0
        public void ReceivingStatusRaisesEvent()
        {
            // Arrange
            var           strContent = File.ReadAllText("Data/tweet.json");
            var           execute    = new Mock <ITwitterExecute>();
            StreamContent content    = new StreamContent(execute.Object, strContent);

            var stream = new Mock <IStreamingConnection>();

            stream.Setup(s => s.Start(It.IsAny <Func <IStreamContent, Task> >()))
            .Callback <Func <StreamContent, Task> >(func => func(content))
            .Returns(Task.FromResult(new List <IStreaming>()));

            var parser = StreamParser.Create(stream.Object);
            StatusStreamEventArgs receivedData = null;
            ManualResetEventSlim  waitHandle   = new ManualResetEventSlim(false);

            parser.StatusReceived += (s, e) =>
            {
                receivedData = e;
                waitHandle.Set();
            };

            // Act
            parser.StartStreaming();
            waitHandle.Wait(1000);

            // Assert
            Assert.IsNotNull(receivedData);
            Assert.AreNotEqual(0ul, receivedData.Status.StatusID);
        }
示例#5
0
        public void ReceivingStatusDeleteRaisesEvent()
        {
            // Arrange
            string        strContent = "{\"delete\":{\"status\":{\"id\":1234,\"id_str\":\"1234\",\"user_id\":3,\"user_id_str\":\"3\"}}}";
            var           execute    = new Mock <ITwitterExecute>();
            StreamContent content    = new StreamContent(execute.Object, strContent);

            var stream = new Mock <IStreamingConnection>();

            stream.Setup(s => s.Start(It.IsAny <Func <IStreamContent, Task> >()))
            .Callback <Func <StreamContent, Task> >(func => func(content))
            .Returns(Task.FromResult(new List <IStreaming>()));

            var parser = StreamParser.Create(stream.Object);
            DeleteStreamEventArgs receivedDelete = null;
            ManualResetEventSlim  waitHandle     = new ManualResetEventSlim(false);

            parser.StatusDeleted += (s, e) =>
            {
                receivedDelete = e;
                waitHandle.Set();
            };

            // Act
            parser.StartStreaming();
            waitHandle.Wait(1000);

            // Assert
            Assert.IsNotNull(receivedDelete);
            Assert.AreEqual(1234ul, receivedDelete.Id);
            Assert.AreEqual(3ul, receivedDelete.UserId);
        }
示例#6
0
        public void StreamParserCanBeCreated()
        {
            // Arrange
            var userStream = new Mock <IStreamingConnection>();

            // Act
            var parser = StreamParser.Create(userStream.Object, null);

            // Assert
            Assert.IsNotNull(parser);
        }
示例#7
0
        private static StreamParser SetupParser(string strContent)
        {
            var           execute = new Mock <ITwitterExecute>();
            StreamContent content = new StreamContent(execute.Object, strContent);

            var stream = new Mock <IStreamingConnection>();

            stream.Setup(s => s.Start(It.IsAny <Func <IStreamContent, Task> >()))
            .Callback <Func <StreamContent, Task> >(func => func(content))
            .Returns(Task.FromResult(new List <IStreaming>()));

            var parser = StreamParser.Create(stream.Object, null);

            return(parser);
        }
示例#8
0
        public void StreamIsNotStartedTwice()
        {
            // Arrange
            var userStream = new Mock <IStreamingConnection>();

            userStream.Setup(s => s.Start(It.IsAny <Func <IStreamContent, Task> >()))
            .Returns(Task.FromResult(new List <IStreaming>())).Verifiable();

            var parser = StreamParser.Create(userStream.Object, null);

            // Act
            parser.StartStreaming();
            parser.StartStreaming();

            // Assert
            userStream.Verify(s => s.Start(It.IsAny <Func <IStreamContent, Task> >()), Times.AtMostOnce());
        }
示例#9
0
        public async Task KeepAliveMessageIsIgnored()
        {
            // Arrange
            string        strContent = "";
            var           execute    = new Mock <ITwitterExecute>();
            StreamContent content    = new StreamContent(execute.Object, strContent);

            var stream = new Mock <IStreamingConnection>();

            stream.Setup(s => s.Start(It.IsAny <Func <IStreamContent, Task> >()))
            .Callback <Func <StreamContent, Task> >(func => func(content))
            .Returns(Task.FromResult(new List <IStreaming>()));

            var parser = StreamParser.Create(stream.Object);

            // Act
            parser.StartStreaming();
            await parser.StreamingTask;

            // Assert
        }