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()); }
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()); }
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); }
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); }
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); }
public void StreamParserCanBeCreated() { // Arrange var userStream = new Mock <IStreamingConnection>(); // Act var parser = StreamParser.Create(userStream.Object, null); // Assert Assert.IsNotNull(parser); }
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); }
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()); }
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 }