|
1 | 1 | using System; |
2 | 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 3 | +using System.Threading; |
| 4 | +using System.Collections.Generic; |
3 | 5 |
|
4 | 6 | namespace EventSource4Net.Test |
5 | 7 | { |
6 | 8 | [TestClass] |
7 | 9 | public class StringSplitterTest |
8 | 10 | { |
| 11 | + Uri url = new Uri("http://test.com"); |
| 12 | + CancellationTokenSource cts; |
| 13 | + List<EventSourceState> states; |
| 14 | + ServiceResponseMock response; |
| 15 | + WebRequesterFactoryMock factory; |
| 16 | + ManualResetEvent stateIsOpen; |
| 17 | + |
| 18 | + |
| 19 | + private TestableEventSource SetupAndConnect() |
| 20 | + { |
| 21 | + // setup |
| 22 | + cts = new CancellationTokenSource(); |
| 23 | + states = new List<EventSourceState>(); |
| 24 | + response = new ServiceResponseMock(url, System.Net.HttpStatusCode.OK); |
| 25 | + factory = new WebRequesterFactoryMock(response); |
| 26 | + stateIsOpen = new ManualResetEvent(false); |
| 27 | + |
| 28 | + TestableEventSource es = new TestableEventSource(url, factory); |
| 29 | + es.StateChanged += (o, e) => |
| 30 | + { |
| 31 | + states.Add(e.State); |
| 32 | + if (e.State == EventSourceState.OPEN) |
| 33 | + stateIsOpen.Set(); |
| 34 | + }; |
| 35 | + |
| 36 | + return es; |
| 37 | + } |
| 38 | + |
9 | 39 | [TestMethod] |
10 | 40 | public void TestDoubleLineFeed() |
11 | 41 | { |
12 | | - string remainingText = string.Empty; |
13 | | - string[] lines = |
14 | | - StringSplitter.SplitIntoLines("test\n\n", out remainingText); |
| 42 | + // setup |
| 43 | + TestableEventSource es = SetupAndConnect(); |
15 | 44 |
|
16 | | - Assert.AreEqual(lines.Length, 2); |
17 | | - Assert.AreEqual(lines[0], "test"); |
18 | | - Assert.AreEqual(lines[1], string.Empty); |
| 45 | + List<string> receivedMessages = new List<string>(); |
| 46 | + ManualResetEvent eventReceived = new ManualResetEvent(false); |
| 47 | + es.EventReceived += (o, e) => |
| 48 | + { |
| 49 | + receivedMessages.Add(e.Message.Data); |
| 50 | + eventReceived.Set(); |
| 51 | + }; |
| 52 | + |
| 53 | + // act |
| 54 | + es.Start(cts.Token); |
| 55 | + stateIsOpen.WaitOne(); |
| 56 | + response.WriteTestTextToStream("test\n\n"); |
| 57 | + eventReceived.WaitOne(); |
| 58 | + |
| 59 | + // assert |
| 60 | + Assert.AreEqual(receivedMessages.Count, 1); |
| 61 | + Assert.AreEqual(receivedMessages[0], "test"); |
19 | 62 | } |
20 | 63 | [TestMethod] |
21 | 64 | public void TestDoubleCarriageReturn() |
|
0 commit comments