|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Text; |
| 4 | +using System.Buffers; |
| 5 | +using System.Net.Http; |
| 6 | +using System.Threading; |
| 7 | +using System.Diagnostics; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using Microsoft.Extensions.Configuration; |
| 10 | +using Xunit; |
| 11 | +using Lib.AspNetCore.ServerSentEvents; |
| 12 | +using Test.AspNetCore.ServerSentEvents.Functional.Infrastructure; |
| 13 | + |
| 14 | +namespace Test.AspNetCore.ServerSentEvents.Functional |
| 15 | +{ |
| 16 | + public class KeepAliveTests |
| 17 | + { |
| 18 | + #region Fields |
| 19 | + private const int KEEPALIVE_INTERVAL = 1; |
| 20 | + private readonly static TimeSpan KEEPALIVE_TIMESPAN = TimeSpan.FromSeconds(KEEPALIVE_INTERVAL + 1); |
| 21 | + |
| 22 | + 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\ndata: \r\n\r\n"; |
| 26 | + #endregion |
| 27 | + |
| 28 | + #region SUT |
| 29 | + private class KeepaliveNeverServerSentEventsServerStartup : FakeServerSentEventsServerStartup |
| 30 | + { |
| 31 | + public KeepaliveNeverServerSentEventsServerStartup(IConfiguration configuration) : base(configuration) |
| 32 | + { } |
| 33 | + |
| 34 | + protected override Action<ServerSentEventsServiceOptions<ServerSentEventsService>> ConfigureServerSentEventsOption |
| 35 | + { |
| 36 | + get |
| 37 | + { |
| 38 | + return options => |
| 39 | + { |
| 40 | + options.KeepaliveMode = ServerSentEventsKeepaliveMode.Never; |
| 41 | + options.KeepaliveInterval = KEEPALIVE_INTERVAL; |
| 42 | + }; |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + private class KeepaliveDefultAlwaysServerSentEventsServerStartup : FakeServerSentEventsServerStartup |
| 48 | + { |
| 49 | + public KeepaliveDefultAlwaysServerSentEventsServerStartup(IConfiguration configuration) : base(configuration) |
| 50 | + { } |
| 51 | + |
| 52 | + protected override Action<ServerSentEventsServiceOptions<ServerSentEventsService>> ConfigureServerSentEventsOption |
| 53 | + { |
| 54 | + get |
| 55 | + { |
| 56 | + return options => |
| 57 | + { |
| 58 | + options.KeepaliveMode = ServerSentEventsKeepaliveMode.Always; |
| 59 | + options.KeepaliveInterval = KEEPALIVE_INTERVAL; |
| 60 | + }; |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 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 | + } |
| 104 | + #endregion |
| 105 | + |
| 106 | + #region Tests |
| 107 | + [Fact] |
| 108 | + public async Task ServerSentEventsServer_KeepaliveModeNever_DoesNotSendKeepalive() |
| 109 | + { |
| 110 | + using FakeServerSentEventsServerrApplicationFactory<KeepaliveNeverServerSentEventsServerStartup> serverSentEventsServerApplicationFactory = new(); |
| 111 | + HttpClient serverSentEventsClient = serverSentEventsServerApplicationFactory.CreateClient(); |
| 112 | + |
| 113 | + string serverSentEvents = await GetServerSentEvents(serverSentEventsClient).ConfigureAwait(false); |
| 114 | + |
| 115 | + Assert.Equal(String.Empty, serverSentEvents); |
| 116 | + } |
| 117 | + |
| 118 | + [Fact] |
| 119 | + public async Task ServerSentEventsServer_KeepaliveModeAlways_SendsDefaultKeepalive() |
| 120 | + { |
| 121 | + using FakeServerSentEventsServerrApplicationFactory<KeepaliveDefultAlwaysServerSentEventsServerStartup> serverSentEventsServerApplicationFactory = new (); |
| 122 | + HttpClient serverSentEventsClient = serverSentEventsServerApplicationFactory.CreateClient(); |
| 123 | + |
| 124 | + string serverSentEvents = await GetServerSentEvents(serverSentEventsClient).ConfigureAwait(false); |
| 125 | + |
| 126 | + Assert.Matches($"^({DEFAULT_KEEPALIVE})+$", serverSentEvents); |
| 127 | + } |
| 128 | + |
| 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 | + |
| 151 | + private static async Task<string> GetServerSentEvents(HttpClient serverSentEventsClient) |
| 152 | + { |
| 153 | + Stopwatch keepaliveStopwatch = new Stopwatch(); |
| 154 | + string serverSentEventsResponseContent = String.Empty; |
| 155 | + |
| 156 | + serverSentEventsClient.DefaultRequestHeaders.Add("Accept", "text/event-stream"); |
| 157 | + using (HttpResponseMessage serverSentEventsResponse = await serverSentEventsClient.GetAsync(FakeServerSentEventsServerStartup.SERVER_SENT_EVENTS_ENDPOINT, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false)) |
| 158 | + { |
| 159 | + serverSentEventsResponse.EnsureSuccessStatusCode(); |
| 160 | + |
| 161 | + keepaliveStopwatch.Start(); |
| 162 | + |
| 163 | + using (Stream responseStream = await serverSentEventsResponse.Content.ReadAsStreamAsync().ConfigureAwait(false)) |
| 164 | + { |
| 165 | + do |
| 166 | + { |
| 167 | + byte[] buffer = ArrayPool<byte>.Shared.Rent(128); |
| 168 | + |
| 169 | + try |
| 170 | + { |
| 171 | + using CancellationTokenSource keepaliveCancellationTokenSource = new CancellationTokenSource(KEEPALIVE_TIMESPAN); |
| 172 | + |
| 173 | + int bytesRead = await responseStream.ReadAsync(buffer, 0, buffer.Length, keepaliveCancellationTokenSource.Token).ConfigureAwait(false); |
| 174 | + serverSentEventsResponseContent += Encoding.UTF8.GetString(buffer, 0, bytesRead); |
| 175 | + } |
| 176 | + catch (Exception ex) when (ex is OperationCanceledException || ex.InnerException is OperationCanceledException) |
| 177 | + { } |
| 178 | + |
| 179 | + ArrayPool<byte>.Shared.Return(buffer); |
| 180 | + } while (keepaliveStopwatch.Elapsed < KEEPALIVE_TIMESPAN); |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + keepaliveStopwatch.Stop(); |
| 185 | + |
| 186 | + return serverSentEventsResponseContent; |
| 187 | + } |
| 188 | + #endregion |
| 189 | + } |
| 190 | +} |
0 commit comments