47 lines
2.5 KiB
C#
47 lines
2.5 KiB
C#
using WxAgent.Service;
|
|
using Xunit;
|
|
|
|
namespace WxAgent.Service.Tests;
|
|
|
|
public sealed class OperationStoreTests
|
|
{
|
|
[Fact]
|
|
public void DurableIdempotencyOwnershipCapacityAndRestartNeverReplay()
|
|
{
|
|
var directory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"));
|
|
var options = new ServiceOptions { DataDirectory = directory, CredentialFile = "unused" };
|
|
try
|
|
{
|
|
string runningId;
|
|
string queuedId;
|
|
using (var store = new OperationStore(options))
|
|
{
|
|
var first = store.Enqueue("alice", "account", "send-text", "one", "digest", true, TimeSpan.FromMinutes(1), 2);
|
|
runningId = first.Record.Id;
|
|
Assert.True(first.Created);
|
|
var again = store.Enqueue("alice", "account", "send-text", "one", "digest", true, TimeSpan.FromMinutes(1), 2);
|
|
Assert.False(again.Created);
|
|
Assert.Equal(runningId, again.Record.Id);
|
|
Assert.Equal("IdempotencyConflict", Assert.Throws<ServiceException>(() =>
|
|
store.Enqueue("alice", "account", "send-text", "one", "different", true, TimeSpan.FromMinutes(1), 2)).Code);
|
|
Assert.Equal("NotFound", Assert.Throws<ServiceException>(() => store.Get(runningId, "bob")).Code);
|
|
queuedId = store.Enqueue("bob", "account", "send-text", "one", "digest", true, TimeSpan.FromMinutes(1), 2).Record.Id;
|
|
Assert.Equal("QueueFull", Assert.Throws<ServiceException>(() =>
|
|
store.Enqueue("alice", "account", "send-text", "two", "digest", true, TimeSpan.FromMinutes(1), 2)).Code);
|
|
store.Transition(runningId, "Running", "executing");
|
|
}
|
|
using (var restarted = new OperationStore(options))
|
|
{
|
|
Assert.Equal("Unconfirmed", restarted.Get(runningId, "alice").State);
|
|
Assert.Equal("Cancelled", restarted.Get(queuedId, "bob").State);
|
|
var again = restarted.Enqueue("alice", "account", "send-text", "one", "digest", true, TimeSpan.FromMinutes(1), 2);
|
|
Assert.False(again.Created);
|
|
Assert.Equal("Unconfirmed", again.Record.State);
|
|
restarted.Transition(runningId, "Succeeded", "late-completion");
|
|
Assert.Equal("Unconfirmed", restarted.Get(runningId, "alice").State);
|
|
}
|
|
}
|
|
finally { Microsoft.Data.Sqlite.SqliteConnection.ClearAllPools(); Directory.Delete(directory, true); }
|
|
}
|
|
}
|