Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -620,89 +620,6 @@ public async Task StopTestRecordingAsyncHandlesSaveFailuresGracefully()

#endregion

#region Delay Methods

[TestCase(RecordedTestMode.Live)]
[TestCase(RecordedTestMode.Record)]
public async Task DelayUsesFullDelayInLiveAndRecordModes(RecordedTestMode mode)
{
var testBase = new TestableRecordedTestBase(isAsync: true, mode);
var delayMs = 100;
var expectedMinDelay = TimeSpan.FromMilliseconds(delayMs * 0.9); // Allow 10% tolerance

var stopwatch = System.Diagnostics.Stopwatch.StartNew();
await testBase.Delay(delayMs);
stopwatch.Stop();

Assert.That(stopwatch.Elapsed, Is.GreaterThanOrEqualTo(expectedMinDelay),
$"Delay should take at least {delayMs}ms in {mode} mode, but took {stopwatch.ElapsedMilliseconds}ms");
}

[Test]
public async Task DelayUsesReducedDelayInPlaybackMode()
{
var testBase = new TestableRecordedTestBase(isAsync: true, RecordedTestMode.Playback);
var delayMs = 1000; // 1 second - should be reduced significantly in playback
var maxExpectedDelay = TimeSpan.FromMilliseconds(100); // Should be much faster in playback

var stopwatch = System.Diagnostics.Stopwatch.StartNew();
await testBase.Delay(delayMs);
stopwatch.Stop();

Assert.That(stopwatch.Elapsed, Is.LessThan(maxExpectedDelay),
$"Delay should be reduced in Playback mode, but took {stopwatch.ElapsedMilliseconds}ms (expected < {maxExpectedDelay.TotalMilliseconds}ms)");
}

[Test]
public async Task DelayWithCustomPlaybackDelayUsesSpecifiedValue()
{
var testBase = new TestableRecordedTestBase(isAsync: true, RecordedTestMode.Playback);
var originalDelayMs = 1000;
var customPlaybackDelayMs = 200;
var expectedMinDelay = TimeSpan.FromMilliseconds(customPlaybackDelayMs * 0.8); // Allow 20% tolerance
var expectedMaxDelay = TimeSpan.FromMilliseconds(customPlaybackDelayMs * 2); // Allow generous upper bound

var stopwatch = System.Diagnostics.Stopwatch.StartNew();
await testBase.Delay(originalDelayMs, customPlaybackDelayMs);
stopwatch.Stop();

Assert.That(stopwatch.Elapsed, Is.GreaterThanOrEqualTo(expectedMinDelay),
$"Custom playback delay should be at least {customPlaybackDelayMs}ms, but was {stopwatch.ElapsedMilliseconds}ms");
Assert.That(stopwatch.Elapsed, Is.LessThan(expectedMaxDelay),
$"Custom playback delay should be less than {expectedMaxDelay.TotalMilliseconds}ms, but was {stopwatch.ElapsedMilliseconds}ms");
}

[TestCase(RecordedTestMode.Live)]
[TestCase(RecordedTestMode.Record)]
[TestCase(RecordedTestMode.Playback)]
public async Task StaticDelayMethodWorksCorrectlyForEachMode(RecordedTestMode mode)
{
var delayMs = 50;

// Set the global test mode for the static method to use
using var testEnv = new TestEnvVar("CLIENTMODEL_TEST_MODE", mode.ToString());

var stopwatch = System.Diagnostics.Stopwatch.StartNew();
await RecordedTestBase.Delay(mode, delayMs);
stopwatch.Stop();

if (mode == RecordedTestMode.Playback)
{
// In playback mode, delay should be very short
Assert.That(stopwatch.Elapsed, Is.LessThan(TimeSpan.FromMilliseconds(30)),
$"Static Delay should be reduced in Playback mode, but took {stopwatch.ElapsedMilliseconds}ms");
}
else
{
// In Live/Record modes, delay should be close to requested
var expectedMinDelay = TimeSpan.FromMilliseconds(delayMs * 0.8); // Allow 20% tolerance
Assert.That(stopwatch.Elapsed, Is.GreaterThanOrEqualTo(expectedMinDelay),
$"Static Delay should take at least {delayMs}ms in {mode} mode, but took {stopwatch.ElapsedMilliseconds}ms");
}
}

#endregion

#region Sanitizer Configuration

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,51 +122,6 @@ public async Task CompleteRequestResponseCycleWithAllMockComponentsWorksEndToEnd
}
}

[Test]
public async Task MockTransportWithAsyncProcessingHandlesAsyncOperationsCorrectly()
{
var processedItems = new List<string>();
var transport = new MockPipelineTransport(msg =>
{
processedItems.Add($"Processed: {msg.Request.Method} {msg.Request.Uri}");
return new MockPipelineResponse(200, "OK").WithContent("async response");
});
transport.ExpectSyncPipeline = false; // Enable async processing

var messages = new[]
{
CreateMessageWithUrl(transport, "GET", "https://api.example.com/item/1"),
CreateMessageWithUrl(transport, "GET", "https://api.example.com/item/2"),
CreateMessageWithUrl(transport, "GET", "https://api.example.com/item/3")
};

var tasks = messages.Select(async msg =>
{
await Task.Delay(10); // Simulate some async work
await transport.ProcessAsync(msg);
return msg;
});

var results = await Task.WhenAll(tasks);

using (Assert.EnterMultipleScope())
{
Assert.That(results.Length, Is.EqualTo(3));
Assert.That(processedItems.Count, Is.EqualTo(3));
Assert.That(transport.Requests.Count, Is.EqualTo(3));
}

foreach (var result in results)
{
Assert.That(result.Response, Is.Not.Null);
using (Assert.EnterMultipleScope())
{
Assert.That(result.Response.Status, Is.EqualTo(200));
Assert.That(result.Response.Content.ToString(), Is.EqualTo("async response"));
}
}
}

[Test]
public void MockTransportWithSyncAsyncMismatchThrowsAppropriateExceptions()
{
Expand Down