|
| 1 | +/// <summary> |
| 2 | +/// The tray persisted DiffEngine_MaxInstances on every settings save, whether or not the limit was |
| 3 | +/// part of what changed - the tray's SettingsHelper.Write calls SetForUser unconditionally, and |
| 4 | +/// the value it passes is whatever the options form was populated with, which is the value already |
| 5 | +/// in effect. Saves of unrelated settings go through the same path, including the "always kill |
| 6 | +/// locking processes" prompt, which has no options form at all. So opening the tray once was enough |
| 7 | +/// to leave a user scope variable behind on a machine that had never chosen a limit. |
| 8 | +/// </summary> |
| 9 | +[NotInParallel] |
| 10 | +public class MaxInstanceUserWriteTests |
| 11 | +{ |
| 12 | + const string variable = "DiffEngine_MaxInstances"; |
| 13 | + |
| 14 | + /// <summary> |
| 15 | + /// The case that put the variable on machines that never asked for it: nothing set, so the |
| 16 | + /// form opens at the default and saves it straight back. |
| 17 | + /// </summary> |
| 18 | + [Test] |
| 19 | + public async Task Saving_the_default_on_a_machine_with_nothing_set_writes_nothing() |
| 20 | + { |
| 21 | + Environment.SetEnvironmentVariable(variable, null); |
| 22 | + MaxInstance.ResetAppDomainValue(); |
| 23 | + |
| 24 | + MaxInstance.SetForUser(MaxInstance.MaxInstancesToLaunch); |
| 25 | + |
| 26 | + await Assert.That(Environment.GetEnvironmentVariable(variable)).IsNull(); |
| 27 | + } |
| 28 | + |
| 29 | + [Test] |
| 30 | + public async Task Saving_the_value_already_set_writes_nothing() |
| 31 | + { |
| 32 | + Environment.SetEnvironmentVariable(variable, "10"); |
| 33 | + MaxInstance.ResetAppDomainValue(); |
| 34 | + |
| 35 | + MaxInstance.SetForUser(10); |
| 36 | + |
| 37 | + await Assert.That(Environment.GetEnvironmentVariable(variable)).IsEqualTo("10"); |
| 38 | + await Assert.That(MaxInstance.MaxInstancesToLaunch).IsEqualTo(10); |
| 39 | + } |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// Choosing the default is a way back to an unset machine, rather than a pin at whatever the |
| 43 | + /// default happens to be today. Same as SetTargetOnLeft with false. |
| 44 | + /// </summary> |
| 45 | + [Test] |
| 46 | + public async Task Saving_the_default_clears_a_value_that_was_set() |
| 47 | + { |
| 48 | + var @default = Default(); |
| 49 | + Environment.SetEnvironmentVariable(variable, "10"); |
| 50 | + MaxInstance.ResetAppDomainValue(); |
| 51 | + |
| 52 | + MaxInstance.SetForUser(@default); |
| 53 | + |
| 54 | + await Assert.That(Environment.GetEnvironmentVariable(variable)).IsNull(); |
| 55 | + await Assert.That(MaxInstance.MaxInstancesToLaunch).IsEqualTo(@default); |
| 56 | + } |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// Including a value that was pinned at the default explicitly - it says nothing the absent |
| 60 | + /// variable does not, and leaving it would keep the machine pinned. |
| 61 | + /// </summary> |
| 62 | + [Test] |
| 63 | + public async Task Saving_the_default_over_a_redundant_pin_clears_it() |
| 64 | + { |
| 65 | + var @default = Default(); |
| 66 | + Environment.SetEnvironmentVariable(variable, @default.ToString()); |
| 67 | + MaxInstance.ResetAppDomainValue(); |
| 68 | + |
| 69 | + MaxInstance.SetForUser(@default); |
| 70 | + |
| 71 | + await Assert.That(Environment.GetEnvironmentVariable(variable)).IsNull(); |
| 72 | + } |
| 73 | + |
| 74 | + /// <summary> |
| 75 | + /// What the limit is with nothing set, which is what defaultMax is, without reaching into it. |
| 76 | + /// </summary> |
| 77 | + static int Default() |
| 78 | + { |
| 79 | + Environment.SetEnvironmentVariable(variable, null); |
| 80 | + MaxInstance.ResetAppDomainValue(); |
| 81 | + return MaxInstance.MaxInstancesToLaunch; |
| 82 | + } |
| 83 | + |
| 84 | + /// <summary> |
| 85 | + /// And a save that does change the limit still lands, which is the point of the setting. |
| 86 | + /// </summary> |
| 87 | + [Test] |
| 88 | + public async Task Saving_a_different_value_writes_it() |
| 89 | + { |
| 90 | + Environment.SetEnvironmentVariable(variable, "10"); |
| 91 | + MaxInstance.ResetAppDomainValue(); |
| 92 | + |
| 93 | + MaxInstance.SetForUser(7); |
| 94 | + |
| 95 | + await Assert.That(Environment.GetEnvironmentVariable(variable)).IsEqualTo("7"); |
| 96 | + await Assert.That(MaxInstance.MaxInstancesToLaunch).IsEqualTo(7); |
| 97 | + } |
| 98 | + |
| 99 | + string? original = Environment.GetEnvironmentVariable(variable); |
| 100 | + |
| 101 | + [After(Test)] |
| 102 | + public void Restore() |
| 103 | + { |
| 104 | + Environment.SetEnvironmentVariable(variable, original); |
| 105 | + MaxInstance.ResetAppDomainValue(); |
| 106 | + MaxInstance.ResetCount(); |
| 107 | + } |
| 108 | +} |
0 commit comments