Skip to content

Commit

Permalink
Only perform I/O for existing registered timeouts in TimeoutProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
stidsborg committed Jul 31, 2024
1 parent 0846899 commit e09391f
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,8 @@ public override Task RegisteredTimeoutIsReturnedFromTimeoutProviderForFunctionId
[TestMethod]
public override Task TimeoutIsNotRegisteredAgainWhenProviderAlreadyContainsTimeout()
=> TimeoutIsNotRegisteredAgainWhenProviderAlreadyContainsTimeout(new InMemoryTimeoutStore().CastTo<ITimeoutStore>().ToTask());

[TestMethod]
public override Task CancellingNonExistingTimeoutDoesNotResultInIO()
=> CancellingNonExistingTimeoutDoesNotResultInIO(FunctionStoreFactory.Create().SelectAsync(s => s.TimeoutStore));
}
Original file line number Diff line number Diff line change
Expand Up @@ -181,30 +181,62 @@ protected async Task TimeoutIsNotRegisteredAgainWhenProviderAlreadyContainsTimeo
await timeoutProvider.RegisterTimeout("timeoutId1", expiresIn: TimeSpan.FromHours(1), overwrite: false);
upsertCount.ShouldBe(1);
}

public abstract Task CancellingNonExistingTimeoutDoesNotResultInIO();
protected async Task CancellingNonExistingTimeoutDoesNotResultInIO(Task<ITimeoutStore> storeTask)
{
var removeCount = 0;
var store = new TimeoutStoreDecorator(await storeTask, removeTimeoutCallback: () => removeCount++);
var functionId = TestFlowId.Create();

var timeoutProvider = new TimeoutProvider(
functionId,
store,
messageWriter: null,
timeoutCheckFrequency: TimeSpan.Zero
);

var pendingTimeouts = await timeoutProvider.PendingTimeouts();
pendingTimeouts.ShouldBeEmpty();

await timeoutProvider.RegisterTimeout("SomeOtherTimeoutId", expiresIn: TimeSpan.FromHours(1));

await timeoutProvider.CancelTimeout("SomeTimeoutId");

removeCount.ShouldBe(0);
}

private class TimeoutStoreDecorator : ITimeoutStore
{
private readonly ITimeoutStore _inner;
private readonly Action _upsertTimeoutCallback;
private readonly Action? _upsertTimeoutCallback;
private readonly Action? _removeTimeoutCallback;

public TimeoutStoreDecorator(ITimeoutStore inner, Action upsertTimeoutCallback)
public TimeoutStoreDecorator(
ITimeoutStore inner,
Action? upsertTimeoutCallback = null,
Action? removeTimeoutCallback = null)
{
_inner = inner;
_upsertTimeoutCallback = upsertTimeoutCallback;
_removeTimeoutCallback = removeTimeoutCallback;
}

public Task Initialize() => _inner.Initialize();
public Task Truncate() => _inner.Truncate();

public Task UpsertTimeout(StoredTimeout storedTimeout, bool overwrite)
{
_upsertTimeoutCallback();
_upsertTimeoutCallback?.Invoke();
return _inner.UpsertTimeout(storedTimeout, overwrite);
}

public Task RemoveTimeout(FlowId flowId, string timeoutId)
=> _inner.RemoveTimeout(flowId, timeoutId);

{
_removeTimeoutCallback?.Invoke();
return _inner.RemoveTimeout(flowId, timeoutId);
}

public Task Remove(FlowId flowId)
=> _inner.Remove(flowId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ await _messageWriter.AppendMessage(
public async Task CancelTimeout(string timeoutId)
{
lock (_sync)
_localTimeouts.Remove(timeoutId);
if (!_localTimeouts.Remove(timeoutId))
return;

await _timeoutStore.RemoveTimeout(_flowId, timeoutId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@ public override Task RegisteredTimeoutIsReturnedFromTimeoutProviderForFunctionId
[TestMethod]
public override Task TimeoutIsNotRegisteredAgainWhenProviderAlreadyContainsTimeout()
=> TimeoutIsNotRegisteredAgainWhenProviderAlreadyContainsTimeout(FunctionStoreFactory.Create().SelectAsync(s => s.TimeoutStore));

[TestMethod]
public override Task CancellingNonExistingTimeoutDoesNotResultInIO()
=> CancellingNonExistingTimeoutDoesNotResultInIO(FunctionStoreFactory.Create().SelectAsync(s => s.TimeoutStore));
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ public override Task RegisteredTimeoutIsReturnedFromTimeoutProviderForFunctionId
public override Task TimeoutIsNotRegisteredAgainWhenProviderAlreadyContainsTimeout()
=> TimeoutIsNotRegisteredAgainWhenProviderAlreadyContainsTimeout(FunctionStoreFactory.Create().SelectAsync(s => s.TimeoutStore));

[TestMethod]
public override Task CancellingNonExistingTimeoutDoesNotResultInIO()
=> CancellingNonExistingTimeoutDoesNotResultInIO(FunctionStoreFactory.Create().SelectAsync(s => s.TimeoutStore));

[TestMethod]
public override Task RegisteredTimeoutIsReturnedFromTimeoutProvider()
=> RegisteredTimeoutIsReturnedFromTimeoutProvider(FunctionStoreFactory.Create().SelectAsync(s => s.TimeoutStore));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,8 @@ public override Task RegisteredTimeoutIsReturnedFromTimeoutProviderForFunctionId
[TestMethod]
public override Task TimeoutIsNotRegisteredAgainWhenProviderAlreadyContainsTimeout()
=> TimeoutIsNotRegisteredAgainWhenProviderAlreadyContainsTimeout(FunctionStoreFactory.Create().SelectAsync(s => s.TimeoutStore));

[TestMethod]
public override Task CancellingNonExistingTimeoutDoesNotResultInIO()
=> CancellingNonExistingTimeoutDoesNotResultInIO(FunctionStoreFactory.Create().SelectAsync(s => s.TimeoutStore));
}

0 comments on commit e09391f

Please sign in to comment.