forked from OrleansContrib/Orleans.Indexing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IndexingTestUtils.cs
268 lines (220 loc) · 21.2 KB
/
IndexingTestUtils.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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
using System;
using System.Linq;
using System.Threading.Tasks;
using Xunit;
using Orleans.Indexing.Tests.MultiInterface;
using Orleans.Indexing.Facet;
using System.Reflection;
namespace Orleans.Indexing.Tests
{
public static class IndexingTestUtils
{
public static async Task<int> CountItemsStreamingIn<TIGrain, TProperties, TQueryProp>(this IndexingTestRunnerBase runner,
Func<IndexingTestRunnerBase, TQueryProp, Tuple<IOrleansQueryable<TIGrain, TProperties>, Func<TIGrain, Task<TQueryProp>>>> queryTupleFunc,
string propertyName, TQueryProp queryValue, int delayInMilliseconds = 0)
where TIGrain : IIndexableGrain
{
if (delayInMilliseconds > 0)
{
await Task.Delay(delayInMilliseconds);
}
var taskCompletionSource = new TaskCompletionSource<int>();
var queryTuple = queryTupleFunc(runner, queryValue);
var queryItems = queryTuple.Item1;
var queryPropAsync = queryTuple.Item2;
int counter = 0;
await queryItems.ObserveResults(new QueryResultStreamObserver<TIGrain>(/*async*/ entry =>
{
counter++;
// For Total indexes, the grain may not be active; querying the property will activate it. If we have a mix of Active and Total
// indexes on a grain, this will cause the Active counts to be incorrect during testing. TODO: specify per-test whether to retrieve this
var isActiveIndex = runner.IndexFactory.GetIndex(typeof(TIGrain), IndexUtils.PropertyNameToIndexName(propertyName)).IsActiveIndex();
var propertyValue = /* isActiveIndex ? (await queryPropAsync(entry)).ToString() : */ "[not retrieved]";
runner.Output.WriteLine($"grain id = {entry}, {propertyName} = {propertyValue}, primary key = {entry.GetPrimaryKeyLong()}");
return Task.CompletedTask;
}, () =>
{
taskCompletionSource.SetResult(counter);
return Task.CompletedTask;
}));
int observedCount = await taskCompletionSource.Task;
Assert.Equal(observedCount, (await queryItems.GetResults()).Count());
return observedCount;
}
internal static async Task SetPropertyAndWriteStateAsync<TGrainState>(Action<TGrainState> setterAction, IIndexedState<TGrainState> indexedState, bool retry)
where TGrainState: class, new()
{
const int MaxRetries = 10;
int retries = 0;
while (true)
{
try
{
await indexedState.PerformUpdate(setterAction);
return;
}
catch (Exception) when (retry && retries < MaxRetries)
{
++retries;
await indexedState.PerformRead();
}
}
}
private static IOrleansQueryable<TIGrain, TProperties> QueryActiveGrains<TIGrain, TProperties>(IndexingTestRunnerBase runner)
where TIGrain : IIndexableGrain
=> runner.IndexFactory.GetActiveGrains<TIGrain, TProperties>();
#region PlayerGrain
internal static Tuple<IOrleansQueryable<TIGrain, TProperties>, Func<TIGrain, Task<string>>> QueryByPlayerLocation<TIGrain, TProperties>(
this IndexingTestRunnerBase runner, string queryValue)
where TIGrain : IPlayerGrain, IIndexableGrain where TProperties : IPlayerProperties
=> Tuple.Create<IOrleansQueryable<TIGrain, TProperties>, Func<TIGrain, Task<string>>>(
from item in QueryActiveGrains<TIGrain, TProperties>(runner) where item.Location == queryValue select item,
entry => entry.GetLocation());
internal static Tuple<IOrleansQueryable<TIGrain, TProperties>, Func<TIGrain, Task<string>>> QueryByPlayerLocationTxn<TIGrain, TProperties>(
this IndexingTestRunnerBase runner, string queryValue)
where TIGrain : IPlayerGrainTransactional, IIndexableGrain where TProperties : IPlayerProperties
=> Tuple.Create<IOrleansQueryable<TIGrain, TProperties>, Func<TIGrain, Task<string>>>(
from item in QueryActiveGrains<TIGrain, TProperties>(runner) where item.Location == queryValue select item,
entry => entry.GetLocation());
internal static Tuple<IOrleansQueryable<TIGrain, TProperties>, Func<TIGrain, Task<int>>> QueryByPlayerScore<TIGrain, TProperties>(
this IndexingTestRunnerBase runner, int queryValue)
where TIGrain : IPlayerGrain, IIndexableGrain where TProperties : IPlayerProperties
=> Tuple.Create<IOrleansQueryable<TIGrain, TProperties>, Func<TIGrain, Task<int>>>(
from item in QueryActiveGrains<TIGrain, TProperties>(runner) where item.Score == queryValue select item,
entry => entry.GetScore());
internal static Tuple<IOrleansQueryable<TIGrain, TProperties>, Func<TIGrain, Task<int>>> QueryByPlayerScoreTxn<TIGrain, TProperties>(
this IndexingTestRunnerBase runner, int queryValue)
where TIGrain : IPlayerGrainTransactional, IIndexableGrain where TProperties : IPlayerProperties
=> Tuple.Create<IOrleansQueryable<TIGrain, TProperties>, Func<TIGrain, Task<int>>>(
from item in QueryActiveGrains<TIGrain, TProperties>(runner) where item.Score == queryValue select item,
entry => entry.GetScore());
internal static Task<int> GetPlayerLocationCount<TIGrain, TProperties>(this IndexingTestRunnerBase runner, string location, int delayInMilliseconds = 0)
where TIGrain : IPlayerGrain, IIndexableGrain where TProperties : IPlayerProperties
=> runner.CountItemsStreamingIn((r, v) => r.QueryByPlayerLocation<TIGrain, TProperties>(v), nameof(IPlayerProperties.Location), location, delayInMilliseconds);
internal static Task<int> GetPlayerLocationCountTxn<TIGrain, TProperties>(this IndexingTestRunnerBase runner, string location, int delayInMilliseconds = 0)
where TIGrain : IPlayerGrainTransactional, IIndexableGrain where TProperties : IPlayerProperties
=> runner.CountItemsStreamingIn((r, v) => r.QueryByPlayerLocationTxn<TIGrain, TProperties>(v), nameof(IPlayerProperties.Location), location, delayInMilliseconds);
internal static Task<int> GetPlayerScoreCount<TIGrain, TProperties>(this IndexingTestRunnerBase runner, int score, int delayInMilliseconds = 0)
where TIGrain : IPlayerGrain, IIndexableGrain where TProperties : IPlayerProperties
=> runner.CountItemsStreamingIn((r, v) => r.QueryByPlayerScore<TIGrain, TProperties>(v), nameof(IPlayerProperties.Score), score, delayInMilliseconds);
internal static Task<int> GetPlayerScoreCountTxn<TIGrain, TProperties>(this IndexingTestRunnerBase runner, int score, int delayInMilliseconds = 0)
where TIGrain : IPlayerGrainTransactional, IIndexableGrain where TProperties : IPlayerProperties
=> runner.CountItemsStreamingIn((r, v) => r.QueryByPlayerScoreTxn<TIGrain, TProperties>(v), nameof(IPlayerProperties.Score), score, delayInMilliseconds);
#endregion PlayerGrain
#region MultiIndex
internal static Tuple<IOrleansQueryable<TIGrain, TProperties>, Func<TIGrain, Task<int>>> QueryByUniqueInt<TIGrain, TProperties>(this IndexingTestRunnerBase runner, int queryValue)
where TIGrain : ITestMultiIndexGrain, IIndexableGrain where TProperties : ITestMultiIndexProperties
=> Tuple.Create<IOrleansQueryable<TIGrain, TProperties>, Func<TIGrain, Task<int>>>(
from item in QueryActiveGrains<TIGrain, TProperties>(runner) where item.UniqueInt == queryValue select item,
entry => entry.GetUniqueInt());
internal static Tuple<IOrleansQueryable<TIGrain, TProperties>, Func<TIGrain, Task<int>>> QueryByUniqueIntTxn<TIGrain, TProperties>(this IndexingTestRunnerBase runner, int queryValue)
where TIGrain : ITestMultiIndexGrainTransactional, IIndexableGrain where TProperties : ITestMultiIndexProperties
=> Tuple.Create<IOrleansQueryable<TIGrain, TProperties>, Func<TIGrain, Task<int>>>(
from item in QueryActiveGrains<TIGrain, TProperties>(runner) where item.UniqueInt == queryValue select item,
entry => entry.GetUniqueInt());
internal static Tuple<IOrleansQueryable<TIGrain, TProperties>, Func<TIGrain, Task<string>>> QueryByUniqueString<TIGrain, TProperties>(this IndexingTestRunnerBase runner, string queryValue)
where TIGrain : ITestMultiIndexGrain, IIndexableGrain where TProperties : ITestMultiIndexProperties
=> Tuple.Create<IOrleansQueryable<TIGrain, TProperties>, Func<TIGrain, Task<string>>>(
from item in QueryActiveGrains<TIGrain, TProperties>(runner) where item.UniqueString == queryValue select item,
entry => entry.GetUniqueString());
internal static Tuple<IOrleansQueryable<TIGrain, TProperties>, Func<TIGrain, Task<string>>> QueryByUniqueStringTxn<TIGrain, TProperties>(this IndexingTestRunnerBase runner, string queryValue)
where TIGrain : ITestMultiIndexGrainTransactional, IIndexableGrain where TProperties : ITestMultiIndexProperties
=> Tuple.Create<IOrleansQueryable<TIGrain, TProperties>, Func<TIGrain, Task<string>>>(
from item in QueryActiveGrains<TIGrain, TProperties>(runner) where item.UniqueString == queryValue select item,
entry => entry.GetUniqueString());
// Note: QueryByNonUnique* reverses the order of the comparison so that both variations are tested.
internal static Tuple<IOrleansQueryable<TIGrain, TProperties>, Func<TIGrain, Task<int>>> QueryByNonUniqueInt<TIGrain, TProperties>(this IndexingTestRunnerBase runner, int queryValue)
where TIGrain : ITestMultiIndexGrain, IIndexableGrain where TProperties : ITestMultiIndexProperties
=> Tuple.Create<IOrleansQueryable<TIGrain, TProperties>, Func<TIGrain, Task<int>>>(
from item in QueryActiveGrains<TIGrain, TProperties>(runner) where queryValue == item.NonUniqueInt select item,
entry => entry.GetNonUniqueInt());
internal static Tuple<IOrleansQueryable<TIGrain, TProperties>, Func<TIGrain, Task<int>>> QueryByNonUniqueIntTxn<TIGrain, TProperties>(this IndexingTestRunnerBase runner, int queryValue)
where TIGrain : ITestMultiIndexGrainTransactional, IIndexableGrain where TProperties : ITestMultiIndexProperties
=> Tuple.Create<IOrleansQueryable<TIGrain, TProperties>, Func<TIGrain, Task<int>>>(
from item in QueryActiveGrains<TIGrain, TProperties>(runner) where queryValue == item.NonUniqueInt select item,
entry => entry.GetNonUniqueInt());
internal static Tuple<IOrleansQueryable<TIGrain, TProperties>, Func<TIGrain, Task<string>>> QueryByNonUniqueString<TIGrain, TProperties>(this IndexingTestRunnerBase runner, string queryValue)
where TIGrain : ITestMultiIndexGrain, IIndexableGrain where TProperties : ITestMultiIndexProperties
=> Tuple.Create<IOrleansQueryable<TIGrain, TProperties>, Func<TIGrain, Task<string>>>(
from item in QueryActiveGrains<TIGrain, TProperties>(runner) where queryValue == item.NonUniqueString select item,
entry => entry.GetNonUniqueString());
internal static Tuple<IOrleansQueryable<TIGrain, TProperties>, Func<TIGrain, Task<string>>> QueryByNonUniqueStringTxn<TIGrain, TProperties>(this IndexingTestRunnerBase runner, string queryValue)
where TIGrain : ITestMultiIndexGrainTransactional, IIndexableGrain where TProperties : ITestMultiIndexProperties
=> Tuple.Create<IOrleansQueryable<TIGrain, TProperties>, Func<TIGrain, Task<string>>>(
from item in QueryActiveGrains<TIGrain, TProperties>(runner) where queryValue == item.NonUniqueString select item,
entry => entry.GetNonUniqueString());
internal static Task<int> GetUniqueIntCount<TIGrain, TProperties>(this IndexingTestRunnerBase runner, int uniqueValue, int delayInMilliseconds = 0)
where TIGrain : ITestMultiIndexGrain, IIndexableGrain where TProperties : ITestMultiIndexProperties
=> runner.CountItemsStreamingIn((r, v) => r.QueryByUniqueInt<TIGrain, TProperties>(v), nameof(ITestMultiIndexProperties.UniqueInt), uniqueValue, delayInMilliseconds);
internal static Task<int> GetUniqueIntCountTxn<TIGrain, TProperties>(this IndexingTestRunnerBase runner, int uniqueValue, int delayInMilliseconds = 0)
where TIGrain : ITestMultiIndexGrainTransactional, IIndexableGrain where TProperties : ITestMultiIndexProperties
=> runner.CountItemsStreamingIn((r, v) => r.QueryByUniqueIntTxn<TIGrain, TProperties>(v), nameof(ITestMultiIndexProperties.UniqueInt), uniqueValue, delayInMilliseconds);
internal static Task<int> GetUniqueStringCount<TIGrain, TProperties>(this IndexingTestRunnerBase runner, string uniqueValue, int delayInMilliseconds = 0)
where TIGrain : ITestMultiIndexGrain, IIndexableGrain where TProperties : ITestMultiIndexProperties
=> runner.CountItemsStreamingIn((r, v) => r.QueryByUniqueString<TIGrain, TProperties>(v), nameof(ITestMultiIndexProperties.UniqueString), uniqueValue, delayInMilliseconds);
internal static Task<int> GetUniqueStringCountTxn<TIGrain, TProperties>(this IndexingTestRunnerBase runner, string uniqueValue, int delayInMilliseconds = 0)
where TIGrain : ITestMultiIndexGrainTransactional, IIndexableGrain where TProperties : ITestMultiIndexProperties
=> runner.CountItemsStreamingIn((r, v) => r.QueryByUniqueStringTxn<TIGrain, TProperties>(v), nameof(ITestMultiIndexProperties.UniqueString), uniqueValue, delayInMilliseconds);
internal static Task<int> GetNonUniqueIntCount<TIGrain, TProperties>(this IndexingTestRunnerBase runner, int nonUniqueValue, int delayInMilliseconds = 0)
where TIGrain : ITestMultiIndexGrain, IIndexableGrain where TProperties : ITestMultiIndexProperties
=> runner.CountItemsStreamingIn((r, v) => r.QueryByNonUniqueInt<TIGrain, TProperties>(v), nameof(ITestMultiIndexProperties.NonUniqueInt), nonUniqueValue, delayInMilliseconds);
internal static Task<int> GetNonUniqueIntCountTxn<TIGrain, TProperties>(this IndexingTestRunnerBase runner, int nonUniqueValue, int delayInMilliseconds = 0)
where TIGrain : ITestMultiIndexGrainTransactional, IIndexableGrain where TProperties : ITestMultiIndexProperties
=> runner.CountItemsStreamingIn((r, v) => r.QueryByNonUniqueIntTxn<TIGrain, TProperties>(v), nameof(ITestMultiIndexProperties.NonUniqueInt), nonUniqueValue, delayInMilliseconds);
internal static Task<int> GetNonUniqueStringCount<TIGrain, TProperties>(this IndexingTestRunnerBase runner, string nonUniqueValue, int delayInMilliseconds = 0)
where TIGrain : ITestMultiIndexGrain, IIndexableGrain where TProperties : ITestMultiIndexProperties
=> runner.CountItemsStreamingIn((r, v) => r.QueryByNonUniqueString<TIGrain, TProperties>(v), nameof(ITestMultiIndexProperties.NonUniqueString), nonUniqueValue, delayInMilliseconds);
internal static Task<int> GetNonUniqueStringCountTxn<TIGrain, TProperties>(this IndexingTestRunnerBase runner, string nonUniqueValue, int delayInMilliseconds = 0)
where TIGrain : ITestMultiIndexGrainTransactional, IIndexableGrain where TProperties : ITestMultiIndexProperties
=> runner.CountItemsStreamingIn((r, v) => r.QueryByNonUniqueStringTxn<TIGrain, TProperties>(v), nameof(ITestMultiIndexProperties.NonUniqueString), nonUniqueValue, delayInMilliseconds);
#endregion MultiIndex
#region MultiInterface
internal static Tuple<IOrleansQueryable<TIGrain, TProperties>, Func<TIGrain, Task<string>>> QueryByPersonName<TIGrain, TProperties>(
this IndexingTestRunnerBase runner, string queryValue)
where TIGrain : IPersonGrain, IIndexableGrain where TProperties : IPersonProperties
=> Tuple.Create<IOrleansQueryable<TIGrain, TProperties>, Func<TIGrain, Task<string>>>(
from item in QueryActiveGrains<TIGrain, TProperties>(runner) where item.Name == queryValue select item,
entry => entry.GetName());
internal static Tuple<IOrleansQueryable<TIGrain, TProperties>, Func<TIGrain, Task<int>>> QueryByPersonAge<TIGrain, TProperties>(
this IndexingTestRunnerBase runner, int queryValue)
where TIGrain : IPersonGrain, IIndexableGrain where TProperties : IPersonProperties
=> Tuple.Create<IOrleansQueryable<TIGrain, TProperties>, Func<TIGrain, Task<int>>>(
from item in QueryActiveGrains<TIGrain, TProperties>(runner) where item.Age == queryValue select item,
entry => entry.GetAge());
// Note: Queries for Job and Employee reverse the order of the comparison so that both variations are tested.
internal static Tuple<IOrleansQueryable<TIGrain, TProperties>, Func<TIGrain, Task<string>>> QueryByJobTitle<TIGrain, TProperties>(
this IndexingTestRunnerBase runner, string queryValue)
where TIGrain : IJobGrain, IIndexableGrain where TProperties : IJobProperties
=> Tuple.Create<IOrleansQueryable<TIGrain, TProperties>, Func<TIGrain, Task<string>>>(
from item in QueryActiveGrains<TIGrain, TProperties>(runner) where queryValue == item.Title select item,
entry => entry.GetTitle());
internal static Tuple<IOrleansQueryable<TIGrain, TProperties>, Func<TIGrain, Task<string>>> QueryByJobDepartment<TIGrain, TProperties>(
this IndexingTestRunnerBase runner, string queryValue)
where TIGrain : IJobGrain, IIndexableGrain where TProperties : IJobProperties
=> Tuple.Create<IOrleansQueryable<TIGrain, TProperties>, Func<TIGrain, Task<string>>>(
from item in QueryActiveGrains<TIGrain, TProperties>(runner) where queryValue == item.Department select item,
entry => entry.GetDepartment());
internal static Tuple<IOrleansQueryable<TIGrain, TProperties>, Func<TIGrain, Task<int>>> QueryByEmployeeId<TIGrain, TProperties>(
this IndexingTestRunnerBase runner, int queryValue)
where TIGrain : IEmployeeGrain, IIndexableGrain where TProperties : IEmployeeProperties
=> Tuple.Create<IOrleansQueryable<TIGrain, TProperties>, Func<TIGrain, Task<int>>>(
from item in QueryActiveGrains<TIGrain, TProperties>(runner) where item.EmployeeId == queryValue select item,
entry => entry.GetEmployeeId());
internal static Task<int> GetNameCount<TIGrain, TProperties>(this IndexingTestRunnerBase runner, string name, int delayInMilliseconds = 0)
where TIGrain : IPersonGrain, IIndexableGrain where TProperties : IPersonProperties
=> runner.CountItemsStreamingIn((r, v) => r.QueryByPersonName<TIGrain, TProperties>(v), nameof(IPersonProperties.Name), name, delayInMilliseconds);
internal static Task<int> GetPersonAgeCount<TIGrain, TProperties>(this IndexingTestRunnerBase runner, int age, int delayInMilliseconds = 0)
where TIGrain : IPersonGrain, IIndexableGrain where TProperties : IPersonProperties
=> runner.CountItemsStreamingIn((r, v) => r.QueryByPersonAge<TIGrain, TProperties>(v), nameof(IPersonProperties.Age), age, delayInMilliseconds);
internal static Task<int> GetJobTitleCount<TIGrain, TProperties>(this IndexingTestRunnerBase runner, string title, int delayInMilliseconds = 0)
where TIGrain : IJobGrain, IIndexableGrain where TProperties : IJobProperties
=> runner.CountItemsStreamingIn((r, v) => r.QueryByJobTitle<TIGrain, TProperties>(v), nameof(IJobProperties.Title), title, delayInMilliseconds);
internal static Task<int> GetJobDepartmentCount<TIGrain, TProperties>(this IndexingTestRunnerBase runner, string department, int delayInMilliseconds = 0)
where TIGrain : IJobGrain, IIndexableGrain where TProperties : IJobProperties
=> runner.CountItemsStreamingIn((r, v) => r.QueryByJobDepartment<TIGrain, TProperties>(v), nameof(IJobProperties.Department), department, delayInMilliseconds);
internal static Task<int> GetEmployeeIdCount<TIGrain, TProperties>(this IndexingTestRunnerBase runner, int id, int delayInMilliseconds = 0)
where TIGrain : IEmployeeGrain, IIndexableGrain where TProperties : IEmployeeProperties
=> runner.CountItemsStreamingIn((r, v) => r.QueryByEmployeeId<TIGrain, TProperties>(v), nameof(IEmployeeProperties.EmployeeId), id, delayInMilliseconds);
#endregion MultiInterface
}
}