From 6c6a42979e7b15f5af327aa5528ee127f85e9436 Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Mon, 30 Mar 2026 19:49:28 +0200 Subject: [PATCH] Add comments for ScenarioStateStore related code (#1433) --- .../Handlers/IScenarioStateStore.cs | 4 +--- .../Handlers/FileBasedScenarioStateStore.cs | 24 +++++++++++++++---- .../Handlers/InMemoryScenarioStateStore.cs | 16 +++++++++---- .../Settings/WireMockServerSettings.cs | 9 ++++++- .../WireMock.Net.Tests.UsingNuGet.csproj | 4 ++-- 5 files changed, 42 insertions(+), 15 deletions(-) diff --git a/src/WireMock.Net.Abstractions/Handlers/IScenarioStateStore.cs b/src/WireMock.Net.Abstractions/Handlers/IScenarioStateStore.cs index e39e9da3..85b96392 100644 --- a/src/WireMock.Net.Abstractions/Handlers/IScenarioStateStore.cs +++ b/src/WireMock.Net.Abstractions/Handlers/IScenarioStateStore.cs @@ -1,7 +1,5 @@ // Copyright © WireMock.Net -using System; -using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; namespace WireMock.Handlers; @@ -23,4 +21,4 @@ public interface IScenarioStateStore bool TryRemove(string name); void Clear(); -} +} \ No newline at end of file diff --git a/src/WireMock.Net.Minimal/Handlers/FileBasedScenarioStateStore.cs b/src/WireMock.Net.Minimal/Handlers/FileBasedScenarioStateStore.cs index 53c57b41..515d3827 100644 --- a/src/WireMock.Net.Minimal/Handlers/FileBasedScenarioStateStore.cs +++ b/src/WireMock.Net.Minimal/Handlers/FileBasedScenarioStateStore.cs @@ -1,43 +1,53 @@ // Copyright © WireMock.Net -using System; using System.Collections.Concurrent; -using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; -using System.IO; -using System.Linq; using Newtonsoft.Json; +using Stef.Validation; namespace WireMock.Handlers; +/// +/// Provides a file-based implementation of that persists scenario states to disk and allows concurrent access. +/// public class FileBasedScenarioStateStore : IScenarioStateStore { private readonly ConcurrentDictionary _scenarios = new(StringComparer.OrdinalIgnoreCase); private readonly string _scenariosFolder; private readonly object _lock = new(); + /// + /// Initializes a new instance of the FileBasedScenarioStateStore class using the specified root folder as the base directory for scenario state storage. + /// + /// The root directory under which scenario state data will be stored. Must be a valid file system path. public FileBasedScenarioStateStore(string rootFolder) { + Guard.NotNullOrEmpty(rootFolder); + _scenariosFolder = Path.Combine(rootFolder, "__admin", "scenarios"); Directory.CreateDirectory(_scenariosFolder); LoadScenariosFromDisk(); } + /// public bool TryGet(string name, [NotNullWhen(true)] out ScenarioState? state) { return _scenarios.TryGetValue(name, out state); } + /// public IReadOnlyList GetAll() { return _scenarios.Values.ToArray(); } + /// public bool ContainsKey(string name) { return _scenarios.ContainsKey(name); } + /// public bool TryAdd(string name, ScenarioState scenarioState) { if (_scenarios.TryAdd(name, scenarioState)) @@ -49,6 +59,7 @@ public class FileBasedScenarioStateStore : IScenarioStateStore return false; } + /// public ScenarioState AddOrUpdate(string name, Func addFactory, Func updateFactory) { lock (_lock) @@ -59,6 +70,7 @@ public class FileBasedScenarioStateStore : IScenarioStateStore } } + /// public ScenarioState? Update(string name, Action updateAction) { lock (_lock) @@ -74,6 +86,7 @@ public class FileBasedScenarioStateStore : IScenarioStateStore } } + /// public bool TryRemove(string name) { if (_scenarios.TryRemove(name, out _)) @@ -85,6 +98,7 @@ public class FileBasedScenarioStateStore : IScenarioStateStore return false; } + /// public void Clear() { _scenarios.Clear(); @@ -128,4 +142,4 @@ public class FileBasedScenarioStateStore : IScenarioStateStore } } } -} +} \ No newline at end of file diff --git a/src/WireMock.Net.Shared/Handlers/InMemoryScenarioStateStore.cs b/src/WireMock.Net.Shared/Handlers/InMemoryScenarioStateStore.cs index 0af16776..4ef27a3e 100644 --- a/src/WireMock.Net.Shared/Handlers/InMemoryScenarioStateStore.cs +++ b/src/WireMock.Net.Shared/Handlers/InMemoryScenarioStateStore.cs @@ -1,42 +1,48 @@ // Copyright © WireMock.Net -using System; using System.Collections.Concurrent; -using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; -using System.Linq; namespace WireMock.Handlers; +/// +/// Provides an in-memory implementation of the interface for managing scenario state objects by name. +/// public class InMemoryScenarioStateStore : IScenarioStateStore { private readonly ConcurrentDictionary _scenarios = new(StringComparer.OrdinalIgnoreCase); + /// public bool TryGet(string name, [NotNullWhen(true)] out ScenarioState? state) { return _scenarios.TryGetValue(name, out state); } + /// public IReadOnlyList GetAll() { return _scenarios.Values.ToArray(); } + /// public bool ContainsKey(string name) { return _scenarios.ContainsKey(name); } + /// public bool TryAdd(string name, ScenarioState scenarioState) { return _scenarios.TryAdd(name, scenarioState); } + /// public ScenarioState AddOrUpdate(string name, Func addFactory, Func updateFactory) { return _scenarios.AddOrUpdate(name, addFactory, updateFactory); } + /// public ScenarioState? Update(string name, Action updateAction) { if (_scenarios.TryGetValue(name, out var state)) @@ -48,13 +54,15 @@ public class InMemoryScenarioStateStore : IScenarioStateStore return null; } + /// public bool TryRemove(string name) { return _scenarios.TryRemove(name, out _); } + /// public void Clear() { _scenarios.Clear(); } -} +} \ No newline at end of file diff --git a/src/WireMock.Net.Shared/Settings/WireMockServerSettings.cs b/src/WireMock.Net.Shared/Settings/WireMockServerSettings.cs index 258cd3a8..1f1b343c 100644 --- a/src/WireMock.Net.Shared/Settings/WireMockServerSettings.cs +++ b/src/WireMock.Net.Shared/Settings/WireMockServerSettings.cs @@ -175,6 +175,13 @@ public class WireMockServerSettings [JsonIgnore] public IFileSystemHandler FileSystemHandler { get; set; } = null!; + /// + /// Gets or sets the store used to persist scenario state information. + /// + /// + /// The scenario state store manages the storage and retrieval of state data associated with scenarios. + /// By default, an in-memory implementation is used, but this property can be set to a custom implementation to support alternative storage mechanisms such as databases or distributed caches. + /// [PublicAPI] [JsonIgnore] public IScenarioStateStore ScenarioStateStore { get; set; } = new InMemoryScenarioStateStore(); @@ -258,7 +265,7 @@ public class WireMockServerSettings /// Whether to accept any client certificate /// public bool AcceptAnyClientCertificate { get; set; } - + /// /// Defines the global IWebhookSettings to use. /// diff --git a/test/WireMock.Net.Tests.UsingNuGet/WireMock.Net.Tests.UsingNuGet.csproj b/test/WireMock.Net.Tests.UsingNuGet/WireMock.Net.Tests.UsingNuGet.csproj index 7800b45d..f42ac291 100644 --- a/test/WireMock.Net.Tests.UsingNuGet/WireMock.Net.Tests.UsingNuGet.csproj +++ b/test/WireMock.Net.Tests.UsingNuGet/WireMock.Net.Tests.UsingNuGet.csproj @@ -11,12 +11,12 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive - + all