Skip to content

Commit 4e1cf75

Browse files
committed
[DEVEX-222] Made old Read methods obsolete and used the new ones in tests
1 parent 1a8e507 commit 4e1cf75

16 files changed

+472
-231
lines changed

src/KurrentDB.Client/Streams/KurrentDBClient.Append.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public Task<IWriteResult> AppendToStreamAsync(
3333
CancellationToken cancellationToken = default
3434
) {
3535
var messageSerializationContext = new MessageSerializationContext(FromStreamName(streamName));
36-
36+
3737
var messageData = _messageSerializer.With(options?.SerializationSettings)
3838
.Serialize(messages, messageSerializationContext);
3939

@@ -507,10 +507,11 @@ public class AppendToStreamOptions : OperationOptions {
507507
/// </summary>
508508
/// <returns></returns>
509509
public void With(KurrentDBClientOperationOptions clientOperationOptions) {
510-
ThrowOnAppendFailure = clientOperationOptions.ThrowOnAppendFailure;
511-
BatchAppendSize = clientOperationOptions.BatchAppendSize;
510+
ThrowOnAppendFailure ??= clientOperationOptions.ThrowOnAppendFailure;
511+
512+
BatchAppendSize ??= clientOperationOptions.BatchAppendSize;
512513
}
513-
514+
514515
/// <summary>
515516
/// Allows to customize or disable the automatic deserialization
516517
/// </summary>

src/KurrentDB.Client/Streams/KurrentDBClient.Read.cs

+124-1
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,64 @@ public class ReadAllOptions : OperationOptions {
471471
/// Allows to customize or disable the automatic deserialization
472472
/// </summary>
473473
public OperationSerializationSettings? SerializationSettings { get; set; }
474+
475+
public static ReadAllOptions Get() =>
476+
new ReadAllOptions();
477+
478+
public ReadAllOptions Forwards() {
479+
Direction = Direction.Forwards;
480+
481+
return this;
482+
}
483+
484+
public ReadAllOptions WithFilter(IEventFilter filter) {
485+
Filter = filter;
486+
487+
return this;
488+
}
489+
490+
public ReadAllOptions Backwards() {
491+
Direction = Direction.Backwards;
492+
493+
return this;
494+
}
495+
496+
public ReadAllOptions From(Position streamPosition) {
497+
Position = streamPosition;
498+
499+
return this;
500+
}
501+
502+
public ReadAllOptions FromStart() =>
503+
From(Position.Start);
504+
505+
public ReadAllOptions FromEnd() =>
506+
From(Position.End);
507+
508+
public ReadAllOptions WithMaxCount(long maxCount) {
509+
MaxCount = maxCount;
510+
511+
return this;
512+
}
513+
514+
public ReadAllOptions MaxOne() =>
515+
WithMaxCount(1);
516+
517+
public ReadAllOptions First() =>
518+
FromStart()
519+
.Forwards()
520+
.MaxOne();
521+
522+
public ReadAllOptions Last() =>
523+
FromEnd()
524+
.Backwards()
525+
.MaxOne();
526+
527+
public ReadAllOptions DisableAutoSerialization() {
528+
SerializationSettings = OperationSerializationSettings.Disabled;
529+
530+
return this;
531+
}
474532
}
475533

476534
/// <summary>
@@ -502,9 +560,62 @@ public class ReadStreamOptions : OperationOptions {
502560
/// Allows to customize or disable the automatic deserialization
503561
/// </summary>
504562
public OperationSerializationSettings? SerializationSettings { get; set; }
563+
564+
public static ReadStreamOptions Get() =>
565+
new ReadStreamOptions();
566+
567+
public ReadStreamOptions Forwards() {
568+
Direction = Direction.Forwards;
569+
570+
return this;
571+
}
572+
573+
public ReadStreamOptions Backwards() {
574+
Direction = Direction.Backwards;
575+
576+
return this;
577+
}
578+
579+
public ReadStreamOptions From(StreamPosition streamPosition) {
580+
StreamPosition = streamPosition;
581+
582+
return this;
583+
}
584+
585+
public ReadStreamOptions FromStart() =>
586+
From(StreamPosition.Start);
587+
588+
public ReadStreamOptions FromEnd() =>
589+
From(StreamPosition.End);
590+
591+
public ReadStreamOptions WithMaxCount(long maxCount) {
592+
MaxCount = maxCount;
593+
594+
return this;
595+
}
596+
597+
public ReadStreamOptions MaxOne() =>
598+
WithMaxCount(1);
599+
600+
public ReadStreamOptions First() =>
601+
FromStart()
602+
.Forwards()
603+
.MaxOne();
604+
605+
public ReadStreamOptions Last() =>
606+
FromEnd()
607+
.Backwards()
608+
.MaxOne();
609+
610+
public ReadStreamOptions DisableAutoSerialization() {
611+
SerializationSettings = OperationSerializationSettings.Disabled;
612+
613+
return this;
614+
}
505615
}
506616

507-
public static class KurrentDBClientReadExtensions {
617+
[Obsolete("Those extensions may be removed in the future versions", false)]
618+
public static class ObsoleteKurrentDBClientReadExtensions {
508619
/// <summary>
509620
/// Asynchronously reads all events.
510621
/// </summary>
@@ -517,6 +628,10 @@ public static class KurrentDBClientReadExtensions {
517628
/// <param name="userCredentials">The optional <see cref="UserCredentials"/> to perform operation with.</param>
518629
/// <param name="cancellationToken">The optional <see cref="System.Threading.CancellationToken"/>.</param>
519630
/// <returns></returns>
631+
[Obsolete(
632+
"This method may be removed in future releases. Use the overload with ReadAllOptions and get auto-serialization capabilities",
633+
false
634+
)]
520635
public static KurrentDBClient.ReadAllStreamResult ReadAllAsync(
521636
this KurrentDBClient dbClient,
522637
Direction direction,
@@ -554,6 +669,10 @@ public static KurrentDBClient.ReadAllStreamResult ReadAllAsync(
554669
/// <param name="userCredentials">The optional <see cref="UserCredentials"/> to perform operation with.</param>
555670
/// <param name="cancellationToken">The optional <see cref="System.Threading.CancellationToken"/>.</param>
556671
/// <returns></returns>
672+
[Obsolete(
673+
"This method may be removed in future releases. Use the overload with ReadAllOptions and get auto-serialization capabilities",
674+
false
675+
)]
557676
public static KurrentDBClient.ReadAllStreamResult ReadAllAsync(
558677
this KurrentDBClient dbClient,
559678
Direction direction,
@@ -598,6 +717,10 @@ public static KurrentDBClient.ReadAllStreamResult ReadAllAsync(
598717
/// <param name="userCredentials">The optional <see cref="UserCredentials"/> to perform operation with.</param>
599718
/// <param name="cancellationToken">The optional <see cref="System.Threading.CancellationToken"/>.</param>
600719
/// <returns></returns>
720+
[Obsolete(
721+
"This method may be removed in future releases. Use the overload with ReadStreamOptions and get auto-serialization capabilities",
722+
false
723+
)]
601724
public static KurrentDBClient.ReadStreamResult ReadStreamAsync(
602725
this KurrentDBClient dbClient,
603726
Direction direction,

test/KurrentDB.Client.Tests.Common/Extensions/KurrentDBClientWarmupExtensions.cs

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using Grpc.Core;
2-
using KurrentDB.Client;
32
using Polly;
43
using Polly.Contrib.WaitAndRetry;
54
using static System.TimeSpan;
@@ -52,11 +51,11 @@ public static Task<KurrentDBClient> WarmUp(
5251
// 2. we are connected to leader if we require it
5352
var users = await dbClient
5453
.ReadStreamAsync(
55-
direction: Direction.Forwards,
56-
streamName: "$dbUsers",
57-
revision: StreamPosition.Start,
58-
maxCount: 1,
59-
userCredentials: TestCredentials.Root,
54+
"$dbUsers",
55+
new ReadStreamOptions {
56+
MaxCount = 1,
57+
UserCredentials = TestCredentials.Root
58+
},
6059
cancellationToken: ct
6160
)
6261
.ToArrayAsync(ct);

test/KurrentDB.Client.Tests/PersistentSubscriptions/SubscribeToAll/Obsolete/SubscribeToAllObsoleteTests.cs

+28-7
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ public async Task connect_to_existing_with_max_one_client() {
1010
// Arrange
1111
var group = Fixture.GetGroupName();
1212

13-
await Fixture.Subscriptions.CreateToAllAsync(group, new(maxSubscriberCount: 1), userCredentials: TestCredentials.Root);
13+
await Fixture.Subscriptions.CreateToAllAsync(
14+
group,
15+
new(maxSubscriberCount: 1),
16+
userCredentials: TestCredentials.Root
17+
);
1418

1519
using var first = await Fixture.Subscriptions.SubscribeToAllAsync(
1620
group,
@@ -67,10 +71,14 @@ await Fixture.Streams.AppendToStreamAsync(
6771
}
6872

6973
var events = await Fixture.Streams
70-
.ReadAllAsync(Direction.Forwards, Position.Start, 10, userCredentials: TestCredentials.Root)
74+
.ReadAllAsync(new ReadAllOptions { MaxCount = 10, UserCredentials = TestCredentials.Root })
7175
.ToArrayAsync();
7276

73-
await Fixture.Subscriptions.CreateToAllAsync(group, new(startFrom: Position.Start), userCredentials: TestCredentials.Root);
77+
await Fixture.Subscriptions.CreateToAllAsync(
78+
group,
79+
new(startFrom: Position.Start),
80+
userCredentials: TestCredentials.Root
81+
);
7482

7583
using var subscription = await Fixture.Subscriptions.SubscribeToAllAsync(
7684
group,
@@ -150,7 +158,12 @@ await Fixture.Streams.AppendToStreamAsync(
150158
);
151159
}
152160

153-
await Fixture.Subscriptions.CreateToAllAsync(group, new(startFrom: Position.End), userCredentials: TestCredentials.Root);
161+
await Fixture.Subscriptions.CreateToAllAsync(
162+
group,
163+
new(startFrom: Position.End),
164+
userCredentials: TestCredentials.Root
165+
);
166+
154167
using var subscription = await Fixture.Subscriptions.SubscribeToAllAsync(
155168
group,
156169
async (subscription, e, r, ct) => {
@@ -208,7 +221,7 @@ public async Task connect_to_existing_with_start_from_set_to_valid_middle_positi
208221
TaskCompletionSource<ResolvedEvent> firstEventSource = new();
209222

210223
var events = await Fixture.Streams
211-
.ReadAllAsync(Direction.Forwards, Position.Start, 10, userCredentials: TestCredentials.Root)
224+
.ReadAllAsync(new ReadAllOptions { MaxCount = 10, UserCredentials = TestCredentials.Root })
212225
.ToArrayAsync();
213226

214227
var expectedEvent = events[events.Length / 2]; //just a random event in the middle of the results
@@ -245,7 +258,11 @@ public async Task connect_with_retries() {
245258

246259
TaskCompletionSource<int> retryCountSource = new();
247260

248-
await Fixture.Subscriptions.CreateToAllAsync(group, new(startFrom: Position.Start), userCredentials: TestCredentials.Root);
261+
await Fixture.Subscriptions.CreateToAllAsync(
262+
group,
263+
new(startFrom: Position.Start),
264+
userCredentials: TestCredentials.Root
265+
);
249266

250267
// Act
251268
using var subscription = await Fixture.Subscriptions.SubscribeToAllAsync(
@@ -613,7 +630,11 @@ public async Task update_existing_with_subscribers() {
613630

614631
TaskCompletionSource<(SubscriptionDroppedReason, Exception?)> droppedSource = new();
615632

616-
await Fixture.Subscriptions.CreateToAllAsync(group, new(startFrom: Position.Start), userCredentials: TestCredentials.Root);
633+
await Fixture.Subscriptions.CreateToAllAsync(
634+
group,
635+
new(startFrom: Position.Start),
636+
userCredentials: TestCredentials.Root
637+
);
617638

618639
using var subscription = Fixture.Subscriptions.SubscribeToAllAsync(
619640
group,

0 commit comments

Comments
 (0)