|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Threading; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using Microsoft.JavaScript.NodeApi.Interop; |
| 8 | +using Xunit; |
| 9 | + |
| 10 | +namespace Microsoft.JavaScript.NodeApi.Test; |
| 11 | + |
| 12 | +public class TsfnCallGateTests |
| 13 | +{ |
| 14 | + [Fact] |
| 15 | + public void EnterAndExitWhileOpen() |
| 16 | + { |
| 17 | + var gate = new TsfnCallGate(); |
| 18 | + |
| 19 | + Assert.True(gate.TryEnter()); |
| 20 | + gate.Exit(); |
| 21 | + |
| 22 | + Assert.False(gate.IsClosed); |
| 23 | + } |
| 24 | + |
| 25 | + [Fact] |
| 26 | + public void CloseRejectsFurtherEntry() |
| 27 | + { |
| 28 | + var gate = new TsfnCallGate(); |
| 29 | + |
| 30 | + gate.Close(); |
| 31 | + |
| 32 | + Assert.True(gate.IsClosed); |
| 33 | + Assert.False(gate.TryEnter()); |
| 34 | + } |
| 35 | + |
| 36 | + [Fact] |
| 37 | + public void CloseIsIdempotent() |
| 38 | + { |
| 39 | + var gate = new TsfnCallGate(); |
| 40 | + |
| 41 | + gate.Close(); |
| 42 | + gate.Close(); |
| 43 | + |
| 44 | + Assert.True(gate.IsClosed); |
| 45 | + Assert.False(gate.TryEnter()); |
| 46 | + } |
| 47 | + |
| 48 | + // Close must not return while a call is in flight: it has to wait for the corresponding Exit, |
| 49 | + // guaranteeing no native TSFN call is in progress once the TSFN is released. |
| 50 | + [Fact] |
| 51 | + public void CloseWaitsForInFlightCallToExit() |
| 52 | + { |
| 53 | + var gate = new TsfnCallGate(); |
| 54 | + |
| 55 | + // Simulate an in-flight native call that has entered but not yet exited. |
| 56 | + Assert.True(gate.TryEnter()); |
| 57 | + |
| 58 | + Task closeTask = Task.Run(() => gate.Close()); |
| 59 | + |
| 60 | + // Close cannot complete while the call is in flight. |
| 61 | + Assert.False(closeTask.Wait(TimeSpan.FromMilliseconds(200))); |
| 62 | + |
| 63 | + // Once the in-flight call exits, Close completes. |
| 64 | + gate.Exit(); |
| 65 | + Assert.True(closeTask.Wait(TimeSpan.FromSeconds(5))); |
| 66 | + |
| 67 | + // After closing, no further calls are admitted. |
| 68 | + Assert.False(gate.TryEnter()); |
| 69 | + } |
| 70 | + |
| 71 | + // Once Close has set the closed flag, an entry that races with it must be rejected, so the |
| 72 | + // in-flight count cannot rise again after Close begins draining. |
| 73 | + [Fact] |
| 74 | + public void EntryDoesNotSucceedAfterCloseFlagSet() |
| 75 | + { |
| 76 | + var gate = new TsfnCallGate(); |
| 77 | + |
| 78 | + gate.Close(); |
| 79 | + |
| 80 | + // A burst of concurrent entry attempts after Close must all fail. |
| 81 | + Parallel.For(0, 1000, _ => Assert.False(gate.TryEnter())); |
| 82 | + } |
| 83 | + |
| 84 | + // Stress the gate with concurrent enter/exit callers while another thread closes it, then |
| 85 | + // assert the invariant that after Close returns no caller is inside the gate and none can |
| 86 | + // enter. |
| 87 | + [Fact] |
| 88 | + public void ConcurrentCallersDrainBeforeCloseCompletes() |
| 89 | + { |
| 90 | + var gate = new TsfnCallGate(); |
| 91 | + using var start = new ManualResetEventSlim(false); |
| 92 | + |
| 93 | + Task[] callers = new Task[8]; |
| 94 | + for (int i = 0; i < callers.Length; i++) |
| 95 | + { |
| 96 | + callers[i] = Task.Run(() => |
| 97 | + { |
| 98 | + start.Wait(); |
| 99 | + for (int j = 0; j < 5000; j++) |
| 100 | + { |
| 101 | + if (gate.TryEnter()) |
| 102 | + { |
| 103 | + // Represents a brief native call. |
| 104 | + Thread.SpinWait(10); |
| 105 | + gate.Exit(); |
| 106 | + } |
| 107 | + } |
| 108 | + }); |
| 109 | + } |
| 110 | + |
| 111 | + start.Set(); |
| 112 | + |
| 113 | + // Close concurrently with the callers; it must wait for any in-flight call to exit. |
| 114 | + gate.Close(); |
| 115 | + |
| 116 | + // After Close returns, no caller may enter and the callers finish without error. |
| 117 | + Assert.False(gate.TryEnter()); |
| 118 | + Assert.True(Task.WaitAll(callers, TimeSpan.FromSeconds(30))); |
| 119 | + Assert.True(gate.IsClosed); |
| 120 | + } |
| 121 | +} |
0 commit comments