forked from yevhen/Streamstone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
S08_Concurrency_conflicts.cs
86 lines (72 loc) · 2.89 KB
/
S08_Concurrency_conflicts.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using System;
using System.Threading.Tasks;
using Streamstone;
namespace Example.Scenarios
{
public class S08_Concurrency_conflicts : Scenario
{
public override async Task RunAsync()
{
await SimultaneousProvisioning();
await SimultaneousWriting();
await SimultaneousSettingOfStreamMetadata();
await SequentiallyWritingToStreamIgnoringReturnedStreamHeader();
}
async Task SimultaneousProvisioning()
{
await Stream.ProvisionAsync(Partition);
try
{
await Stream.ProvisionAsync(Partition);
}
catch (ConcurrencyConflictException)
{
Console.WriteLine("Simultaneously provisioning stream in a same partition will lead to ConcurrencyConflictException");
}
}
async Task SimultaneousWriting()
{
var a = await Stream.OpenAsync(Partition);
var b = await Stream.OpenAsync(Partition);
await Stream.WriteAsync(a, new EventData(EventId.From("123")));
try
{
await Stream.WriteAsync(b, new EventData(EventId.From("456")));
}
catch (ConcurrencyConflictException)
{
Console.WriteLine("Simultaneously writing to the same version of stream will lead to ConcurrencyConflictException");
}
}
async Task SimultaneousSettingOfStreamMetadata()
{
var a = await Stream.OpenAsync(Partition);
var b = await Stream.OpenAsync(Partition);
await Stream.SetPropertiesAsync(a, StreamProperties.From(new {A = 42}));
try
{
await Stream.SetPropertiesAsync(b, StreamProperties.From(new {A = 56}));
}
catch (ConcurrencyConflictException)
{
Console.WriteLine("Simultaneously setting metadata using the same version of stream will lead to ConcurrencyConflictException");
}
}
async Task SequentiallyWritingToStreamIgnoringReturnedStreamHeader()
{
var stream = await Stream.OpenAsync(Partition);
var result = await Stream.WriteAsync(stream, new EventData(EventId.From("AAA")));
// a new stream header is returned after each write, it contains new Etag
// and it should be used for subsequent operations
// stream = result.Stream;
try
{
await Stream.WriteAsync(stream, new EventData(EventId.From("BBB")));
}
catch (ConcurrencyConflictException)
{
Console.WriteLine("Ignoring new stream (header) returned after each Write() operation will lead to ConcurrencyConflictException on subsequent write operation");
}
}
}
}