|
| 1 | +// DiffEngineTray is the obsolete public shim, but its IsRunning is still where the tray check |
| 2 | +// lives, and this test has to move it. |
| 3 | +#pragma warning disable CS0618 |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// <see cref="DiffRunner.TrayDisabled" />: a process that wants a diff tool launched but does not |
| 7 | +/// want the tray collecting what it produces. |
| 8 | +/// <para> |
| 9 | +/// The case it exists for is a test suite driving a library that stages snapshots. Turning diff off |
| 10 | +/// is not the same switch: in Verify it also turns off the inline staging such a suite exists to |
| 11 | +/// test, and it does not stop the tracking anyway, since every exit of |
| 12 | +/// <c>DiffRunner.InnerLaunch</c> - <c>Disabled</c> included - still adds the move. So a developer |
| 13 | +/// box collected a pending move per snapshot per run, each pointing at a throwaway directory, and |
| 14 | +/// each offering an accept that would write to it. |
| 15 | +/// </para> |
| 16 | +/// </summary> |
| 17 | +[NotInParallel] |
| 18 | +public class TrayDisabledTests |
| 19 | +{ |
| 20 | + const string Variable = "DiffEngine_TrayDisabled"; |
| 21 | + |
| 22 | + [Test] |
| 23 | + public async Task Read_from_the_environment_until_set() |
| 24 | + { |
| 25 | + DiffRunner.ResetTrayDisabled(); |
| 26 | + |
| 27 | + Environment.SetEnvironmentVariable(Variable, "true"); |
| 28 | + await Assert.That(DiffRunner.TrayDisabled).IsTrue(); |
| 29 | + |
| 30 | + // Setting pins it, exactly as Disabled does, so a consumer that opts back in is not |
| 31 | + // overruled by the machine it runs on. |
| 32 | + DiffRunner.TrayDisabled = false; |
| 33 | + await Assert.That(DiffRunner.TrayDisabled).IsFalse(); |
| 34 | + } |
| 35 | + |
| 36 | + [Test] |
| 37 | + public async Task A_disabled_tray_leaves_the_move_to_the_queue_owner() |
| 38 | + { |
| 39 | + await Assert.That(ViewerServer.TryBind(0, out var bound)).IsTrue(); |
| 40 | + using var server = bound!; |
| 41 | + using var cancel = new CancelSource(); |
| 42 | + |
| 43 | + var heardByOwner = new ConcurrentBag<string>(); |
| 44 | + var listening = server.Listen( |
| 45 | + _ => |
| 46 | + { |
| 47 | + heardByOwner.Add($"{_.Verb}:{_.Key}"); |
| 48 | + return ViewerResponse.Success(); |
| 49 | + }, |
| 50 | + cancel.Token); |
| 51 | + |
| 52 | + using var tray = new PiperListener(); |
| 53 | + |
| 54 | + var previousPort = PiperClient.Port; |
| 55 | + var previousViewerPort = Environment.GetEnvironmentVariable(ViewerClient.PortVariable); |
| 56 | + var previousRunning = DiffEngineTray.IsRunning; |
| 57 | + try |
| 58 | + { |
| 59 | + // A tray that is running and really would take it, so what follows is the switch |
| 60 | + // rather than an absent tray. |
| 61 | + PiperClient.Port = tray.Port; |
| 62 | + DiffEngineTray.IsRunning = true; |
| 63 | + Environment.SetEnvironmentVariable(ViewerClient.PortVariable, server.Port.ToString()); |
| 64 | + |
| 65 | + DiffRunner.TrayDisabled = false; |
| 66 | + await PendingFiles.AddMoveAsync("taken.txt", "target.txt", null, null, false, null, cancel.Token); |
| 67 | + |
| 68 | + await tray.WaitFor(1); |
| 69 | + await Assert.That(heardByOwner.Count).IsEqualTo(0); |
| 70 | + |
| 71 | + DiffRunner.TrayDisabled = true; |
| 72 | + await PendingFiles.AddMoveAsync("skipped.txt", "target.txt", null, null, false, null, cancel.Token); |
| 73 | + |
| 74 | + // The owner took the second one, which is the fallback branch for no tray at all. |
| 75 | + await Assert.That(heardByOwner.Count).IsEqualTo(1); |
| 76 | + await Assert.That(heardByOwner).Contains(_ => _.StartsWith("Move:", StringComparison.Ordinal)); |
| 77 | + |
| 78 | + // And the tray still holds only the first. Asserted after the owner heard the second, |
| 79 | + // because the piper decision is made before that send, so by here it has happened. |
| 80 | + await Assert.That(tray.Payloads.Count).IsEqualTo(1); |
| 81 | + await Assert.That(tray.Payloads).Contains(_ => _.Contains("taken.txt", StringComparison.Ordinal)); |
| 82 | + } |
| 83 | + finally |
| 84 | + { |
| 85 | + PiperClient.Port = previousPort; |
| 86 | + DiffEngineTray.IsRunning = previousRunning; |
| 87 | + Environment.SetEnvironmentVariable(ViewerClient.PortVariable, previousViewerPort); |
| 88 | + await cancel.CancelAsync(); |
| 89 | + try |
| 90 | + { |
| 91 | + // No token: the line above already cancelled it, so passing it here would return |
| 92 | + // before the listener had unwound rather than waiting for it to. The timeout is |
| 93 | + // what bounds the drain |
| 94 | + // ReSharper disable once MethodSupportsCancellation |
| 95 | + await listening.WaitAsync(TimeSpan.FromSeconds(5)); |
| 96 | + } |
| 97 | + catch (Exception exception) |
| 98 | + when (exception is OperationCanceledException or TimeoutException) |
| 99 | + { |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + /// <summary> |
| 105 | + /// Every other test in this assembly runs with the ambient value, which the module initializer |
| 106 | + /// leaves alone. |
| 107 | + /// </summary> |
| 108 | + [After(Test)] |
| 109 | + public void Restore() |
| 110 | + { |
| 111 | + Environment.SetEnvironmentVariable(Variable, null); |
| 112 | + DiffRunner.ResetTrayDisabled(); |
| 113 | + } |
| 114 | + |
| 115 | + /// <summary> |
| 116 | + /// Stands in for the tray's PiperServer: the payload is written one way and never answered, so |
| 117 | + /// accepting the connection and reading it to the end is the whole protocol from this side. |
| 118 | + /// </summary> |
| 119 | + sealed class PiperListener : IDisposable |
| 120 | + { |
| 121 | + readonly TcpListener listener; |
| 122 | + readonly CancelSource cancellation = new(); |
| 123 | + readonly Task loop; |
| 124 | + |
| 125 | + public PiperListener() |
| 126 | + { |
| 127 | + listener = new(IPAddress.Loopback, 0); |
| 128 | + listener.Start(); |
| 129 | + Port = ((IPEndPoint) listener.LocalEndpoint).Port; |
| 130 | + loop = Task.Run(Accept); |
| 131 | + } |
| 132 | + |
| 133 | + public int Port { get; } |
| 134 | + |
| 135 | + public ConcurrentBag<string> Payloads { get; } = []; |
| 136 | + |
| 137 | + public async Task WaitFor(int count) |
| 138 | + { |
| 139 | + for (var attempt = 0; attempt < 250; attempt++) |
| 140 | + { |
| 141 | + if (Payloads.Count >= count) |
| 142 | + { |
| 143 | + return; |
| 144 | + } |
| 145 | + |
| 146 | + await Task.Delay(20); |
| 147 | + } |
| 148 | + |
| 149 | + throw new($"Only {Payloads.Count} payloads reached the tray, expected {count}."); |
| 150 | + } |
| 151 | + |
| 152 | + async Task Accept() |
| 153 | + { |
| 154 | + while (!cancellation.IsCancellationRequested) |
| 155 | + { |
| 156 | + try |
| 157 | + { |
| 158 | + // No token: the cancellable overload is net6 and up, and this compiles for |
| 159 | + // net48 too. Stop in Dispose is what breaks the accept, which lands in the |
| 160 | + // catch below. |
| 161 | + using var client = await listener.AcceptTcpClientAsync(); |
| 162 | + using var stream = client.GetStream(); |
| 163 | + using var reader = new StreamReader(stream); |
| 164 | + Payloads.Add(await reader.ReadToEndAsync()); |
| 165 | + } |
| 166 | + catch (Exception exception) |
| 167 | + when (exception is OperationCanceledException or ObjectDisposedException or SocketException) |
| 168 | + { |
| 169 | + return; |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + public void Dispose() |
| 175 | + { |
| 176 | + cancellation.Cancel(); |
| 177 | + listener.Stop(); |
| 178 | + try |
| 179 | + { |
| 180 | + loop.Wait(TimeSpan.FromSeconds(2)); |
| 181 | + } |
| 182 | + catch (AggregateException) |
| 183 | + { |
| 184 | + } |
| 185 | + |
| 186 | + cancellation.Dispose(); |
| 187 | + } |
| 188 | + } |
| 189 | +} |
0 commit comments