Skip to content

Commit

Permalink
Added cache flag for configuration validity. (#97)
Browse files Browse the repository at this point in the history
* Added ConfigurableVaultApplicationBase.IsCurrentConfigurationValid method that will cache the configuration validity.
* Added unit tests for various scenarios.
  • Loading branch information
CraigHawker committed Mar 20, 2023
1 parent afa382b commit 255ebf5
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using MFilesAPI;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MFiles.VAF.Extensions.Tests
{
[TestClass]
public class ConfigurableVaultApplicationBase
{
[TestMethod]
[DataRow(true, false, false, true)] // Valid, no cache.
[DataRow(false, false, false, true)] // Invalid, no cache.
[DataRow(true, true, false, false)] // Valid, with cache.
[DataRow(false, true, false, false)] // Invalid, with cache.
[DataRow(true, true, true, true)] // Valid, with cache, force refresh.
[DataRow(false, true, true, true)] // Invalid, with cache, force refresh.
public void IsCurrentConfigurationValid
(
bool isConfigurationValid, // What IsValid will return.
bool warmCache, // If true, cache will be populated before tests.
bool forceCacheRefresh, // If true then the cache will be populated.
bool isValidCallExpected // If true, IsValid must be called.
)
{
var proxy = new Proxy(isConfigurationValid); // IsValid will return true.

// Should we warm the cache?
if (warmCache)
{
proxy.IsCurrentConfigurationValid(null, forceCacheRefresh); // Call it once so that it populates the cache.
proxy.IsValidCalled = false; // Reset our flag.
}

// Does it return the correct valid?
Assert.AreEqual(isConfigurationValid, proxy.IsCurrentConfigurationValid(null, forceCacheRefresh));

// Did it call IsValid?
Assert.AreEqual(isValidCallExpected, proxy.IsValidCalled);
}

private class Proxy
: ConfigurableVaultApplicationBaseProxy
{
public bool IsConfigurationValid { get; } = false;
public bool IsValidCalled { get; set; } = false;
public Proxy(bool isConfigurationValid)
{
this.IsConfigurationValid = isConfigurationValid;
}
public override bool IsValid(Vault vault)
{
this.IsValidCalled = true;
return this.IsConfigurationValid;
}
public new bool IsCurrentConfigurationValid(Vault vault, bool force = false)
{
return base.IsCurrentConfigurationValid(vault, force);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,30 @@ public abstract partial class ConfigurableVaultApplicationBase<TSecureConfigurat
: MFiles.VAF.Core.ConfigurableVaultApplicationBase<TSecureConfiguration>
where TSecureConfiguration : class, new()
{
/// <summary>
/// Whether the current configuration is valid.
/// </summary>
private bool? isCurrentConfigurationValid;

/// <summary>
/// Returns whether the configuration is valid.
/// Note: uses a cached value that is updated during
/// <see cref="StartOperations(Vault)"/> and <see cref="OnConfigurationUpdated(Configuration, bool, bool)"/>.
/// Only re-validates if the status is unknown, or if <paramref name="force"/> is <see langword="true"/>.
/// Validation is done by calling <see cref="Core.ConfigurableVaultApplicationBase{TSecureConfiguration}.IsValid"/>.
/// </summary>
/// <param name="vault">The vault reference to use to validate, if the status is unknown or <paramref name="force"/> is <see langword="true"/>.</param>
/// <param name="force">If <see langword="true"/> then the cached value is updated. This incurs a performance hit and should not be used often.</param>
/// <returns><see langword="true"/> if the configuration is valid, <see langword="false"/> otherwise.</returns>
protected bool IsCurrentConfigurationValid(Vault vault, bool force = false)
{
this.isCurrentConfigurationValid =
this.isCurrentConfigurationValid.HasValue && !force
? this.isCurrentConfigurationValid.Value // No-op.
: this.IsValid(vault); // Use the IsValid method.
return this.isCurrentConfigurationValid.Value;
}

/// <inheritdoc />
protected override void OnConfigurationUpdated(TSecureConfiguration oldConfiguration, bool isValid, bool updateExternals)
{
Expand All @@ -27,7 +51,10 @@ protected override void OnConfigurationUpdated(TSecureConfiguration oldConfigura

// Populate the task processing schedule configuration.
this.RecurringOperationConfigurationManager?.PopulateFromConfiguration(isVaultStartup: false);


// Update the cached value with the validity status.
this.isCurrentConfigurationValid = isValid;

// If we have logging configuration then set it up.
var loggingConfiguration = this.GetLoggingConfiguration();
this.Logger?.Debug($"Logging configuration updating");
Expand All @@ -45,6 +72,9 @@ internal new SecureConfigurationManager<TSecureConfiguration> ConfManager
/// <inheritdoc />
public override void StartOperations(Vault vaultPersistent)
{
// Do we have a valid configuration?
this.isCurrentConfigurationValid = this.IsValid(vaultPersistent);

// Initialize the application.
base.StartOperations(vaultPersistent);

Expand Down

0 comments on commit 255ebf5

Please sign in to comment.