public async Task ListensToStreamAsyncTest() { ParticleEventManagerMock eventManager = new ParticleEventManagerMock(); WebEventArgs lastEvent = null; int count = 0; eventManager.Events += (s, e) => { lastEvent = e; count++; eventManager.Stop(); }; using (Stream s = new MemoryStream()) { StreamWriter w = new StreamWriter(s, Encoding.UTF8); w.Write(":ok\n\nevent: test\ndata: {\"data\":\"25.34\",\"ttl\":\"60\",\"published_at\":\"2015-07-18T00:12:18.174Z\",\"coreid\":\"0123456789abcdef01234567\"}\n\n"); w.Flush(); s.Position = 0; // go back to the beginning of the stream await eventManager.ListensToStreamAsyncMock(s); await Task.Delay(500); // Delay a little bit so we make sure the other threads has time to execute. Assert.IsNotNull(lastEvent); Assert.AreEqual("test", lastEvent.Event); Assert.IsNotNull(lastEvent.Data); Assert.AreEqual(1, lastEvent.Data.Length); var data = lastEvent.Data[0]; Assert.AreEqual("25.34", data.Data); Assert.AreEqual(60, data.TimeToLive); Assert.AreEqual(DateTime.Parse("2015-07-18T00:12:18.174Z").ToLocalTime(), data.PublishedAt); Assert.AreEqual("0123456789abcdef01234567", data.CoreId); } }
public async Task ListensToStreamAsyncTest() { ParticleEventManagerMock eventManager = new ParticleEventManagerMock(); WebEventArgs lastEvent = null; int count = 0; eventManager.Events += (s, e) => { lastEvent = e; count++; eventManager.Stop(); }; using(Stream s = new MemoryStream()) { StreamWriter w = new StreamWriter(s, Encoding.UTF8); w.Write(":ok\n\nevent: test\ndata: {\"data\":\"25.34\",\"ttl\":\"60\",\"published_at\":\"2015-07-18T00:12:18.174Z\",\"coreid\":\"0123456789abcdef01234567\"}\n\n"); w.Flush(); s.Position = 0; // go back to the beginning of the stream await eventManager.ListensToStreamAsyncMock(s); await Task.Delay(500); // Delay a little bit so we make sure the other threads has time to execute. Assert.IsNotNull(lastEvent); Assert.AreEqual("test", lastEvent.Event); Assert.IsNotNull(lastEvent.Data); Assert.AreEqual(1, lastEvent.Data.Length); var data = lastEvent.Data[0]; Assert.AreEqual("25.34", data.Data); Assert.AreEqual(60, data.TimeToLive); Assert.AreEqual(DateTime.Parse("2015-07-18T00:12:18.174Z").ToLocalTime(), data.PublishedAt); Assert.AreEqual("0123456789abcdef01234567", data.CoreId); } }