aboutsummaryrefslogtreecommitdiff
path: root/tests/Jellyfin.Server.Tests/ServerSetupApp/StartupLoggerTests.cs
blob: c2894e96477ec20aea7f8a7cf9560f4928f15754 (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
using Jellyfin.Server.ServerSetupApp;
using Microsoft.Extensions.Logging.Abstractions;
using Xunit;

namespace Jellyfin.Server.Tests.ServerSetupApp;

public class StartupLoggerTests
{
    [Fact]
    public void BeginAmbientTopic_AttachesNewLoggersToTheTopic()
    {
        var migration = new StartupLogger(NullLogger.Instance).BeginGroup($"Migration");

        using (StartupLogger.BeginAmbientTopic(migration.Topic))
        {
            Assert.Same(migration.Topic, new StartupLogger(NullLogger.Instance).Topic);
        }
    }

    [Fact]
    public void BeginAmbientTopic_RestoresThePreviousTopic()
    {
        var root = new StartupLogger(NullLogger.Instance);
        var outer = root.BeginGroup($"Outer");
        var inner = outer.BeginGroup($"Inner");

        Assert.Null(new StartupLogger(NullLogger.Instance).Topic);

        using (StartupLogger.BeginAmbientTopic(outer.Topic))
        {
            using (StartupLogger.BeginAmbientTopic(inner.Topic))
            {
                Assert.Same(inner.Topic, new StartupLogger(NullLogger.Instance).Topic);
            }

            // Leaving a nested topic has to fall back to the enclosing one, not to the setup UI root.
            Assert.Same(outer.Topic, new StartupLogger(NullLogger.Instance).Topic);
        }

        Assert.Null(new StartupLogger(NullLogger.Instance).Topic);
    }

    [Fact]
    public void BeginGroup_KeepsAnExplicitTopicOverTheAmbientOne()
    {
        var migration = new StartupLogger(NullLogger.Instance).BeginGroup($"Migration");
        var unrelated = new StartupLogger(NullLogger.Instance).BeginGroup($"Unrelated");

        using (StartupLogger.BeginAmbientTopic(migration.Topic))
        {
            Assert.Same(unrelated.Topic, unrelated.With(NullLogger.Instance).Topic);
        }
    }
}