• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

me-viper / OpaDotNet.Extensions / 6945658615

21 Nov 2023 03:01PM UTC coverage: 86.565% (+0.1%) from 86.431%
6945658615

push

github

me-viper
feat: Close #4. Support precompiled bundles

92 of 120 branches covered (0.0%)

Branch coverage included in aggregate %.

78 of 92 new or added lines in 3 files covered. (84.78%)

533 of 602 relevant lines covered (88.54%)

4489.43 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

79.07
/src/OpaDotNet.Extensions.AspNetCore/PathPolicySource.cs
1
using Microsoft.Extensions.Options;
2

3
using OpaDotNet.Compilation.Abstractions;
4

5
namespace OpaDotNet.Extensions.AspNetCore;
6

7
public abstract class PathPolicySource : OpaPolicySource
8
{
9
    protected IDisposable? PolicyWatcher { get; init; }
24✔
10

11
    private readonly PeriodicTimer? _changesMonitor;
12

13
    private readonly CancellationTokenSource _cancellationTokenSource = new();
44✔
14

15
    protected bool NeedsRecompilation { get; set; }
96✔
16

17
    protected bool MonitoringEnabled => Options.Value.MonitoringInterval > TimeSpan.Zero;
140✔
18

19
    protected PathPolicySource(
20
        IRegoCompiler compiler,
21
        IOptions<OpaAuthorizationOptions> options,
22
        IOpaImportsAbiFactory importsAbiFactory,
23
        ILoggerFactory loggerFactory) : base(compiler, options, importsAbiFactory, loggerFactory)
44✔
24
    {
44✔
25
        if (string.IsNullOrWhiteSpace(options.Value.PolicyBundlePath))
44!
NEW
26
        {
×
NEW
27
            throw new InvalidOperationException(
×
NEW
28
                $"{GetType()} requires OpaAuthorizationOptions.PolicyBundlePath specified"
×
NEW
29
                );
×
30
        }
31

32
        if (MonitoringEnabled)
44✔
33
            _changesMonitor = new(Options.Value.MonitoringInterval);
8✔
34
    }
44✔
35

36
    private async Task TrackPolicyChanged(CancellationToken cancellationToken)
37
    {
8✔
38
        if (!MonitoringEnabled || _changesMonitor == null)
8!
NEW
39
            return;
×
40

41
        Logger.LogDebug("Watching for policy changes in {Path}", Options.Value.PolicyBundlePath);
8✔
42

43
        while (await _changesMonitor.WaitForNextTickAsync(cancellationToken).ConfigureAwait(false))
47!
44
        {
39✔
45
            try
46
            {
39✔
47
                if (!NeedsRecompilation)
39✔
48
                    continue;
15✔
49

50
                if (cancellationToken.IsCancellationRequested)
24!
NEW
51
                    break;
×
52

53
                NeedsRecompilation = false;
24✔
54

55
                Logger.LogDebug("Detected changes. Recompiling");
24✔
56
                await CompileBundle(true, cancellationToken).ConfigureAwait(false);
24✔
57
                Logger.LogDebug("Recompilation succeeded");
24✔
58
            }
24✔
NEW
59
            catch (Exception ex)
×
NEW
60
            {
×
NEW
61
                Logger.LogError(ex, "Failed to process policy changes");
×
NEW
62
                NeedsRecompilation = true;
×
NEW
63
            }
×
64
        }
24✔
65

NEW
66
        Logger.LogDebug("Stopped watching for policy changes");
×
NEW
67
    }
×
68

69
    protected override void Dispose(bool disposing)
70
    {
16✔
71
        try
72
        {
16✔
73
            if (disposing)
16✔
74
            {
16✔
75
                _changesMonitor?.Dispose();
16✔
76
                PolicyWatcher?.Dispose();
16✔
77
                _cancellationTokenSource.Dispose();
16✔
78
            }
16✔
79
        }
16✔
80
        finally
81
        {
16✔
82
            base.Dispose(disposing);
16✔
83
        }
16✔
84
    }
16✔
85

86
    /// <inheritdoc/>
87
    public override async Task StartAsync(CancellationToken cancellationToken)
88
    {
44✔
89
        await base.StartAsync(cancellationToken).ConfigureAwait(false);
44✔
90

91
        if (MonitoringEnabled)
44✔
92
        {
8✔
93
            _ = Task.Run(() => TrackPolicyChanged(_cancellationTokenSource.Token), cancellationToken);
16✔
94
        }
8✔
95
    }
44✔
96

97
    /// <inheritdoc/>
98
    public override async Task StopAsync(CancellationToken cancellationToken)
99
    {
16✔
100
        await base.StopAsync(cancellationToken).ConfigureAwait(false);
16✔
101

102
#if NET8_0_OR_GREATER
103
        await _cancellationTokenSource.CancelAsync().ConfigureAwait(false);
8✔
104
#else
105
        _cancellationTokenSource.Cancel();
8✔
106
#endif
107

108
        Logger.LogDebug("Stopped");
16✔
109
    }
16✔
110
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc