|
| 1 | +using Microsoft.Extensions.Logging; |
| 2 | +using UICatalog; |
| 3 | + |
| 4 | +namespace IntegrationTests; |
| 5 | + |
| 6 | +public class ScenarioLogCaptureTests |
| 7 | +{ |
| 8 | + [Fact] |
| 9 | + public void LogBuffer_IsTrimmed_WhenItGrowsTooLarge () |
| 10 | + { |
| 11 | + ScenarioLogCapture capture = new (); |
| 12 | + ILogger logger = capture.CreateLogger ("test"); |
| 13 | + string message = new ('x', 4096); |
| 14 | + |
| 15 | + for (var i = 0; i < 120; i++) |
| 16 | + { |
| 17 | + logger.LogInformation ("{Message}", message); |
| 18 | + } |
| 19 | + |
| 20 | + Assert.True (capture.GetAllLogs ().Length <= 256_000); |
| 21 | + } |
| 22 | + |
| 23 | + [Fact] |
| 24 | + public void GetScenarioLogs_RespectsScenarioStart_AfterTrim () |
| 25 | + { |
| 26 | + ScenarioLogCapture capture = new (); |
| 27 | + ILogger logger = capture.CreateLogger ("test"); |
| 28 | + string message = new ('y', 4096); |
| 29 | + |
| 30 | + // Fill close to the cap so the next few entries will trigger a trim. |
| 31 | + while (capture.GetAllLogs ().Length < 250_000) |
| 32 | + { |
| 33 | + logger.LogInformation ("{Message}", message); |
| 34 | + } |
| 35 | + |
| 36 | + logger.LogInformation ("before-start"); |
| 37 | + |
| 38 | + capture.MarkScenarioStart (); |
| 39 | + |
| 40 | + int lengthBefore = capture.GetAllLogs ().Length; |
| 41 | + bool trimmed = false; |
| 42 | + |
| 43 | + for (int i = 0; i < 50; i++) |
| 44 | + { |
| 45 | + logger.LogInformation ("{Message}", message); |
| 46 | + |
| 47 | + int lengthAfter = capture.GetAllLogs ().Length; |
| 48 | + |
| 49 | + if (lengthAfter < lengthBefore) |
| 50 | + { |
| 51 | + trimmed = true; |
| 52 | + break; |
| 53 | + } |
| 54 | + |
| 55 | + lengthBefore = lengthAfter; |
| 56 | + } |
| 57 | + |
| 58 | + Assert.True (trimmed); |
| 59 | + |
| 60 | + logger.LogInformation ("after-start"); |
| 61 | + |
| 62 | + string logs = capture.GetScenarioLogs (); |
| 63 | + Assert.DoesNotContain ("before-start", logs); |
| 64 | + Assert.Contains ("after-start", logs); |
| 65 | + } |
| 66 | +} |
0 commit comments