@@ -17,15 +17,18 @@ public class KeepAliveTests
1717 {
1818 #region Fields
1919 private const int KEEPALIVE_INTERVAL = 1 ;
20- private readonly static TimeSpan KEEPALIVE_TIMESPAN = TimeSpan . FromSeconds ( KEEPALIVE_INTERVAL ) ;
20+ private readonly static TimeSpan KEEPALIVE_TIMESPAN = TimeSpan . FromSeconds ( KEEPALIVE_INTERVAL + 1 ) ;
2121
2222 private const string DEFAULT_KEEPALIVE = ": KEEPALIVE\r \n \r \n " ;
23+ private const string CUSTOM_KEEPALIVE_CONTENT = "PING" ;
24+ private const string CUSTOM_KEEPALIVE_COMMENT = ": PING\r \n \r \n " ;
25+ private const string CUSTOM_KEEPALIVE_EVENT = "event: PING\r \n data: \r \n \r \n " ;
2326 #endregion
2427
2528 #region SUT
26- private class KeepaliveModeNeverServerSentEventsServerStartup : FakeServerSentEventsServerStartup
29+ private class KeepaliveNeverServerSentEventsServerStartup : FakeServerSentEventsServerStartup
2730 {
28- public KeepaliveModeNeverServerSentEventsServerStartup ( IConfiguration configuration ) : base ( configuration )
31+ public KeepaliveNeverServerSentEventsServerStartup ( IConfiguration configuration ) : base ( configuration )
2932 { }
3033
3134 protected override Action < ServerSentEventsServiceOptions < ServerSentEventsService > > ConfigureServerSentEventsOption
@@ -41,9 +44,9 @@ protected override Action<ServerSentEventsServiceOptions<ServerSentEventsService
4144 }
4245 }
4346
44- private class KeepaliveModeAlwaysServerSentEventsServerStartup : FakeServerSentEventsServerStartup
47+ private class KeepaliveDefultAlwaysServerSentEventsServerStartup : FakeServerSentEventsServerStartup
4548 {
46- public KeepaliveModeAlwaysServerSentEventsServerStartup ( IConfiguration configuration ) : base ( configuration )
49+ public KeepaliveDefultAlwaysServerSentEventsServerStartup ( IConfiguration configuration ) : base ( configuration )
4750 { }
4851
4952 protected override Action < ServerSentEventsServiceOptions < ServerSentEventsService > > ConfigureServerSentEventsOption
@@ -58,13 +61,53 @@ protected override Action<ServerSentEventsServiceOptions<ServerSentEventsService
5861 }
5962 }
6063 }
64+
65+ private class KeepaliveCustomCommentAlwaysServerSentEventsServerStartup : FakeServerSentEventsServerStartup
66+ {
67+ public KeepaliveCustomCommentAlwaysServerSentEventsServerStartup ( IConfiguration configuration ) : base ( configuration )
68+ { }
69+
70+ protected override Action < ServerSentEventsServiceOptions < ServerSentEventsService > > ConfigureServerSentEventsOption
71+ {
72+ get
73+ {
74+ return options =>
75+ {
76+ options . KeepaliveMode = ServerSentEventsKeepaliveMode . Always ;
77+ options . KeepaliveInterval = KEEPALIVE_INTERVAL ;
78+ options . KeepaliveKind = ServerSentEventsKeepaliveKind . Comment ;
79+ options . KeepaliveContent = CUSTOM_KEEPALIVE_CONTENT ;
80+ } ;
81+ }
82+ }
83+ }
84+
85+ private class KeepaliveCustomEventAlwaysServerSentEventsServerStartup : FakeServerSentEventsServerStartup
86+ {
87+ public KeepaliveCustomEventAlwaysServerSentEventsServerStartup ( IConfiguration configuration ) : base ( configuration )
88+ { }
89+
90+ protected override Action < ServerSentEventsServiceOptions < ServerSentEventsService > > ConfigureServerSentEventsOption
91+ {
92+ get
93+ {
94+ return options =>
95+ {
96+ options . KeepaliveMode = ServerSentEventsKeepaliveMode . Always ;
97+ options . KeepaliveInterval = KEEPALIVE_INTERVAL ;
98+ options . KeepaliveKind = ServerSentEventsKeepaliveKind . Event ;
99+ options . KeepaliveContent = CUSTOM_KEEPALIVE_CONTENT ;
100+ } ;
101+ }
102+ }
103+ }
61104 #endregion
62105
63106 #region Tests
64107 [ Fact ]
65108 public async Task ServerSentEventsServer_KeepaliveModeNever_DoesNotSendKeepalive ( )
66109 {
67- using FakeServerSentEventsServerrApplicationFactory < KeepaliveModeNeverServerSentEventsServerStartup > serverSentEventsServerApplicationFactory = new ( ) ;
110+ using FakeServerSentEventsServerrApplicationFactory < KeepaliveNeverServerSentEventsServerStartup > serverSentEventsServerApplicationFactory = new ( ) ;
68111 HttpClient serverSentEventsClient = serverSentEventsServerApplicationFactory . CreateClient ( ) ;
69112
70113 string serverSentEvents = await GetServerSentEvents ( serverSentEventsClient ) . ConfigureAwait ( false ) ;
@@ -75,14 +118,36 @@ public async Task ServerSentEventsServer_KeepaliveModeNever_DoesNotSendKeepalive
75118 [ Fact ]
76119 public async Task ServerSentEventsServer_KeepaliveModeAlways_SendsDefaultKeepalive ( )
77120 {
78- using FakeServerSentEventsServerrApplicationFactory < KeepaliveModeAlwaysServerSentEventsServerStartup > serverSentEventsServerApplicationFactory = new ( ) ;
121+ using FakeServerSentEventsServerrApplicationFactory < KeepaliveDefultAlwaysServerSentEventsServerStartup > serverSentEventsServerApplicationFactory = new ( ) ;
79122 HttpClient serverSentEventsClient = serverSentEventsServerApplicationFactory . CreateClient ( ) ;
80123
81124 string serverSentEvents = await GetServerSentEvents ( serverSentEventsClient ) . ConfigureAwait ( false ) ;
82125
83126 Assert . Matches ( $ "^({ DEFAULT_KEEPALIVE } )+$", serverSentEvents ) ;
84127 }
85128
129+ [ Fact ]
130+ public async Task ServerSentEventsServer_KeepaliveModeAlwaysKeepaliveKindCommentKeepaliveContentCustom_SendsCustomCommentKeepalive ( )
131+ {
132+ using FakeServerSentEventsServerrApplicationFactory < KeepaliveCustomCommentAlwaysServerSentEventsServerStartup > serverSentEventsServerApplicationFactory = new ( ) ;
133+ HttpClient serverSentEventsClient = serverSentEventsServerApplicationFactory . CreateClient ( ) ;
134+
135+ string serverSentEvents = await GetServerSentEvents ( serverSentEventsClient ) . ConfigureAwait ( false ) ;
136+
137+ Assert . Matches ( $ "^({ CUSTOM_KEEPALIVE_COMMENT } )+$", serverSentEvents ) ;
138+ }
139+
140+ [ Fact ]
141+ public async Task ServerSentEventsServer_KeepaliveModeAlwaysKeepaliveKindEventKeepaliveContentCustom_SendsCustomEventKeepalive ( )
142+ {
143+ using FakeServerSentEventsServerrApplicationFactory < KeepaliveCustomEventAlwaysServerSentEventsServerStartup > serverSentEventsServerApplicationFactory = new ( ) ;
144+ HttpClient serverSentEventsClient = serverSentEventsServerApplicationFactory . CreateClient ( ) ;
145+
146+ string serverSentEvents = await GetServerSentEvents ( serverSentEventsClient ) . ConfigureAwait ( false ) ;
147+
148+ Assert . Matches ( $ "^({ CUSTOM_KEEPALIVE_EVENT } )+$", serverSentEvents ) ;
149+ }
150+
86151 private static async Task < string > GetServerSentEvents ( HttpClient serverSentEventsClient )
87152 {
88153 Stopwatch keepaliveStopwatch = new Stopwatch ( ) ;
@@ -103,9 +168,8 @@ private static async Task<string> GetServerSentEvents(HttpClient serverSentEvent
103168
104169 try
105170 {
106- CancellationTokenSource keepaliveCancellationTokenSource = new CancellationTokenSource ( ) ;
107- keepaliveCancellationTokenSource . CancelAfter ( KEEPALIVE_TIMESPAN ) ;
108-
171+ using CancellationTokenSource keepaliveCancellationTokenSource = new CancellationTokenSource ( KEEPALIVE_TIMESPAN ) ;
172+
109173 int bytesRead = await responseStream . ReadAsync ( buffer , 0 , buffer . Length , keepaliveCancellationTokenSource . Token ) . ConfigureAwait ( false ) ;
110174 serverSentEventsResponseContent += Encoding . UTF8 . GetString ( buffer , 0 , bytesRead ) ;
111175 }
0 commit comments