-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathRegisterTests.cs
More file actions
158 lines (120 loc) · 6.29 KB
/
RegisterTests.cs
File metadata and controls
158 lines (120 loc) · 6.29 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Cleipnir.ResilientFunctions.Tests.Utils;
using Cleipnir.ResilientFunctions.Utils.Register;
using Shouldly;
namespace Cleipnir.ResilientFunctions.Tests.TestTemplates.UtilsTests;
public abstract class RegisterTests
{
public abstract Task SetValueWithNoExistingValueSucceeds();
protected async Task SetValueWithNoExistingValueSucceeds(Task<IRegister> registerTask)
{
var register = await registerTask;
var (group, key) = GetGroupAndKey();
await register.Exists(group, key).ShouldBeFalseAsync();
await register.SetIfEmpty(group, key, value: "hello world").ShouldBeTrueAsync();
var value = await register.Get(group, key);
value.ShouldBe("hello world");
await register.Exists(group, key).ShouldBeTrueAsync();
}
public abstract Task SetValueIfEmptyFailsWhenRegisterHasExistingValue();
protected async Task SetValueIfEmptyFailsWhenRegisterHasExistingValue(Task<IRegister> registerTask)
{
var register = await registerTask;
var (group, key) = GetGroupAndKey();
await register.SetIfEmpty(group, key, value: "hello world").ShouldBeTrueAsync();
await register.SetIfEmpty(group, key, value: "hello universe").ShouldBeFalseAsync();
var value = await register.Get(group, key);
value.ShouldBe("hello world");
}
public abstract Task CompareAndSwapWithNoExistingValueSucceeds();
protected async Task CompareAndSwapWithNoExistingValueSucceeds(Task<IRegister> registerTask)
{
var register = await registerTask;
var (group, key) = GetGroupAndKey();
await register.CompareAndSwap(group, key, newValue: "hello world", expectedValue: "", setIfEmpty: true).ShouldBeTrueAsync();
var value = await register.Get(group, key);
value.ShouldBe("hello world");
}
public abstract Task CompareAndSwapFailsWithNoExistingValue();
protected async Task CompareAndSwapFailsWithNoExistingValue(Task<IRegister> registerTask)
{
var register = await registerTask;
var (group, key) = GetGroupAndKey();
await register.CompareAndSwap(group, key, newValue: "hello world", expectedValue: "", setIfEmpty: false).ShouldBeFalseAsync();
await register.Exists(group, key).ShouldBeFalseAsync();
}
public abstract Task CompareAndSwapSucceedsIfAsExpected();
protected async Task CompareAndSwapSucceedsIfAsExpected(Task<IRegister> registerTask)
{
var register = await registerTask;
var (group, key) = GetGroupAndKey();
await register.SetIfEmpty(group, key, value: "hello world").ShouldBeTrueAsync();
await register.CompareAndSwap(group, key, newValue: "hello universe", expectedValue: "hello world");
await register.SetIfEmpty(group, key, value: "hello universe").ShouldBeFalseAsync();
var value = await register.Get(group, key);
value.ShouldBe("hello universe");
}
public abstract Task CompareAndSwapSucceedsIfAsExpectedIgnoreIfNoExisting();
protected async Task CompareAndSwapSucceedsIfAsExpectedIgnoreIfNoExisting(Task<IRegister> registerTask)
{
var register = await registerTask;
var (group, key) = GetGroupAndKey();
await register.SetIfEmpty(group, key, value: "hello world").ShouldBeTrueAsync();
await register.CompareAndSwap(group, key, newValue: "hello universe", expectedValue: "hello world", setIfEmpty: false);
await register.SetIfEmpty(group, key, value: "hello universe").ShouldBeFalseAsync();
var value = await register.Get(group, key);
value.ShouldBe("hello universe");
}
public abstract Task ExistsIfFalseForNonExistingRegister();
protected async Task ExistsIfFalseForNonExistingRegister(Task<IRegister> registerTask)
{
var register = await registerTask;
var (group, key) = GetGroupAndKey();
await register.Exists(group, key).ShouldBeFalseAsync();
}
public abstract Task ExistingValueIsNullForNonExistingRegister();
protected async Task ExistingValueIsNullForNonExistingRegister(Task<IRegister> registerTask)
{
var register = await registerTask;
var (group, key) = GetGroupAndKey();
await register.Get(group, key).ShouldBeNullAsync();
}
public abstract Task DeleteSucceedsForNonExistingRegister();
protected async Task DeleteSucceedsForNonExistingRegister(Task<IRegister> registerTask)
{
var register = await registerTask;
var (group, key) = GetGroupAndKey();
await register.Delete(group, key);
}
public abstract Task DeleteSucceedsForExistingRegister();
protected async Task DeleteSucceedsForExistingRegister(Task<IRegister> registerTask)
{
var register = await registerTask;
var (group, key) = GetGroupAndKey();
await register.SetIfEmpty(group, key, value: "hello world").ShouldBeTrueAsync();
await register.Delete(group, key);
await register.Exists(group, key).ShouldBeFalseAsync();
}
public abstract Task DeleteSucceedsWithExpectedValueForExistingRegister();
protected async Task DeleteSucceedsWithExpectedValueForExistingRegister(Task<IRegister> registerTask)
{
var register = await registerTask;
var (group, key) = GetGroupAndKey();
await register.SetIfEmpty(group, key, value: "hello world").ShouldBeTrueAsync();
await register.Delete(group, key, "hello world").ShouldBeTrueAsync();
await register.Exists(group, key).ShouldBeFalseAsync();
}
public abstract Task DeleteFailsWhenNonExpectedValueForExistingRegister();
protected async Task DeleteFailsWhenNonExpectedValueForExistingRegister(Task<IRegister> registerTask)
{
var register = await registerTask;
var (group, name) = GetGroupAndKey();
await register.SetIfEmpty(group, name, value: "hello world").ShouldBeTrueAsync();
await register.Delete(group, name, "hello universe").ShouldBeFalseAsync();
await register.Exists(group, name).ShouldBeTrueAsync();
}
private record GroupAndKey(string Group, string Key);
private static GroupAndKey GetGroupAndKey([CallerMemberName] string memberName = "") =>
new(Group: nameof(RegisterTests), Key: memberName);
}