using XFEExtension.NetCore.XUnit.Assertions; using XFEExtension.NetCore.XUnit.Attributes; namespace ChaoXingAutoClass.Test; [TestFixture] public sealed class CourseSchedulingTests { [Test] public async Task MultipleCoursesRunUpToConfiguredLimitAndRemainingCoursesQueue() { using var gate = new AdaptiveCourseGate(2); using var first = await gate.EnterAsync("one", () => { }, CancellationToken.None); using var second = await gate.EnterAsync("two", () => { }, CancellationToken.None); var third = gate.EnterAsync("three", () => { }, CancellationToken.None); Assert.False(third.IsCompleted); second.Dispose(); using var admitted = await third.WaitAsync(TimeSpan.FromSeconds(3)); Assert.Equal(2, gate.EffectiveConcurrency); } [Test] public async Task SiteRestrictionDrainsAllActiveCoursesBeforeSequentialRetries() { using var gate = new AdaptiveCourseGate(3); var restarts = 0; using var first = await gate.EnterAsync("one", () => restarts++, CancellationToken.None); using var second = await gate.EnterAsync("two", () => restarts++, CancellationToken.None); Assert.True(gate.SwitchToSequential()); Assert.False(gate.SwitchToSequential()); Assert.Equal(2, restarts); var retry = gate.EnterAsync("retry", () => { }, CancellationToken.None); Assert.False(retry.IsCompleted); first.Dispose(); Assert.False(retry.IsCompleted); second.Dispose(); using var admitted = await retry.WaitAsync(TimeSpan.FromSeconds(3)); var next = gate.EnterAsync("next", () => { }, CancellationToken.None); Assert.False(next.IsCompleted); admitted.Dispose(); using var last = await next.WaitAsync(TimeSpan.FromSeconds(3)); Assert.Equal(1, gate.EffectiveConcurrency); } [Test] public async Task CancelledQueuedCourseCannotConsumeAConcurrencySlot() { using var gate = new AdaptiveCourseGate(1); using var first = await gate.EnterAsync("one", () => { }, CancellationToken.None); using var cancelled = new CancellationTokenSource(); var queued = gate.EnterAsync("cancel", () => { }, cancelled.Token); cancelled.Cancel(); try { await queued; throw new Exception("Expected cancellation"); } catch (OperationCanceledException) { } first.Dispose(); using var next = await gate.EnterAsync("next", () => { }, CancellationToken.None).WaitAsync(TimeSpan.FromSeconds(3)); } }