Skip to content

Commit b819cc7

Browse files
committed
PR
1 parent 6f38b2a commit b819cc7

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

src/MongoDB.Driver/OperationContext.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
using System.Diagnostics;
1818
using System.Threading;
1919
using System.Threading.Tasks;
20-
using MongoDB.Driver.Core.Misc;
2120

2221
namespace MongoDB.Driver
2322
{
@@ -26,26 +25,26 @@ internal sealed class OperationContext
2625
// TODO: this static field is temporary here and will be removed in a future PRs in scope of CSOT.
2726
public static readonly OperationContext NoTimeout = new(System.Threading.Timeout.InfiniteTimeSpan, CancellationToken.None);
2827

29-
private readonly Stopwatch _stopwatch;
30-
3128
public OperationContext(TimeSpan timeout, CancellationToken cancellationToken)
3229
: this(Stopwatch.StartNew(), timeout, cancellationToken)
3330
{
3431
}
3532

3633
internal OperationContext(Stopwatch stopwatch, TimeSpan timeout, CancellationToken cancellationToken)
3734
{
38-
_stopwatch = stopwatch;
35+
Stopwatch = stopwatch;
3936
Timeout = timeout;
4037
CancellationToken = cancellationToken;
4138
}
4239

4340
public CancellationToken CancellationToken { get; }
4441

45-
public TimeSpan Elapsed => _stopwatch.Elapsed;
42+
public TimeSpan Elapsed => Stopwatch.Elapsed;
4643

4744
public TimeSpan Timeout { get; }
4845

46+
public OperationContext ParentContext { get; private init; }
47+
4948
public TimeSpan RemainingTimeout
5049
{
5150
get
@@ -55,10 +54,12 @@ public TimeSpan RemainingTimeout
5554
return System.Threading.Timeout.InfiniteTimeSpan;
5655
}
5756

58-
return Timeout - _stopwatch.Elapsed;
57+
return Timeout - Elapsed;
5958
}
6059
}
6160

61+
private Stopwatch Stopwatch { get; }
62+
6263
public bool IsTimedOut()
6364
{
6465
var remainingTimeout = RemainingTimeout;
@@ -82,7 +83,10 @@ public OperationContext WithTimeout(TimeSpan timeout)
8283
timeout = remainingTimeout;
8384
}
8485

85-
return new OperationContext(timeout, CancellationToken);
86+
return new OperationContext(timeout, CancellationToken)
87+
{
88+
ParentContext = this
89+
};
8690
}
8791

8892
public void WaitTask(Task task)

tests/MongoDB.Driver.Tests/OperationContextTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public void Constructor_should_initialize_properties()
3535

3636
operationContext.Timeout.Should().Be(timeout);
3737
operationContext.CancellationToken.Should().Be(cancellationToken);
38+
operationContext.ParentContext.Should().BeNull();
3839
}
3940

4041
[Fact]
@@ -127,6 +128,15 @@ public void WithTimeout_should_calculate_proper_timeout(TimeSpan expected, TimeS
127128
[TimeSpan.FromMilliseconds(5), TimeSpan.FromMilliseconds(10), TimeSpan.FromMilliseconds(5)],
128129
];
129130

131+
[Fact]
132+
public void WithTimeout_should_set_ParentContext()
133+
{
134+
var operationContext = new OperationContext(new Stopwatch(), Timeout.InfiniteTimeSpan, CancellationToken.None);
135+
var resultContext = operationContext.WithTimeout(TimeSpan.FromSeconds(10));
136+
137+
resultContext.ParentContext.Should().Be(operationContext);
138+
}
139+
130140
// TODO: Add tests for WaitTask and WaitTaskAsync.
131141
}
132142
}

0 commit comments

Comments
 (0)