Skip to content

Commit

Permalink
Improve connection cancellation token tests
Browse files Browse the repository at this point in the history
  • Loading branch information
xinchen10 committed Jan 19, 2024
1 parent 8d21715 commit 9667771
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
12 changes: 9 additions & 3 deletions test/Common/TestRuntimeProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,21 @@ public void EndOpenLink(IAsyncResult result)

class TestSaslPlainAuthenticator : ISaslPlainAuthenticator
{
public Task<IPrincipal> AuthenticateAsync(string identity, string password)
public int DelayInMilliseconds { get; set; }

public async Task<IPrincipal> AuthenticateAsync(string identity, string password)
{
if (this.DelayInMilliseconds > 0)
{
await Task.Delay(this.DelayInMilliseconds);
}

if (identity != password)
{
throw new UnauthorizedAccessException();
}

IPrincipal principal = new GenericPrincipal(new GenericIdentity(identity), new string[] { "SEND", "RECV" });
return Task.FromResult(principal);
return new GenericPrincipal(new GenericIdentity(identity), new string[] { "SEND", "RECV" });
}
}

Expand Down
8 changes: 4 additions & 4 deletions test/TestCases/CancellationTokenTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public Task ConnectionFactoryCanceledTest()

async Task RunConnectionFactoryTest(bool cancelBefore)
{
AmqpConnectionListener listener = OpenListener();
AmqpConnectionListener listener = OpenListener(saslDelayMs: 200);

try
{
Expand Down Expand Up @@ -130,7 +130,7 @@ public Task ConnectionOpenCanceledTest()

async Task RunConnectionOpenTest(bool cancelBefore)
{
AmqpConnectionListener listener = OpenListener();
AmqpConnectionListener listener = OpenListener(saslDelayMs: 200);

try
{
Expand Down Expand Up @@ -724,11 +724,11 @@ await Assert.ThrowsAnyAsync<TaskCanceledException>(async () =>
}
}

AmqpConnectionListener OpenListener(TestRuntimeProvider runtimeProvider = null)
AmqpConnectionListener OpenListener(TestRuntimeProvider runtimeProvider = null, int saslDelayMs = 0)
{
AmqpSettings settings = new AmqpSettings { RuntimeProvider = runtimeProvider ?? new TestRuntimeProvider() };
var saslProvider = new SaslTransportProvider(AmqpVersion.V100);
saslProvider.AddHandler(new SaslPlainHandler(new TestSaslPlainAuthenticator()));
saslProvider.AddHandler(new SaslPlainHandler(new TestSaslPlainAuthenticator() { DelayInMilliseconds = saslDelayMs }));
settings.TransportProviders.Add(saslProvider);
var listener = new AmqpConnectionListener(new[] { addressUri.AbsoluteUri }, settings, new AmqpConnectionSettings());
listener.Open();
Expand Down

0 comments on commit 9667771

Please sign in to comment.