Skip to content

Commit

Permalink
Fix some test issues
Browse files Browse the repository at this point in the history
  • Loading branch information
xinchen10 committed Feb 21, 2024
1 parent 354669c commit 6795f1f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 31 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
5 changes: 3 additions & 2 deletions test/TestCases/AmqpTransportTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public void TlsProtocolsTest()
#if NET471_OR_GREATER || NETCOREAPP
protocols.Add(SslProtocols.None);
#endif
#if NET48_OR_GREATER || NET5_0_OR_GREATER || NETCOREAPP3_0_OR_GREATER
#if NET48_OR_GREATER || NET5_0_OR_GREATER
protocols.Add(SslProtocols.Tls13);
#endif

Expand Down Expand Up @@ -152,7 +152,8 @@ public void TlsProtocolsNoFallbackForUserProtocolListenerTest()
}
catch (SocketException se)
{
Assert.Equal(SocketError.ConnectionReset, se.SocketErrorCode);
Assert.True(SocketError.ConnectionReset == se.SocketErrorCode ||
SocketError.ConnectionAborted == se.SocketErrorCode);
}
}
#endif
Expand Down
61 changes: 35 additions & 26 deletions test/TestCases/CancellationTokenTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ namespace Test.Microsoft.Azure.Amqp
using System.Threading.Tasks;
using global::Microsoft.Azure.Amqp;
using global::Microsoft.Azure.Amqp.Framing;
using global::Microsoft.Azure.Amqp.Sasl;
using global::Microsoft.Azure.Amqp.Transport;
using Xunit;
using TestAmqpBroker;
using Xunit;

[Trait("Category", TestCategory.Current)]
public class CancellationTokenTests
{
Uri addressUri = new Uri("amqp://localhost:5678");
Uri addressUri = new Uri("amqp://guest:guest@localhost:5678");

[Fact]
public Task TransportTest()
Expand All @@ -41,6 +42,9 @@ async Task RunTransportTest(bool cancelBefore)
await Assert.ThrowsAsync<TaskCanceledException>(async () =>
{
AmqpSettings settings = new AmqpSettings();
var sasl = new SaslTransportProvider();
sasl.Versions.Add(new AmqpVersion(1, 0, 0));
settings.TransportProviders.Add(sasl);
var provider = new AmqpTransportProvider();
provider.Versions.Add(new AmqpVersion(1, 0, 0));
settings.TransportProviders.Add(provider);
Expand Down Expand Up @@ -82,8 +86,7 @@ public Task ConnectionFactoryCanceledTest()

async Task RunConnectionFactoryTest(bool cancelBefore)
{
AmqpConnectionListener listener = new AmqpConnectionListener(addressUri.AbsoluteUri, new TestRuntimeProvider());
listener.Open();
AmqpConnectionListener listener = OpenListener(saslDelayMs: 200);

try
{
Expand Down Expand Up @@ -126,8 +129,7 @@ public Task ConnectionOpenCanceledTest()

async Task RunConnectionOpenTest(bool cancelBefore)
{
AmqpConnectionListener listener = new AmqpConnectionListener(addressUri.AbsoluteUri, new TestRuntimeProvider());
listener.Open();
AmqpConnectionListener listener = OpenListener(saslDelayMs: 200);

try
{
Expand Down Expand Up @@ -179,8 +181,7 @@ public Task ConnectionCloseCanceledTest()

async Task RunConnectionCloseTest(bool cancelBefore)
{
AmqpConnectionListener listener = new AmqpConnectionListener(addressUri.AbsoluteUri, new TestRuntimeProvider());
listener.Open();
AmqpConnectionListener listener = OpenListener();

try
{
Expand Down Expand Up @@ -233,8 +234,7 @@ public Task SessionOpenCanceledTest()

async Task RunSessionOpenTest(bool cancelBefore)
{
AmqpConnectionListener listener = new AmqpConnectionListener(addressUri.AbsoluteUri, new TestRuntimeProvider());
listener.Open();
AmqpConnectionListener listener = OpenListener();

try
{
Expand Down Expand Up @@ -280,8 +280,7 @@ public Task SessionCloseCanceledTest()

async Task RunSessionCloseTest(bool cancelBefore)
{
AmqpConnectionListener listener = new AmqpConnectionListener(addressUri.AbsoluteUri, new TestRuntimeProvider());
listener.Open();
AmqpConnectionListener listener = OpenListener();

try
{
Expand Down Expand Up @@ -328,8 +327,7 @@ public Task LinkOpenCanceledTest()

async Task RunLinkOpenTest(bool cancelBefore)
{
AmqpConnectionListener listener = new AmqpConnectionListener(addressUri.AbsoluteUri, new TestRuntimeProvider());
listener.Open();
AmqpConnectionListener listener = OpenListener();

try
{
Expand Down Expand Up @@ -377,8 +375,7 @@ public Task LinkCloseCanceledTest()

async Task RunLinkCloseTest(bool cancelBefore)
{
AmqpConnectionListener listener = new AmqpConnectionListener(addressUri.AbsoluteUri, new TestRuntimeProvider());
listener.Open();
AmqpConnectionListener listener = OpenListener();

try
{
Expand Down Expand Up @@ -432,8 +429,8 @@ async Task RunLinkSendTest(bool cancelBefore)
{
LinkFactory = (s, t) => { t.TotalLinkCredit = 10; return new TestLink(s, t, sendHang: true); }
};
AmqpConnectionListener listener = new AmqpConnectionListener(addressUri.AbsoluteUri, provider);
listener.Open();

AmqpConnectionListener listener = OpenListener(provider);

try
{
Expand Down Expand Up @@ -487,8 +484,8 @@ async Task RunLinkReceiveTest(bool cancelBefore)
{
LinkFactory = (s, t) => new TestLink(s, t, receiveHang: true)
};
AmqpConnectionListener listener = new AmqpConnectionListener(addressUri.AbsoluteUri, provider);
listener.Open();

AmqpConnectionListener listener = OpenListener(provider);

try
{
Expand Down Expand Up @@ -539,8 +536,8 @@ async Task RunLinkDispositionTest(bool cancelBefore)
{
LinkFactory = (s, t) => { t.SettleType = SettleMode.SettleOnDispose; return new TestLink(s, t, disposeHang: true); }
};
AmqpConnectionListener listener = new AmqpConnectionListener(addressUri.AbsoluteUri, provider);
listener.Open();

AmqpConnectionListener listener = OpenListener(provider);

try
{
Expand Down Expand Up @@ -603,8 +600,8 @@ async Task RunLinkDrainTest(bool cancelBefore, int timeoutMilliseconds = 0)
{
LinkFactory = (s, t) => { t.SettleType = SettleMode.SettleOnDispose; return new TestLink(s, t, flowHang: true); }
};
AmqpConnectionListener listener = new AmqpConnectionListener(addressUri.AbsoluteUri, provider);
listener.Open();

AmqpConnectionListener listener = OpenListener(provider);

try
{
Expand Down Expand Up @@ -661,7 +658,7 @@ async Task RunLinkDrainTest(bool cancelBefore, int timeoutMilliseconds = 0)
[Fact]
public async Task CbsSendTokenNoCancelTest()
{
var broker = new TestAmqpBroker(new[] { addressUri.AbsoluteUri }, null, null, null);
var broker = new TestAmqpBroker(new[] { addressUri.AbsoluteUri }, addressUri.UserInfo, null, null);
broker.AddNode(new CbsNode());
broker.Start();

Expand Down Expand Up @@ -694,7 +691,7 @@ public Task CbsSendTokenCancelledTest()

async Task RunCbsSendTokenTest(bool cancelBefore)
{
var broker = new TestAmqpBroker(new[] { addressUri.AbsoluteUri }, null, null, null);
var broker = new TestAmqpBroker(new[] { addressUri.AbsoluteUri }, addressUri.UserInfo, null, null);
broker.AddNode(new CbsNode() { ProcessingTime = TimeSpan.FromSeconds(10) });
broker.Start();

Expand Down Expand Up @@ -730,6 +727,18 @@ await Assert.ThrowsAnyAsync<TaskCanceledException>(async () =>
}
}

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

class TestTokenProvider : ICbsTokenProvider
{
public Task<CbsToken> GetTokenAsync(Uri namespaceAddress, string appliesTo, string[] requiredClaims)
Expand Down

0 comments on commit 6795f1f

Please sign in to comment.