|
1 | | -using FluentAssertions; |
2 | | - |
3 | | -using k8s.LeaderElection; |
4 | | - |
5 | | -using KubeOps.Operator.LeaderElection; |
6 | | - |
7 | | -using Microsoft.Extensions.Logging; |
8 | | - |
9 | | -using Moq; |
10 | | - |
11 | | -namespace KubeOps.Operator.Test.LeaderElector; |
12 | | - |
13 | | -public sealed class LeaderElectionBackgroundServiceTest |
14 | | -{ |
15 | | - [Fact] |
16 | | - public async Task Elector_Throws_Should_Retry() |
17 | | - { |
18 | | - // Arrange. |
19 | | - var logger = Mock.Of<ILogger<LeaderElectionBackgroundService>>(); |
20 | | - |
21 | | - var electionLock = Mock.Of<ILock>(); |
22 | | - |
23 | | - var electionLockSubsequentCallEvent = new AutoResetEvent(false); |
24 | | - bool hasElectionLockThrown = false; |
25 | | - Mock.Get(electionLock) |
26 | | - .Setup(electionLock => electionLock.GetAsync(It.IsAny<CancellationToken>())) |
27 | | - .Returns<CancellationToken>( |
28 | | - async cancellationToken => |
29 | | - { |
30 | | - if (hasElectionLockThrown) |
31 | | - { |
32 | | - // Signal to the test that a subsequent call has been made. |
33 | | - electionLockSubsequentCallEvent.Set(); |
34 | | - |
35 | | - // Delay returning for a long time, allowing the test to stop the background service, in turn cancelling the cancellation token. |
36 | | - await Task.Delay(TimeSpan.FromSeconds(10), cancellationToken); |
37 | | - throw new InvalidOperationException(); |
38 | | - } |
39 | | - |
40 | | - hasElectionLockThrown = true; |
41 | | - throw new Exception("Unit test exception"); |
42 | | - }); |
43 | | - |
44 | | - var leaderElectionConfig = new LeaderElectionConfig(electionLock); |
45 | | - var leaderElector = new k8s.LeaderElection.LeaderElector(leaderElectionConfig); |
46 | | - |
47 | | - var leaderElectionBackgroundService = new LeaderElectionBackgroundService(logger, leaderElector); |
48 | | - |
49 | | - // Act / Assert. |
50 | | - await leaderElectionBackgroundService.StartAsync(CancellationToken.None); |
51 | | - |
52 | | - // Starting the background service should result in the lock attempt throwing, and then a subsequent attempt being made. |
53 | | - // 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. |
54 | | - electionLockSubsequentCallEvent.WaitOne(TimeSpan.FromMilliseconds(3100)).Should().BeTrue(); |
55 | | - |
56 | | - await leaderElectionBackgroundService.StopAsync(CancellationToken.None); |
57 | | - } |
58 | | -} |
| 1 | +using FluentAssertions; |
| 2 | + |
| 3 | +using k8s.LeaderElection; |
| 4 | + |
| 5 | +using KubeOps.Operator.LeaderElection; |
| 6 | + |
| 7 | +using Microsoft.Extensions.Logging; |
| 8 | + |
| 9 | +using Moq; |
| 10 | + |
| 11 | +namespace KubeOps.Operator.Test.LeaderElector; |
| 12 | + |
| 13 | +public sealed class LeaderElectionBackgroundServiceTest |
| 14 | +{ |
| 15 | + [Fact] |
| 16 | + public async Task Elector_Throws_Should_Retry() |
| 17 | + { |
| 18 | + // Arrange. |
| 19 | + var logger = Mock.Of<ILogger<LeaderElectionBackgroundService>>(); |
| 20 | + |
| 21 | + var electionLock = Mock.Of<ILock>(); |
| 22 | + |
| 23 | + var electionLockSubsequentCallEvent = new AutoResetEvent(false); |
| 24 | + bool hasElectionLockThrown = false; |
| 25 | + Mock.Get(electionLock) |
| 26 | + .Setup(electionLock => electionLock.GetAsync(It.IsAny<CancellationToken>())) |
| 27 | + .Returns<CancellationToken>( |
| 28 | + async cancellationToken => |
| 29 | + { |
| 30 | + if (hasElectionLockThrown) |
| 31 | + { |
| 32 | + // Signal to the test that a subsequent call has been made. |
| 33 | + electionLockSubsequentCallEvent.Set(); |
| 34 | + |
| 35 | + // Delay returning for a long time, allowing the test to stop the background service, in turn cancelling the cancellation token. |
| 36 | + await Task.Delay(TimeSpan.FromSeconds(10), cancellationToken); |
| 37 | + throw new InvalidOperationException(); |
| 38 | + } |
| 39 | + |
| 40 | + hasElectionLockThrown = true; |
| 41 | + throw new Exception("Unit test exception"); |
| 42 | + }); |
| 43 | + |
| 44 | + var leaderElectionConfig = new LeaderElectionConfig(electionLock); |
| 45 | + var leaderElector = new k8s.LeaderElection.LeaderElector(leaderElectionConfig); |
| 46 | + |
| 47 | + var leaderElectionBackgroundService = new LeaderElectionBackgroundService(logger, leaderElector); |
| 48 | + |
| 49 | + // Act / Assert. |
| 50 | + await leaderElectionBackgroundService.StartAsync(CancellationToken.None); |
| 51 | + |
| 52 | + // Starting the background service should result in the lock attempt throwing, and then a subsequent attempt being made. |
| 53 | + // 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. |
| 54 | + electionLockSubsequentCallEvent.WaitOne(TimeSpan.FromMilliseconds(3100)).Should().BeTrue(); |
| 55 | + |
| 56 | + await leaderElectionBackgroundService.StopAsync(CancellationToken.None); |
| 57 | + } |
| 58 | +} |
0 commit comments