Skip to content

Commit 0bc63d6

Browse files
committed
CSHARP-1459: Added support for MaxAwaitTime for TailableAwait cursors.
1 parent 695fd71 commit 0bc63d6

File tree

11 files changed

+120
-1
lines changed

11 files changed

+120
-1
lines changed

src/MongoDB.Driver.Core.Tests/Core/Operations/FindCommandOperationTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,20 @@ public void MaxScan_get_and_set_should_work(
666666
result.Should().Be(value);
667667
}
668668

669+
[Test]
670+
public void MaxAwaitTime_get_and_set_should_work(
671+
[Values(null, 1)]
672+
int? seconds)
673+
{
674+
var subject = new FindCommandOperation<BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings);
675+
var value = seconds == null ? (TimeSpan?)null : TimeSpan.FromSeconds(seconds.Value);
676+
677+
subject.MaxAwaitTime = value;
678+
var result = subject.MaxAwaitTime;
679+
680+
result.Should().Be(value);
681+
}
682+
669683
[Test]
670684
public void MaxTime_get_and_set_should_work(
671685
[Values(null, 1)]

src/MongoDB.Driver.Core.Tests/Core/Operations/FindOperationTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ public void CreateFindCommandOperation_should_return_expected_result()
166166
subject.Limit = 3;
167167
subject.Max = new BsonDocument("max", 1);
168168
subject.MaxScan = 4;
169+
subject.MaxAwaitTime = TimeSpan.FromSeconds(2);
169170
subject.MaxTime = TimeSpan.FromSeconds(1);
170171
subject.Min = new BsonDocument("min", 1);
171172
subject.NoCursorTimeout = true;
@@ -192,6 +193,7 @@ public void CreateFindCommandOperation_should_return_expected_result()
192193
result.Limit.Should().Be(subject.Limit);
193194
result.Max.Should().Be(subject.Max);
194195
result.MaxScan.Should().Be(subject.MaxScan);
196+
result.MaxAwaitTime.Should().Be(subject.MaxAwaitTime);
195197
result.MaxTime.Should().Be(subject.MaxTime);
196198
result.MessageEncoderSettings.Should().BeSameAs(subject.MessageEncoderSettings);
197199
result.Min.Should().Be(subject.Min);
@@ -489,6 +491,20 @@ public void MaxScan_get_and_set_should_work(
489491
result.Should().Be(value);
490492
}
491493

494+
[Test]
495+
public void MaxAwaitTime_get_and_set_should_work(
496+
[Values(null, 1)]
497+
int? seconds)
498+
{
499+
var subject = new FindOperation<BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings);
500+
var value = seconds == null ? (TimeSpan?)null : TimeSpan.FromSeconds(seconds.Value);
501+
502+
subject.MaxAwaitTime = value;
503+
var result = subject.MaxAwaitTime;
504+
505+
result.Should().Be(value);
506+
}
507+
492508
[Test]
493509
public void MaxTime_get_and_set_should_work(
494510
[Values(null, 1)]

src/MongoDB.Driver.Core/Core/Operations/FindCommandOperation.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public class FindCommandOperation<TDocument> : IReadOperation<IAsyncCursor<TDocu
5353
private BsonValue _hint;
5454
private int? _limit;
5555
private BsonDocument _max;
56+
private TimeSpan? _maxAwaitTime;
5657
private int? _maxScan;
5758
private TimeSpan? _maxTime;
5859
private readonly MessageEncoderSettings _messageEncoderSettings;
@@ -207,6 +208,18 @@ public BsonDocument Max
207208
set { _max = value; }
208209
}
209210

211+
/// <summary>
212+
/// Gets or sets the maximum await time for TailableAwait cursors.
213+
/// </summary>
214+
/// <value>
215+
/// The maximum await time for TailableAwait cursors.
216+
/// </value>
217+
public TimeSpan? MaxAwaitTime
218+
{
219+
get { return _maxAwaitTime; }
220+
set { _maxAwaitTime = value; }
221+
}
222+
210223
/// <summary>
211224
/// Gets or sets the max scan.
212225
/// </summary>
@@ -437,7 +450,7 @@ private AsyncCursor<TDocument> CreateCursor(IChannelSourceHandle channelSource,
437450
_limit < 0 ? Math.Abs(_limit.Value) : _limit,
438451
_resultSerializer,
439452
_messageEncoderSettings,
440-
null); // maxTime
453+
_cursorType == CursorType.TailableAwait ? _maxAwaitTime : null);
441454
}
442455

443456
private CursorBatch<TDocument> CreateCursorBatch(BsonDocument result)

src/MongoDB.Driver.Core/Core/Operations/FindOperation.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public class FindOperation<TDocument> : IReadOperation<IAsyncCursor<TDocument>>
4848
private int? _limit;
4949
private BsonDocument _max;
5050
private int? _maxScan;
51+
private TimeSpan? _maxAwaitTime;
5152
private TimeSpan? _maxTime;
5253
private readonly MessageEncoderSettings _messageEncoderSettings;
5354
private BsonDocument _min;
@@ -202,6 +203,18 @@ public BsonDocument Max
202203
set { _max = value; }
203204
}
204205

206+
/// <summary>
207+
/// Gets or sets the maximum await time for TailableAwait cursors.
208+
/// </summary>
209+
/// <value>
210+
/// The maximum await time for TailableAwait cursors.
211+
/// </value>
212+
public TimeSpan? MaxAwaitTime
213+
{
214+
get { return _maxAwaitTime; }
215+
set { _maxAwaitTime = value; }
216+
}
217+
205218
/// <summary>
206219
/// Gets or sets the max scan.
207220
/// </summary>
@@ -471,6 +484,7 @@ internal FindCommandOperation<TDocument> CreateFindCommandOperation()
471484
FirstBatchSize = _firstBatchSize,
472485
Limit = _limit,
473486
Max = max,
487+
MaxAwaitTime = _maxAwaitTime,
474488
MaxScan = maxScan,
475489
MaxTime = maxTime,
476490
Min = min,

src/MongoDB.Driver.Legacy/MongoCursor.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public abstract class MongoCursor : IEnumerable
4343
private IMongoFields _fields;
4444
private BsonDocument _options;
4545
private QueryFlags _flags;
46+
private TimeSpan? _maxAwaitTime;
4647
private ReadConcern _readConcern = ReadConcern.Default;
4748
private ReadPreference _readPreference;
4849
private IBsonSerializer _serializer;
@@ -165,6 +166,22 @@ public virtual QueryFlags Flags
165166
}
166167
}
167168

169+
/// <summary>
170+
/// Gets or sets the maximum await time for TailableAwait cursors.
171+
/// </summary>
172+
/// <value>
173+
/// The maximum await time for TailableAwait cursors.
174+
/// </value>
175+
public virtual TimeSpan? MaxAwaitTime
176+
{
177+
get { return _maxAwaitTime; }
178+
set
179+
{
180+
if (_isFrozen) { ThrowFrozen(); }
181+
_maxAwaitTime = value;
182+
}
183+
}
184+
168185
/// <summary>
169186
/// Gets the read concern.
170187
/// </summary>
@@ -332,6 +349,7 @@ public virtual MongoCursor Clone(Type documentType, IBsonSerializer serializer)
332349
clone._flags = _flags;
333350
clone._skip = _skip;
334351
clone._limit = _limit;
352+
clone._maxAwaitTime = _maxAwaitTime;
335353
clone._batchSize = _batchSize;
336354
clone._fields = _fields;
337355
return clone;
@@ -511,6 +529,18 @@ public virtual MongoCursor SetMax(BsonDocument max)
511529
return this;
512530
}
513531

532+
/// <summary>
533+
/// Sets the maximum await time for tailable await cursors.
534+
/// </summary>
535+
/// <param name="value">The value.</param>
536+
/// <returns>The cursor (so you can chain method calls to it).</returns>
537+
public virtual MongoCursor SetMaxAwaitTime(TimeSpan? value)
538+
{
539+
if (_isFrozen) { ThrowFrozen(); }
540+
_maxAwaitTime = value;
541+
return this;
542+
}
543+
514544
/// <summary>
515545
/// Sets the maximum number of documents to scan.
516546
/// </summary>
@@ -799,6 +829,7 @@ public virtual IEnumerator<TDocument> GetEnumerator()
799829
CursorType = cursorType,
800830
Filter = queryDocument,
801831
Limit = Limit,
832+
MaxAwaitTime = MaxAwaitTime,
802833
Modifiers = Options,
803834
NoCursorTimeout = noCursorTimeout,
804835
Projection = Fields.ToBsonDocument(),
@@ -894,6 +925,16 @@ public virtual IEnumerator<TDocument> GetEnumerator()
894925
return (MongoCursor<TDocument>)base.SetMax(max);
895926
}
896927

928+
/// <summary>
929+
/// Sets the maximum await time for tailable await cursors.
930+
/// </summary>
931+
/// <param name="value">The value.</param>
932+
/// <returns>The cursor (so you can chain method calls to it).</returns>
933+
public new virtual MongoCursor SetMaxAwaitTime(TimeSpan? value)
934+
{
935+
return (MongoCursor<TDocument>)base.SetMaxAwaitTime(value);
936+
}
937+
897938
/// <summary>
898939
/// Sets the maximum number of documents to scan.
899940
/// </summary>

src/MongoDB.Driver.Tests/IMongoCollectionExtensionsTests.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ public void Find_should_call_collection_FindAsync_with_correct_options()
122122
BatchSize = 20,
123123
Comment = "funny",
124124
CursorType = CursorType.TailableAwait,
125+
MaxAwaitTime = TimeSpan.FromSeconds(4),
125126
MaxTime = TimeSpan.FromSeconds(3),
126127
Modifiers = BsonDocument.Parse("{$snapshot: true}"),
127128
NoCursorTimeout = true,
@@ -149,6 +150,7 @@ public void Find_should_call_collection_FindAsync_with_correct_options()
149150
actualOptions.Comment.Should().Be(fluent.Options.Comment);
150151
actualOptions.CursorType.Should().Be(fluent.Options.CursorType);
151152
actualOptions.Limit.Should().Be(fluent.Options.Limit);
153+
actualOptions.MaxAwaitTime.Should().Be(fluent.Options.MaxAwaitTime);
152154
actualOptions.MaxTime.Should().Be(fluent.Options.MaxTime);
153155
actualOptions.Modifiers.Should().Be(fluent.Options.Modifiers);
154156
actualOptions.NoCursorTimeout.Should().Be(fluent.Options.NoCursorTimeout);
@@ -171,6 +173,7 @@ public void Find_with_an_expression_should_call_collection_FindAsync_with_correc
171173
BatchSize = 20,
172174
Comment = "funny",
173175
CursorType = CursorType.TailableAwait,
176+
MaxAwaitTime = TimeSpan.FromSeconds(4),
174177
MaxTime = TimeSpan.FromSeconds(3),
175178
Modifiers = BsonDocument.Parse("{$snapshot: true}"),
176179
NoCursorTimeout = true,
@@ -199,6 +202,7 @@ public void Find_with_an_expression_should_call_collection_FindAsync_with_correc
199202
actualOptions.Comment.Should().Be(fluent.Options.Comment);
200203
actualOptions.CursorType.Should().Be(fluent.Options.CursorType);
201204
actualOptions.Limit.Should().Be(fluent.Options.Limit);
205+
actualOptions.MaxAwaitTime.Should().Be(fluent.Options.MaxAwaitTime);
202206
actualOptions.MaxTime.Should().Be(fluent.Options.MaxTime);
203207
actualOptions.Modifiers.Should().Be(fluent.Options.Modifiers);
204208
actualOptions.NoCursorTimeout.Should().Be(fluent.Options.NoCursorTimeout);

src/MongoDB.Driver.Tests/MongoCollectionImplTests.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,7 @@ public async Task FindAsync_should_execute_the_FindOperation()
464464
Comment = "funny",
465465
CursorType = CursorType.TailableAwait,
466466
Limit = 30,
467+
MaxAwaitTime = TimeSpan.FromSeconds(4),
467468
MaxTime = TimeSpan.FromSeconds(3),
468469
Modifiers = BsonDocument.Parse("{$snapshot: true}"),
469470
NoCursorTimeout = true,
@@ -490,6 +491,7 @@ public async Task FindAsync_should_execute_the_FindOperation()
490491
operation.CursorType.Should().Be(MongoDB.Driver.Core.Operations.CursorType.TailableAwait);
491492
operation.Filter.Should().Be(filter);
492493
operation.Limit.Should().Be(options.Limit);
494+
operation.MaxAwaitTime.Should().Be(options.MaxAwaitTime);
493495
operation.MaxTime.Should().Be(options.MaxTime);
494496
operation.Modifiers.Should().Be(options.Modifiers);
495497
operation.NoCursorTimeout.Should().Be(options.NoCursorTimeout);
@@ -513,6 +515,7 @@ public async Task FindAsync_with_an_expression_should_execute_correctly()
513515
Comment = "funny",
514516
CursorType = CursorType.TailableAwait,
515517
Limit = 30,
518+
MaxAwaitTime = TimeSpan.FromSeconds(4),
516519
MaxTime = TimeSpan.FromSeconds(3),
517520
Modifiers = BsonDocument.Parse("{$snapshot: true}"),
518521
NoCursorTimeout = true,
@@ -539,6 +542,7 @@ public async Task FindAsync_with_an_expression_should_execute_correctly()
539542
operation.CursorType.Should().Be(MongoDB.Driver.Core.Operations.CursorType.TailableAwait);
540543
operation.Filter.Should().Be(new BsonDocument("x", 1));
541544
operation.Limit.Should().Be(options.Limit);
545+
operation.MaxAwaitTime.Should().Be(options.MaxAwaitTime);
542546
operation.MaxTime.Should().Be(options.MaxTime);
543547
operation.Modifiers.Should().Be(options.Modifiers);
544548
operation.NoCursorTimeout.Should().Be(options.NoCursorTimeout);

src/MongoDB.Driver/FindFluent.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ public override IFindFluent<TDocument, TNewProjection> Project<TNewProjection>(P
9090
Comment = _options.Comment,
9191
CursorType = _options.CursorType,
9292
Limit = _options.Limit,
93+
MaxAwaitTime = _options.MaxAwaitTime,
9394
MaxTime = _options.MaxTime,
9495
Modifiers = _options.Modifiers,
9596
NoCursorTimeout = _options.NoCursorTimeout,

src/MongoDB.Driver/FindOptions.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public abstract class FindOptionsBase
3333
private int? _batchSize;
3434
private string _comment;
3535
private CursorType _cursorType;
36+
private TimeSpan? _maxAwaitTime;
3637
private TimeSpan? _maxTime;
3738
private BsonDocument _modifiers;
3839
private bool? _noCursorTimeout;
@@ -84,6 +85,15 @@ public CursorType CursorType
8485
set { _cursorType = value; }
8586
}
8687

88+
/// <summary>
89+
/// Gets or sets the maximum await time for TailableAwait cursors.
90+
/// </summary>
91+
public TimeSpan? MaxAwaitTime
92+
{
93+
get { return _maxAwaitTime; }
94+
set { _maxAwaitTime = value; }
95+
}
96+
8797
/// <summary>
8898
/// Gets or sets the maximum time.
8999
/// </summary>

src/MongoDB.Driver/IMongoCollectionExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ public static IFindFluent<TDocument, TDocument> Find<TDocument>(this IMongoColle
212212
BatchSize = options.BatchSize,
213213
Comment = options.Comment,
214214
CursorType = options.CursorType,
215+
MaxAwaitTime = options.MaxAwaitTime,
215216
MaxTime = options.MaxTime,
216217
Modifiers = options.Modifiers,
217218
NoCursorTimeout = options.NoCursorTimeout,

src/MongoDB.Driver/MongoCollectionImpl.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ public override Task<IAsyncCursor<TProjection>> FindAsync<TProjection>(FilterDef
238238
CursorType = options.CursorType.ToCore(),
239239
Filter = filter.Render(_documentSerializer, _settings.SerializerRegistry),
240240
Limit = options.Limit,
241+
MaxAwaitTime = options.MaxAwaitTime,
241242
MaxTime = options.MaxTime,
242243
Modifiers = options.Modifiers,
243244
NoCursorTimeout = options.NoCursorTimeout,

0 commit comments

Comments
 (0)