|
1 | 1 | using Moq; |
2 | 2 | using System; |
3 | 3 | using System.IO; |
| 4 | +using System.Reflection; |
4 | 5 | using Xunit; |
5 | 6 |
|
6 | 7 | namespace GitHub.Runner.Common.Tests.Listener |
@@ -126,5 +127,215 @@ public void ShipEmptyLog() |
126 | 127 | CleanLogFolder(); |
127 | 128 | } |
128 | 129 | } |
| 130 | + |
| 131 | + // Dispose without End() should flush/queue partial content. |
| 132 | + [Fact] |
| 133 | + [Trait("Level", "L0")] |
| 134 | + [Trait("Category", "Common")] |
| 135 | + public void Dispose_AfterPartialWrite_FlushesAndClosesFiles() |
| 136 | + { |
| 137 | + CleanLogFolder(); |
| 138 | + |
| 139 | + try |
| 140 | + { |
| 141 | + using (var hc = new TestHostContext(this)) |
| 142 | + { |
| 143 | + var pagingLogger = new PagingLogger(); |
| 144 | + hc.SetSingleton<IJobServerQueue>(_jobServerQueue.Object); |
| 145 | + pagingLogger.Initialize(hc); |
| 146 | + Guid timeLineId = Guid.NewGuid(); |
| 147 | + Guid timeLineRecordId = Guid.NewGuid(); |
| 148 | + |
| 149 | + string queuedPagePath = null; |
| 150 | + _jobServerQueue |
| 151 | + .Setup(x => x.QueueFileUpload(timeLineId, timeLineRecordId, It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), true)) |
| 152 | + .Callback((Guid _, Guid _, string _, string _, string path, bool _) => queuedPagePath = path); |
| 153 | + |
| 154 | + string queuedBlockPath = null; |
| 155 | + _jobServerQueue |
| 156 | + .Setup(x => x.QueueResultsUpload(timeLineRecordId, It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), true, It.IsAny<bool>(), It.IsAny<bool>(), It.IsAny<long>())) |
| 157 | + .Callback((Guid _, string _, string path, string _, bool _, bool _, bool _, long _) => queuedBlockPath = path); |
| 158 | + |
| 159 | + // Act: write once, then dispose without End(). |
| 160 | + pagingLogger.Setup(timeLineId, timeLineRecordId); |
| 161 | + pagingLogger.Write(LogData); |
| 162 | + pagingLogger.Dispose(); |
| 163 | + |
| 164 | + Assert.False(string.IsNullOrEmpty(queuedPagePath), "Dispose should have queued the partial page for upload."); |
| 165 | + Assert.False(string.IsNullOrEmpty(queuedBlockPath), "Dispose should have queued the partial block for upload with finalize=true."); |
| 166 | + |
| 167 | + // Verify flushed content reached queued files. |
| 168 | + Assert.Contains(LogData, File.ReadAllText(queuedPagePath)); |
| 169 | + Assert.Contains(LogData, File.ReadAllText(queuedBlockPath)); |
| 170 | + |
| 171 | + // Cleanup files created outside the normal callback path. |
| 172 | + File.Delete(queuedPagePath); |
| 173 | + File.Delete(queuedBlockPath); |
| 174 | + } |
| 175 | + } |
| 176 | + finally |
| 177 | + { |
| 178 | + CleanLogFolder(); |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + // Dispose should be idempotent. |
| 183 | + [Fact] |
| 184 | + [Trait("Level", "L0")] |
| 185 | + [Trait("Category", "Common")] |
| 186 | + public void Dispose_IsIdempotent() |
| 187 | + { |
| 188 | + CleanLogFolder(); |
| 189 | + |
| 190 | + try |
| 191 | + { |
| 192 | + using (var hc = new TestHostContext(this)) |
| 193 | + { |
| 194 | + var pagingLogger = new PagingLogger(); |
| 195 | + hc.SetSingleton<IJobServerQueue>(_jobServerQueue.Object); |
| 196 | + pagingLogger.Initialize(hc); |
| 197 | + Guid timeLineId = Guid.NewGuid(); |
| 198 | + Guid timeLineRecordId = Guid.NewGuid(); |
| 199 | + |
| 200 | + int queueFileUploadCount = 0; |
| 201 | + int queueResultsUploadCount = 0; |
| 202 | + _jobServerQueue |
| 203 | + .Setup(x => x.QueueFileUpload(timeLineId, timeLineRecordId, It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), true)) |
| 204 | + .Callback(() => queueFileUploadCount++); |
| 205 | + _jobServerQueue |
| 206 | + .Setup(x => x.QueueResultsUpload(timeLineRecordId, It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), true, It.IsAny<bool>(), It.IsAny<bool>(), It.IsAny<long>())) |
| 207 | + .Callback(() => queueResultsUploadCount++); |
| 208 | + |
| 209 | + pagingLogger.Setup(timeLineId, timeLineRecordId); |
| 210 | + pagingLogger.Write(LogData); |
| 211 | + pagingLogger.Dispose(); |
| 212 | + pagingLogger.Dispose(); |
| 213 | + |
| 214 | + Assert.Equal(1, queueFileUploadCount); |
| 215 | + Assert.Equal(1, queueResultsUploadCount); |
| 216 | + } |
| 217 | + } |
| 218 | + finally |
| 219 | + { |
| 220 | + CleanLogFolder(); |
| 221 | + } |
| 222 | + } |
| 223 | + |
| 224 | + // Dispose after End() should be a no-op. |
| 225 | + [Fact] |
| 226 | + [Trait("Level", "L0")] |
| 227 | + [Trait("Category", "Common")] |
| 228 | + public void Dispose_AfterEnd_IsNoOp() |
| 229 | + { |
| 230 | + CleanLogFolder(); |
| 231 | + |
| 232 | + try |
| 233 | + { |
| 234 | + using (var hc = new TestHostContext(this)) |
| 235 | + { |
| 236 | + var pagingLogger = new PagingLogger(); |
| 237 | + hc.SetSingleton<IJobServerQueue>(_jobServerQueue.Object); |
| 238 | + pagingLogger.Initialize(hc); |
| 239 | + Guid timeLineId = Guid.NewGuid(); |
| 240 | + Guid timeLineRecordId = Guid.NewGuid(); |
| 241 | + |
| 242 | + int queueCount = 0; |
| 243 | + _jobServerQueue |
| 244 | + .Setup(x => x.QueueFileUpload(timeLineId, timeLineRecordId, It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), true)) |
| 245 | + .Callback(() => queueCount++); |
| 246 | + |
| 247 | + pagingLogger.Setup(timeLineId, timeLineRecordId); |
| 248 | + pagingLogger.Write(LogData); |
| 249 | + pagingLogger.End(); |
| 250 | + int afterEnd = queueCount; |
| 251 | + |
| 252 | + pagingLogger.Dispose(); |
| 253 | + |
| 254 | + Assert.Equal(afterEnd, queueCount); |
| 255 | + } |
| 256 | + } |
| 257 | + finally |
| 258 | + { |
| 259 | + CleanLogFolder(); |
| 260 | + } |
| 261 | + } |
| 262 | + |
| 263 | + // Dispose before Write() should not queue uploads. |
| 264 | + [Fact] |
| 265 | + [Trait("Level", "L0")] |
| 266 | + [Trait("Category", "Common")] |
| 267 | + public void Dispose_BeforeAnyWrite_DoesNotThrow() |
| 268 | + { |
| 269 | + CleanLogFolder(); |
| 270 | + |
| 271 | + try |
| 272 | + { |
| 273 | + using (var hc = new TestHostContext(this)) |
| 274 | + { |
| 275 | + var pagingLogger = new PagingLogger(); |
| 276 | + hc.SetSingleton<IJobServerQueue>(_jobServerQueue.Object); |
| 277 | + pagingLogger.Initialize(hc); |
| 278 | + |
| 279 | + int queueCount = 0; |
| 280 | + _jobServerQueue |
| 281 | + .Setup(x => x.QueueFileUpload(It.IsAny<Guid>(), It.IsAny<Guid>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>())) |
| 282 | + .Callback(() => queueCount++); |
| 283 | + |
| 284 | + pagingLogger.Setup(Guid.NewGuid(), Guid.NewGuid()); |
| 285 | + pagingLogger.Dispose(); |
| 286 | + |
| 287 | + Assert.Equal(0, queueCount); |
| 288 | + } |
| 289 | + } |
| 290 | + finally |
| 291 | + { |
| 292 | + CleanLogFolder(); |
| 293 | + } |
| 294 | + } |
| 295 | + |
| 296 | + // Safety net: close orphaned _pageData when _pageWriter was never assigned. |
| 297 | + [Fact] |
| 298 | + [Trait("Level", "L0")] |
| 299 | + [Trait("Category", "Common")] |
| 300 | + public void Dispose_ReleasesOrphanedFileStream_WhenWriterWasNeverAssigned() |
| 301 | + { |
| 302 | + CleanLogFolder(); |
| 303 | + |
| 304 | + try |
| 305 | + { |
| 306 | + using (var hc = new TestHostContext(this)) |
| 307 | + { |
| 308 | + var pagingLogger = new PagingLogger(); |
| 309 | + hc.SetSingleton<IJobServerQueue>(_jobServerQueue.Object); |
| 310 | + pagingLogger.Initialize(hc); |
| 311 | + pagingLogger.Setup(Guid.NewGuid(), Guid.NewGuid()); |
| 312 | + |
| 313 | + // Force partial-init state: _pageData set, _pageWriter null. |
| 314 | + var pagesFolder = Path.Combine(hc.GetDirectory(WellKnownDirectory.Diag), PagingLogger.PagingFolder); |
| 315 | + Directory.CreateDirectory(pagesFolder); |
| 316 | + var orphanPath = Path.Combine(pagesFolder, $"orphan-{Guid.NewGuid():N}.log"); |
| 317 | + var orphanStream = new FileStream(orphanPath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); |
| 318 | + |
| 319 | + typeof(PagingLogger) |
| 320 | + .GetField("_pageData", BindingFlags.NonPublic | BindingFlags.Instance) |
| 321 | + .SetValue(pagingLogger, orphanStream); |
| 322 | + |
| 323 | + pagingLogger.Dispose(); |
| 324 | + |
| 325 | + var closedStream = (FileStream)typeof(PagingLogger) |
| 326 | + .GetField("_pageData", BindingFlags.NonPublic | BindingFlags.Instance) |
| 327 | + .GetValue(pagingLogger); |
| 328 | + Assert.Null(closedStream); |
| 329 | + |
| 330 | + Assert.True(orphanStream.SafeFileHandle.IsClosed, "Dispose should have closed the orphaned FileStream's handle."); |
| 331 | + File.Delete(orphanPath); |
| 332 | + } |
| 333 | + } |
| 334 | + finally |
| 335 | + { |
| 336 | + CleanLogFolder(); |
| 337 | + } |
| 338 | + } |
| 339 | + |
129 | 340 | } |
130 | 341 | } |
0 commit comments