-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Resource watcher re-connects should reset currentVersion (#792)
- Loading branch information
1 parent
e8de144
commit 108240e
Showing
2 changed files
with
71 additions
and
67 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
116 changes: 58 additions & 58 deletions
116
test/KubeOps.Operator.Test/LeaderElector/LeaderElectionBackgroundService.Test.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 |
---|---|---|
@@ -1,58 +1,58 @@ | ||
using FluentAssertions; | ||
|
||
using k8s.LeaderElection; | ||
|
||
using KubeOps.Operator.LeaderElection; | ||
|
||
using Microsoft.Extensions.Logging; | ||
|
||
using Moq; | ||
|
||
namespace KubeOps.Operator.Test.LeaderElector; | ||
|
||
public sealed class LeaderElectionBackgroundServiceTest | ||
{ | ||
[Fact] | ||
public async Task Elector_Throws_Should_Retry() | ||
{ | ||
// Arrange. | ||
var logger = Mock.Of<ILogger<LeaderElectionBackgroundService>>(); | ||
|
||
var electionLock = Mock.Of<ILock>(); | ||
|
||
var electionLockSubsequentCallEvent = new AutoResetEvent(false); | ||
bool hasElectionLockThrown = false; | ||
Mock.Get(electionLock) | ||
.Setup(electionLock => electionLock.GetAsync(It.IsAny<CancellationToken>())) | ||
.Returns<CancellationToken>( | ||
async cancellationToken => | ||
{ | ||
if (hasElectionLockThrown) | ||
{ | ||
// Signal to the test that a subsequent call has been made. | ||
electionLockSubsequentCallEvent.Set(); | ||
|
||
// Delay returning for a long time, allowing the test to stop the background service, in turn cancelling the cancellation token. | ||
await Task.Delay(TimeSpan.FromSeconds(10), cancellationToken); | ||
throw new InvalidOperationException(); | ||
} | ||
|
||
hasElectionLockThrown = true; | ||
throw new Exception("Unit test exception"); | ||
}); | ||
|
||
var leaderElectionConfig = new LeaderElectionConfig(electionLock); | ||
var leaderElector = new k8s.LeaderElection.LeaderElector(leaderElectionConfig); | ||
|
||
var leaderElectionBackgroundService = new LeaderElectionBackgroundService(logger, leaderElector); | ||
|
||
// Act / Assert. | ||
await leaderElectionBackgroundService.StartAsync(CancellationToken.None); | ||
|
||
// Starting the background service should result in the lock attempt throwing, and then a subsequent attempt being made. | ||
// Wait for the subsequent event to be signalled, if we time out the test fails. The retry delay requires us to wait at least 3 seconds. | ||
electionLockSubsequentCallEvent.WaitOne(TimeSpan.FromMilliseconds(3100)).Should().BeTrue(); | ||
|
||
await leaderElectionBackgroundService.StopAsync(CancellationToken.None); | ||
} | ||
} | ||
using FluentAssertions; | ||
|
||
using k8s.LeaderElection; | ||
|
||
using KubeOps.Operator.LeaderElection; | ||
|
||
using Microsoft.Extensions.Logging; | ||
|
||
using Moq; | ||
|
||
namespace KubeOps.Operator.Test.LeaderElector; | ||
|
||
public sealed class LeaderElectionBackgroundServiceTest | ||
{ | ||
[Fact] | ||
public async Task Elector_Throws_Should_Retry() | ||
{ | ||
// Arrange. | ||
var logger = Mock.Of<ILogger<LeaderElectionBackgroundService>>(); | ||
|
||
var electionLock = Mock.Of<ILock>(); | ||
|
||
var electionLockSubsequentCallEvent = new AutoResetEvent(false); | ||
bool hasElectionLockThrown = false; | ||
Mock.Get(electionLock) | ||
.Setup(electionLock => electionLock.GetAsync(It.IsAny<CancellationToken>())) | ||
.Returns<CancellationToken>( | ||
async cancellationToken => | ||
{ | ||
if (hasElectionLockThrown) | ||
{ | ||
// Signal to the test that a subsequent call has been made. | ||
electionLockSubsequentCallEvent.Set(); | ||
|
||
// Delay returning for a long time, allowing the test to stop the background service, in turn cancelling the cancellation token. | ||
await Task.Delay(TimeSpan.FromSeconds(10), cancellationToken); | ||
throw new InvalidOperationException(); | ||
} | ||
|
||
hasElectionLockThrown = true; | ||
throw new Exception("Unit test exception"); | ||
}); | ||
|
||
var leaderElectionConfig = new LeaderElectionConfig(electionLock); | ||
var leaderElector = new k8s.LeaderElection.LeaderElector(leaderElectionConfig); | ||
|
||
var leaderElectionBackgroundService = new LeaderElectionBackgroundService(logger, leaderElector); | ||
|
||
// Act / Assert. | ||
await leaderElectionBackgroundService.StartAsync(CancellationToken.None); | ||
|
||
// Starting the background service should result in the lock attempt throwing, and then a subsequent attempt being made. | ||
// Wait for the subsequent event to be signalled, if we time out the test fails. The retry delay requires us to wait at least 3 seconds. | ||
electionLockSubsequentCallEvent.WaitOne(TimeSpan.FromMilliseconds(3100)).Should().BeTrue(); | ||
|
||
await leaderElectionBackgroundService.StopAsync(CancellationToken.None); | ||
} | ||
} |