mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-26 19:31:48 +01:00
Add injectable IScenarioStateStore for distributed scenario state (#1430)
* Move ScenarioState to Abstractions and add IScenarioStateStore interface ScenarioState is moved to the Abstractions project so it can be referenced by the new IScenarioStateStore interface. The interface defines the contract for storing and retrieving scenario states, enabling distributed implementations. * Add InMemoryScenarioStateStore default implementation Wraps ConcurrentDictionary with OrdinalIgnoreCase comparer, preserving exact current behavior. The Update method encapsulates read-modify-write so distributed implementations can make it atomic. * Wire IScenarioStateStore into middleware options, settings, and consumers Replace direct ConcurrentDictionary<string, ScenarioState> usage with IScenarioStateStore across all consumer files. The store is injectable via WireMockServerSettings.ScenarioStateStore, defaulting to the InMemoryScenarioStateStore for backward compatibility. * Add FileBasedScenarioStateStore for persistent scenario state In-memory ConcurrentDictionary backed by JSON file persistence in __admin/scenarios/. Reads from cache, mutations write through to disk. Constructor loads existing state from disk on startup. * Make ScenarioStateStore non-nullable with default InMemoryScenarioStateStore Move InMemoryScenarioStateStore from WireMock.Net.Minimal to WireMock.Net.Shared so it lives alongside WireMockServerSettings. This allows WireMockServerSettings.ScenarioStateStore to be non-nullable with a default value, following the same pattern as DefaultJsonSerializer. The null-coalescing fallback in WireMockMiddlewareOptionsHelper is no longer needed.
This commit is contained in:
@@ -325,26 +325,26 @@ public class StatefulBehaviorTests
|
||||
var getResponse1 = await client.GetStringAsync("/todo/items", cancelationToken);
|
||||
getResponse1.Should().Be("Buy milk");
|
||||
|
||||
server.Scenarios["To do list"].Name.Should().Be("To do list");
|
||||
server.Scenarios["To do list"].NextState.Should().Be("TodoList State Started");
|
||||
server.Scenarios["To do list"].Started.Should().BeTrue();
|
||||
server.Scenarios["To do list"].Finished.Should().BeFalse();
|
||||
server.Scenarios.First(s => s.Name == "To do list").Name.Should().Be("To do list");
|
||||
server.Scenarios.First(s => s.Name == "To do list").NextState.Should().Be("TodoList State Started");
|
||||
server.Scenarios.First(s => s.Name == "To do list").Started.Should().BeTrue();
|
||||
server.Scenarios.First(s => s.Name == "To do list").Finished.Should().BeFalse();
|
||||
|
||||
var postResponse = await client.PostAsync("/todo/items", new StringContent("Cancel newspaper subscription"), cancelationToken);
|
||||
postResponse.StatusCode.Should().Be(HttpStatusCode.Created);
|
||||
|
||||
server.Scenarios["To do list"].Name.Should().Be("To do list");
|
||||
server.Scenarios["To do list"].NextState.Should().Be("Cancel newspaper item added");
|
||||
server.Scenarios["To do list"].Started.Should().BeTrue();
|
||||
server.Scenarios["To do list"].Finished.Should().BeFalse();
|
||||
server.Scenarios.First(s => s.Name == "To do list").Name.Should().Be("To do list");
|
||||
server.Scenarios.First(s => s.Name == "To do list").NextState.Should().Be("Cancel newspaper item added");
|
||||
server.Scenarios.First(s => s.Name == "To do list").Started.Should().BeTrue();
|
||||
server.Scenarios.First(s => s.Name == "To do list").Finished.Should().BeFalse();
|
||||
|
||||
string getResponse2 = await client.GetStringAsync("/todo/items", cancelationToken);
|
||||
getResponse2.Should().Be("Buy milk;Cancel newspaper subscription");
|
||||
|
||||
server.Scenarios["To do list"].Name.Should().Be("To do list");
|
||||
server.Scenarios["To do list"].NextState.Should().BeNull();
|
||||
server.Scenarios["To do list"].Started.Should().BeTrue();
|
||||
server.Scenarios["To do list"].Finished.Should().BeTrue();
|
||||
server.Scenarios.First(s => s.Name == "To do list").Name.Should().Be("To do list");
|
||||
server.Scenarios.First(s => s.Name == "To do list").NextState.Should().BeNull();
|
||||
server.Scenarios.First(s => s.Name == "To do list").Started.Should().BeTrue();
|
||||
server.Scenarios.First(s => s.Name == "To do list").Finished.Should().BeTrue();
|
||||
|
||||
server.Stop();
|
||||
}
|
||||
@@ -372,14 +372,14 @@ public class StatefulBehaviorTests
|
||||
|
||||
// Act and Assert
|
||||
server.SetScenarioState(scenario, "Buy milk");
|
||||
server.Scenarios[scenario].Should().BeEquivalentTo(new { Name = scenario, NextState = "Buy milk" });
|
||||
server.Scenarios.First(s => s.Name == scenario).Should().BeEquivalentTo(new { Name = scenario, NextState = "Buy milk" });
|
||||
|
||||
var getResponse1 = await client.GetStringAsync("/todo/items", cancelationToken);
|
||||
getResponse1.Should().Be("Buy milk");
|
||||
|
||||
server.SetScenarioState(scenario, "Cancel newspaper");
|
||||
server.Scenarios[scenario].Name.Should().Be(scenario);
|
||||
server.Scenarios[scenario].Should().BeEquivalentTo(new { Name = scenario, NextState = "Cancel newspaper" });
|
||||
server.Scenarios.First(s => s.Name == scenario).Name.Should().Be(scenario);
|
||||
server.Scenarios.First(s => s.Name == scenario).Should().BeEquivalentTo(new { Name = scenario, NextState = "Cancel newspaper" });
|
||||
|
||||
var getResponse2 = await client.GetStringAsync("/todo/items", cancelationToken);
|
||||
getResponse2.Should().Be("Buy milk;Cancel newspaper subscription");
|
||||
|
||||
Reference in New Issue
Block a user