|
| 1 | +using System; |
| 2 | +using System.Threading.Tasks; |
| 3 | +using OzaLog.Core; |
| 4 | +using Xunit; |
| 5 | + |
| 6 | +namespace OzaLog.Tests |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// v3.3.0 新增的宿主端生命週期 API:<c>LOG.Flush</c> / <c>LOG.FlushAsync</c> / |
| 10 | + /// <c>LOG.Shutdown</c> / <c>LOG.ShutdownAsync</c> / <c>LOG.IsShutdown</c>。 |
| 11 | + /// </summary> |
| 12 | + /// <remarks> |
| 13 | + /// 這個類別會操作行程級的全域狀態(收尾整條管線),因此: |
| 14 | + /// • 組件層級已關閉平行測試(見 AssemblyInfo.cs) |
| 15 | + /// • 每個會收尾的測試都在 finally 裡用 Configure 把系統復原,讓後續測試不受影響 |
| 16 | + /// </remarks> |
| 17 | + public class LifecycleTests |
| 18 | + { |
| 19 | + private const int EntryCount = 1000; |
| 20 | + |
| 21 | + /// <summary>收尾後把系統復原成可寫入狀態(Shutdown 之後才允許再次 Configure)。</summary> |
| 22 | + private static void RestoreLogging() |
| 23 | + { |
| 24 | + if (LOG.IsShutdown) |
| 25 | + LOG.Configure(_ => { }); |
| 26 | + } |
| 27 | + |
| 28 | + [Fact] |
| 29 | + public void FlushReturnsOnlyAfterEveryQueuedEntryHasLanded() |
| 30 | + { |
| 31 | + var name = "Flush" + Guid.NewGuid().ToString("N").Substring(0, 8); |
| 32 | + |
| 33 | + for (var i = 0; i < EntryCount; i++) |
| 34 | + LOG.CustomName_Log(name, "flush regression " + i.ToString()); |
| 35 | + |
| 36 | + Assert.True(LOG.Flush(), "Flush 應在佇列排空後回 true"); |
| 37 | + |
| 38 | + var file = LogFileProbe.FindLogFile(name); |
| 39 | + Assert.NotNull(file); |
| 40 | + |
| 41 | + var lines = LogFileProbe.ReadAllTextShared(file) |
| 42 | + .Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); |
| 43 | + |
| 44 | + // Flush 回來的當下就必須是 1000 行:不補 sleep、不重讀 |
| 45 | + Assert.Equal(EntryCount, lines.Length); |
| 46 | + } |
| 47 | + |
| 48 | + [Fact] |
| 49 | + public async Task FlushAsyncReturnsOnlyAfterEveryQueuedEntryHasLanded() |
| 50 | + { |
| 51 | + var name = "FlushAsync" + Guid.NewGuid().ToString("N").Substring(0, 8); |
| 52 | + |
| 53 | + for (var i = 0; i < EntryCount; i++) |
| 54 | + LOG.CustomName_Log(name, "flush async regression " + i.ToString()); |
| 55 | + |
| 56 | + Assert.True(await LOG.FlushAsync()); |
| 57 | + |
| 58 | + var file = LogFileProbe.FindLogFile(name); |
| 59 | + Assert.NotNull(file); |
| 60 | + |
| 61 | + var lines = LogFileProbe.ReadAllTextShared(file) |
| 62 | + .Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); |
| 63 | + |
| 64 | + Assert.Equal(EntryCount, lines.Length); |
| 65 | + } |
| 66 | + |
| 67 | + [Fact] |
| 68 | + public void WritesAfterShutdownAreSilentlyDiscarded() |
| 69 | + { |
| 70 | + var marker = "after_shutdown_" + Guid.NewGuid().ToString("N").Substring(0, 8); |
| 71 | + |
| 72 | + try |
| 73 | + { |
| 74 | + Assert.True(LOG.Shutdown()); |
| 75 | + Assert.True(LOG.IsShutdown); |
| 76 | + |
| 77 | + // 收尾之後的寫入不可擲例外(背景服務型套件不能把宿主弄掛) |
| 78 | + var ex = Record.Exception(() => |
| 79 | + { |
| 80 | + LOG.Trace_Log(marker); |
| 81 | + LOG.Info_Log(marker); |
| 82 | + LOG.Warn_Log(marker); |
| 83 | + LOG.Error_Log(marker); |
| 84 | + LOG.Fatal_Log(marker); |
| 85 | + LOG.CustomName_Log("AfterShutdown", marker); |
| 86 | + LOG.Info_Log(marker, args: null, writeTxt: true, immediateFlush: true); |
| 87 | + LOG.Error_Log("with object", new InvalidOperationException(marker)); |
| 88 | + }); |
| 89 | + Assert.Null(ex); |
| 90 | + |
| 91 | + // 靜默丟棄:內容不會出現在任何日誌檔 |
| 92 | + Assert.Equal(0, LogFileProbe.CountOccurrences(marker)); |
| 93 | + |
| 94 | + // 已收尾時 Flush 明確回 false(而不是假裝成功) |
| 95 | + Assert.False(LOG.Flush()); |
| 96 | + } |
| 97 | + finally |
| 98 | + { |
| 99 | + RestoreLogging(); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + [Fact] |
| 104 | + public void ShutdownIsIdempotentAlongsideTheProcessExitCleanup() |
| 105 | + { |
| 106 | + try |
| 107 | + { |
| 108 | + LOG.Info_Log("idempotent shutdown probe"); |
| 109 | + |
| 110 | + Assert.True(LOG.Shutdown()); // 第一次真的收尾 |
| 111 | + Assert.False(LOG.Shutdown()); // 第二次冪等,回 false 不是錯誤 |
| 112 | + |
| 113 | + // 模擬 ProcessExit / UnhandledException 已註冊的收尾在 Shutdown 之後才被觸發 |
| 114 | + var ex = Record.Exception(() => |
| 115 | + { |
| 116 | + AsyncLogHandler.ShutdownGracefully(); |
| 117 | + QuoteLogHandler.ShutdownGracefully(); |
| 118 | + LOG.Shutdown(); |
| 119 | + }); |
| 120 | + |
| 121 | + Assert.Null(ex); |
| 122 | + Assert.True(LOG.IsShutdown); |
| 123 | + } |
| 124 | + finally |
| 125 | + { |
| 126 | + RestoreLogging(); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + [Fact] |
| 131 | + public async Task ShutdownAsyncIsIdempotent() |
| 132 | + { |
| 133 | + try |
| 134 | + { |
| 135 | + LOG.Info_Log("idempotent async shutdown probe"); |
| 136 | + |
| 137 | + Assert.True(await LOG.ShutdownAsync()); |
| 138 | + Assert.False(await LOG.ShutdownAsync()); |
| 139 | + Assert.True(LOG.IsShutdown); |
| 140 | + } |
| 141 | + finally |
| 142 | + { |
| 143 | + RestoreLogging(); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + [Fact] |
| 148 | + public void ConfigureStaysNonReentrantUntilShutdown() |
| 149 | + { |
| 150 | + try |
| 151 | + { |
| 152 | + // 先收尾把狀態歸零,這樣不論前面哪個測試先跑,下一行的 Configure 一定是「第一次」 |
| 153 | + LOG.Shutdown(); |
| 154 | + LOG.Configure(_ => { }); |
| 155 | + |
| 156 | + // 管線還活著時,Configure 仍然不可重入(v3.0 起的 by design 行為不變) |
| 157 | + Assert.Throws<InvalidOperationException>(() => LOG.Configure(_ => { })); |
| 158 | + |
| 159 | + Assert.True(LOG.Shutdown()); |
| 160 | + |
| 161 | + // 收尾之後才允許再次 Configure |
| 162 | + var ex = Record.Exception(() => LOG.Configure(_ => { })); |
| 163 | + Assert.Null(ex); |
| 164 | + Assert.False(LOG.IsShutdown); |
| 165 | + } |
| 166 | + finally |
| 167 | + { |
| 168 | + RestoreLogging(); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + [Fact] |
| 173 | + public void LoggingResumesAfterConfigureFollowingShutdown() |
| 174 | + { |
| 175 | + var marker = "resumed_" + Guid.NewGuid().ToString("N").Substring(0, 8); |
| 176 | + |
| 177 | + try |
| 178 | + { |
| 179 | + LOG.Shutdown(); |
| 180 | + LOG.Configure(_ => { }); |
| 181 | + |
| 182 | + LOG.Info_Log("resume probe " + marker); |
| 183 | + Assert.True(LOG.Flush()); |
| 184 | + |
| 185 | + Assert.Equal(1, LogFileProbe.CountOccurrences(marker)); |
| 186 | + } |
| 187 | + finally |
| 188 | + { |
| 189 | + RestoreLogging(); |
| 190 | + } |
| 191 | + } |
| 192 | + } |
| 193 | +} |
0 commit comments