aboutsummaryrefslogtreecommitdiff
path: root/tests/Jellyfin.Server.Implementations.Tests/Locking/OptimisticLockBehaviorTests.cs
blob: 1a91923f8d80f8396795801406b3a99efd943fa3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Jellyfin.Database.Implementations.Locking;
using Microsoft.Data.Sqlite;
using Microsoft.Extensions.Logging.Abstractions;
using Polly.Utilities;
using Xunit;

namespace Jellyfin.Server.Implementations.Tests.Locking;

/// <summary>
/// The retries of <see cref="OptimisticLockBehavior"/> when the database stays locked. Polly's clock records the
/// waits instead of sleeping through them, and the save hooks never use the context, so none is created.
/// </summary>
public sealed class OptimisticLockBehaviorTests : IDisposable
{
    private static readonly TimeSpan[] _sleepDurations =
    [
        TimeSpan.FromMilliseconds(50),
        TimeSpan.FromMilliseconds(50),
        TimeSpan.FromMilliseconds(50),
        TimeSpan.FromMilliseconds(50),
        TimeSpan.FromMilliseconds(250),
        TimeSpan.FromMilliseconds(250),
        TimeSpan.FromMilliseconds(250),
        TimeSpan.FromMilliseconds(150),
        TimeSpan.FromMilliseconds(150),
        TimeSpan.FromMilliseconds(150),
        TimeSpan.FromMilliseconds(500),
        TimeSpan.FromMilliseconds(150),
        TimeSpan.FromMilliseconds(500),
        TimeSpan.FromMilliseconds(150),
        TimeSpan.FromSeconds(3)
    ];

    private readonly List<TimeSpan> _waits = [];
    private readonly OptimisticLockBehavior _behavior = new(NullLogger<OptimisticLockBehavior>.Instance);

    public OptimisticLockBehaviorTests()
    {
        SystemClock.Sleep = (wait, _) => _waits.Add(wait);
        SystemClock.SleepAsync = (wait, _) =>
        {
            _waits.Add(wait);
            return Task.CompletedTask;
        };
    }

    public void Dispose()
    {
        SystemClock.Reset();
    }

    [Fact]
    public void OnSaveChanges_DatabaseLockedBriefly_WaitsTheConfiguredDurationsInOrder()
    {
        var attempts = 0;

        _behavior.OnSaveChanges(null!, () =>
        {
            if (++attempts <= 5)
            {
                throw DatabaseLocked();
            }
        });

        Assert.Equal(6, attempts);
        AssertWaits(5);
    }

    [Fact]
    public void OnSaveChanges_DatabaseStaysLocked_ThrowsTheDatabaseErrorAfterTheLastRetry()
    {
        var attempts = 0;

        Assert.Throws<SqliteException>(() => _behavior.OnSaveChanges(null!, () =>
        {
            attempts++;
            throw DatabaseLocked();
        }));

        Assert.Equal(_sleepDurations.Length + 1, attempts);
        AssertWaits(_sleepDurations.Length);
    }

    [Fact]
    public async Task OnSaveChangesAsync_DatabaseStaysLocked_ThrowsTheDatabaseErrorAfterTheLastRetry()
    {
        var attempts = 0;

        await Assert.ThrowsAsync<SqliteException>(() => _behavior.OnSaveChangesAsync(null!, () =>
        {
            attempts++;
            throw DatabaseLocked();
        }));

        Assert.Equal(_sleepDurations.Length + 1, attempts);
        AssertWaits(_sleepDurations.Length);
    }

    private static SqliteException DatabaseLocked() => new("SQLite Error 5: 'database is locked'.", 5);

    private void AssertWaits(int retries)
    {
        Assert.Equal(retries, _waits.Count);
        for (var i = 0; i < retries; i++)
        {
            // The configured duration plus a jitter of less than half of it.
            Assert.InRange(_waits[i], _sleepDurations[i], _sleepDurations[i] * 1.5);
        }
    }
}