Skip to content

Commit 1961663

Browse files
Ensure that CommitDelayIncrement is greater than TimeSpan.Zero (#531)
* Throw argument out of range exception when CommitDelayIncrement is set to a negative value in OpenSessionOptions, and add an acceptance test to verify that an exception is thrown to the session user when CommitDelayIncrement is set to zero. Co-authored-by: Tamara Rivera <tamita.rivera@gmail.com>
1 parent eeb5f21 commit 1961663

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
namespace NServiceBus.TransactionalSession.Tests;
2+
3+
using System;
4+
using NUnit.Framework;
5+
6+
public class OpenSessionOptionsTests
7+
{
8+
[Test]
9+
public void Should_not_throw_exception_when_commit_delay_increment_is_greater_than_zero() =>
10+
Assert.DoesNotThrow(() =>
11+
{
12+
var options = new CustomTestingPersistenceOpenSessionOptions
13+
{
14+
CommitDelayIncrement = TimeSpan.FromTicks(1),
15+
MaximumCommitDuration = TimeSpan.FromSeconds(8)
16+
};
17+
});
18+
19+
[Test]
20+
[TestCase(-1)]
21+
[TestCase(0)]
22+
public void Should_throw_exception_when_commit_delay_increment_is_less_than_or_equal_to_zero(int commitDelayIncrement) =>
23+
Assert.Throws<ArgumentOutOfRangeException>(() =>
24+
{
25+
var options = new CustomTestingPersistenceOpenSessionOptions
26+
{
27+
CommitDelayIncrement = TimeSpan.FromSeconds(commitDelayIncrement),
28+
MaximumCommitDuration = TimeSpan.FromSeconds(8)
29+
};
30+
});
31+
32+
class CustomTestingPersistenceOpenSessionOptions : OpenSessionOptions;
33+
}
34+

src/NServiceBus.TransactionalSession/OpenSessionOptions.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,17 @@ public abstract class OpenSessionOptions
3737
/// <summary>
3838
/// The time increment used to delay the commit of the transactional session when the outbox record is not yet in the storage.
3939
/// </summary>
40-
/// <remarks>Defaults to <code>TimeSpan.FromSeconds(2)</code></remarks>
41-
public TimeSpan CommitDelayIncrement { get; set; } = TimeSpan.FromSeconds(2);
40+
/// <remarks>Defaults to <code>TimeSpan.FromSeconds(2)</code>. Value must be greater than <code>TimeSpan.Zero</code>.</remarks>
41+
public TimeSpan CommitDelayIncrement
42+
{
43+
get => commitDelayIncrement;
44+
set
45+
{
46+
ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(value, TimeSpan.Zero, nameof(CommitDelayIncrement));
47+
commitDelayIncrement = value;
48+
}
49+
}
4250

4351
Dictionary<string, string>? metadata;
52+
TimeSpan commitDelayIncrement = TimeSpan.FromSeconds(2);
4453
}

0 commit comments

Comments
 (0)