forked from Azure/azure-sdk-for-net
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ServiceBus] ServicebusRetryPolicy remove locking (Azure#31206)
* Remove the lock from the retry policy * Method visibility * Switch to simpler implementation * Formatting * Allow controlling the server busy base sleep time * A few tests to get started * Rename field * Greater than zero
- Loading branch information
1 parent
3968018
commit f5b32f9
Showing
2 changed files
with
126 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
sdk/servicebus/Azure.Messaging.ServiceBus/tests/Core/ServiceBusRetryPolicyTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using NUnit.Framework; | ||
|
||
namespace Azure.Messaging.ServiceBus.Tests | ||
{ | ||
/// <summary> | ||
/// The suite of tests for the <see cref="ServiceBusRetryPolicy" /> | ||
/// class. | ||
/// </summary> | ||
/// | ||
[TestFixture] | ||
public class ServiceBusRetryPolicyTests | ||
{ | ||
[Test] | ||
public void HasDefaultValues() | ||
{ | ||
var policy = new MockServiceBusRetryPolicy(); | ||
|
||
Assert.That(policy.IsServerBusy, Is.False); | ||
Assert.That(policy.ServerBusyBaseSleepTime, Is.GreaterThan(TimeSpan.FromSeconds(0))); | ||
Assert.That(policy.ServerBusyExceptionMessage, Is.Null); | ||
} | ||
|
||
[Test] | ||
public async Task ExecutesRunOperationWithState() | ||
{ | ||
var policy = new MockServiceBusRetryPolicy(); | ||
|
||
var operationResult = await policy.RunOperation((state, timeout, token) => new ValueTask<bool>(!state), false, null, | ||
CancellationToken.None); | ||
|
||
Assert.That(operationResult, Is.True); | ||
} | ||
|
||
[Test] | ||
public void SetsServerBusy() | ||
{ | ||
var policy = new MockServiceBusRetryPolicy(); | ||
|
||
Assert.ThrowsAsync<ServiceBusException>(async () => | ||
{ | ||
await policy.RunOperation((state, timeout, token) => | ||
{ | ||
throw new ServiceBusException("Busy", ServiceBusFailureReason.ServiceBusy); | ||
}, false, null, | ||
CancellationToken.None); | ||
}); | ||
Assert.That(policy.IsServerBusy, Is.True); | ||
} | ||
|
||
[Test] | ||
public void ResetsServerBusyAfterBaseSleepTime() | ||
{ | ||
var policy = new MockServiceBusRetryPolicy | ||
{ | ||
ServerBusyBaseSleepTime = TimeSpan.FromMilliseconds(10) | ||
}; | ||
|
||
Assert.ThrowsAsync<ServiceBusException>(async () => | ||
{ | ||
await policy.RunOperation((state, timeout, token) => | ||
{ | ||
throw new ServiceBusException("Busy", ServiceBusFailureReason.ServiceBusy); | ||
}, false, null, | ||
CancellationToken.None); | ||
}); | ||
|
||
var serverNoLongerBusy = SpinWait.SpinUntil(() => policy.IsServerBusy == false, TimeSpan.FromSeconds(1)); | ||
Assert.That(serverNoLongerBusy, Is.True); | ||
} | ||
|
||
private class MockServiceBusRetryPolicy : ServiceBusRetryPolicy | ||
{ | ||
public override TimeSpan CalculateTryTimeout(int attemptCount) | ||
{ | ||
return TimeSpan.Zero; | ||
} | ||
|
||
public override TimeSpan? CalculateRetryDelay(Exception lastException, int attemptCount) | ||
{ | ||
return null; | ||
} | ||
} | ||
} | ||
} |