Skip to content

Commit fc1bf12

Browse files
authored
StackExchange.Redis Sync tests (#162)
* test async methods * add batch implementation * fix field * look for operations * fix integrations, use a dictionary for test * add sync methods and test
1 parent 3a968c4 commit fc1bf12

File tree

2 files changed

+297
-39
lines changed

2 files changed

+297
-39
lines changed

samples/Samples.RedisCore/Program.cs

+136-5
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ private static void RunStackExchange(string prefix)
8585
{ "TIME", () => db.Execute("TIME") },
8686
});
8787

88+
RunCommands(StackExchangeSyncCommands(prefix + "Database.", db));
8889

8990
var batch = db.CreateBatch();
9091
var batchPending = RunStackExchangeAsync(prefix + "Batch.", batch);
@@ -115,6 +116,125 @@ private static void RunStackExchange(string prefix)
115116
}
116117
}
117118

119+
private static TupleList<string, Func<object>> StackExchangeSyncCommands(string prefix, IDatabase db)
120+
{
121+
return new TupleList<string, Func<object>>()
122+
{
123+
{ "DebugObject", () => db.DebugObject($"{prefix}DebugObject") },
124+
{ "Execute", () => db.Execute("DDCUSTOM", "COMMAND") },
125+
126+
{ "GeoAdd", () => db.GeoAdd($"{prefix}Geo", new GeoEntry(1.5, 2.5, "member")) },
127+
{ "GeoDistance", () => db.GeoDistance($"{prefix}Geo", "member1", "member2") },
128+
{ "GeoHash", () => db.GeoHash($"{prefix}Geo", "member") },
129+
{ "GeoPosition", () => db.GeoPosition($"{prefix}Geo", "member") },
130+
{ "GeoRadius", () => db.GeoRadius($"{prefix}Geo", "member", 2.3) },
131+
{ "GeoRemove", () => db.GeoRemove($"{prefix}Geo", "member") },
132+
133+
{ "HashDecrement", () => db.HashDecrement($"{prefix}Hash", "hashfield", 4.5) },
134+
{ "HashDelete", () => db.HashDelete($"{prefix}Hash", "hashfield") },
135+
{ "HashExists", () => db.HashExists($"{prefix}Hash", "hashfield") },
136+
{ "HashGet", () => db.HashGet($"{prefix}Hash", "hashfield") },
137+
{ "HashGetAll", () => db.HashGetAll($"{prefix}Hash") },
138+
{ "HashIncrement", () => db.HashIncrement($"{prefix}Hash", "hashfield") },
139+
{ "HashKeys", () => db.HashKeys($"{prefix}Hash") },
140+
{ "HashLength", () => db.HashLength($"{prefix}Hash") },
141+
{ "HashScan", () => db.HashScan($"{prefix}Hash", "*", 5, CommandFlags.None) },
142+
{ "HashSet", () => { db.HashSet($"{prefix}Hash", new HashEntry[] { new HashEntry("hashfield", "hashvalue") }); return null; } },
143+
{ "HashValues", () => db.HashValues($"{prefix}Hash") },
144+
145+
{ "HyperLogLogAdd", () => db.HyperLogLogAdd($"{prefix}HyperLogLog", "value") },
146+
{ "HyperLogLogLength", () => db.HyperLogLogLength($"{prefix}HyperLogLog") },
147+
{ "HyperLogLogMerge", () => { db.HyperLogLogMerge($"{prefix}HyperLogLog2", new RedisKey[] { $"{prefix}HyperLogLog" }); return null; } },
148+
149+
{ "KeyDelete", () => db.KeyDelete($"{prefix}Key") },
150+
{ "KeyDump", () => db.KeyDump($"{prefix}Key") },
151+
{ "KeyExists", () => db.KeyExists($"{prefix}Key") },
152+
{ "KeyExpire", () => db.KeyExpire($"{prefix}Key", DateTime.Now) },
153+
{ "KeyMigrate", () => { db.KeyMigrate($"{prefix}Key", db.IdentifyEndpoint()); return null; } },
154+
{ "KeyMove", () => db.KeyMove($"{prefix}Key", 1) },
155+
{ "KeyPersist", () => db.KeyPersist($"{prefix}Key") },
156+
{ "KeyRandom", () => db.KeyRandom() },
157+
{ "KeyRename", () => db.KeyRename($"{prefix}Key", $"{prefix}Key2") },
158+
{ "KeyRestore", () => { db.KeyRestore($"{prefix}Key", new byte[] { 1,2,3,4 }); return null; } },
159+
{ "KeyTimeToLive", () => db.KeyTimeToLive($"{prefix}Key") },
160+
{ "KeyType", () => db.KeyType($"{prefix}Key") },
161+
162+
{ "ListGetByIndex", () => db.ListGetByIndex($"{prefix}List", 0) },
163+
{ "ListInsertAfter", () => db.ListInsertAfter($"{prefix}List", "value1", "value2") },
164+
{ "ListInsertBefore", () => db.ListInsertBefore($"{prefix}List", "value1", "value2") },
165+
{ "ListLeftPop", () => db.ListLeftPop($"{prefix}List") },
166+
{ "ListLeftPush", () => db.ListLeftPush($"{prefix}List", "value3") },
167+
{ "ListLength", () => db.ListLength($"{prefix}List") },
168+
{ "ListRange", () => db.ListRange($"{prefix}List") },
169+
{ "ListRemove", () => db.ListRemove($"{prefix}List", "value1") },
170+
{ "ListRightPop", () => db.ListRightPop($"{prefix}List") },
171+
{ "ListRightPopLeftPush", () => db.ListRightPopLeftPush($"{prefix}List", $"{prefix}List2") },
172+
{ "ListRightPush", () => db.ListRightPush($"{prefix}List", new RedisValue[] { "value1", "value2" }) },
173+
{ "ListSetByIndex", () => { db.ListSetByIndex($"{prefix}List", 0, "value3"); return null; } },
174+
{ "ListTrim", () => { db.ListTrim($"{prefix}List", 0, 1); return null; } },
175+
176+
{ "LockExtend", () => db.LockExtend($"{prefix}Lock", "value1", new TimeSpan(0, 0, 10)) },
177+
{ "LockQuery", () => db.LockQuery($"{prefix}Lock") },
178+
{ "LockRelease", () => db.LockRelease($"{prefix}Lock", "value1") },
179+
{ "LockTake", () => db.LockTake($"{prefix}Lock", "value1", new TimeSpan(0, 0, 10)) },
180+
181+
{ "Ping", () => db.Ping() },
182+
{ "Publish", () => db.Publish(new RedisChannel("value", RedisChannel.PatternMode.Auto), "message") },
183+
// { "ScriptEvaluate", () => db.ScriptEvaluate() }
184+
185+
{ "SetAdd", () => db.SetAdd($"{prefix}Set", "value1") },
186+
{ "SetCombine", () => db.SetCombine(SetOperation.Union, new RedisKey[] { $"{prefix}Set" }) },
187+
{ "SetCombineAndStore", () => db.SetCombineAndStore(SetOperation.Union, $"{prefix}Set", new RedisKey[] { $"{prefix}Set" }) },
188+
{ "SetContains", () => db.SetContains($"{prefix}Set", "value1") },
189+
{ "SetLength", () => db.SetLength($"{prefix}Set") },
190+
{ "SetMembers", () => db.SetMembers($"{prefix}Set") },
191+
{ "SetMove", () => db.SetMove($"{prefix}Set", $"{prefix}Set2", "value1") },
192+
{ "SetPop", () => db.SetPop($"{prefix}Set") },
193+
{ "SetRandomMember", () => db.SetRandomMember($"{prefix}Set") },
194+
{ "SetRandomMembers", () => db.SetRandomMembers($"{prefix}Set", 1) },
195+
{ "SetRemove", () => db.SetRemove($"{prefix}Set", "value1") },
196+
{ "SetScan", () => db.SetScan($"{prefix}Set", "*", 5) },
197+
198+
{ "Sort", () => db.Sort($"{prefix}Key") },
199+
{ "SortAndStore", () => db.SortAndStore($"{prefix}Key2", $"{prefix}Key") },
200+
201+
{ "SortedSetAdd", () => db.SortedSetAdd($"{prefix}SortedSet", new SortedSetEntry[] { new SortedSetEntry("element", 1) }) },
202+
{ "SortedSetCombineAndStore", () => db.SortedSetCombineAndStore(SetOperation.Union, $"{prefix}SortedSet2", $"{prefix}SortedSet", $"{prefix}SortedSet2") },
203+
{ "SortedSetDecrement", () => db.SortedSetDecrement($"{prefix}SortedSet", "element", 0.5) },
204+
{ "SortedSetIncrement", () => db.SortedSetIncrement($"{prefix}SortedSet", "element", 0.5) },
205+
{ "SortedSetLength", () => db.SortedSetLength($"{prefix}SortedSet") },
206+
{ "SortedSetLengthByValue", () => db.SortedSetLengthByValue($"{prefix}SortedSet", "value1", "value2") },
207+
{ "SortedSetRangeByRank", () => db.SortedSetRangeByRank($"{prefix}SortedSet") },
208+
{ "SortedSetRangeByRankWithScores", () => db.SortedSetRangeByRankWithScores($"{prefix}SortedSet") },
209+
{ "SortedSetRangeByScore", () => db.SortedSetRangeByScore($"{prefix}SortedSet") },
210+
{ "SortedSetRangeByScoreWithScores", () => db.SortedSetRangeByScoreWithScores($"{prefix}SortedSet") },
211+
{ "SortedSetRangeByValue", () => db.SortedSetRangeByValue($"{prefix}SortedSet") },
212+
{ "SortedSetRank", () => db.SortedSetRank($"{prefix}SortedSet", "element") },
213+
{ "SortedSetRemove", () => db.SortedSetRemove($"{prefix}SortedSet", "element") },
214+
{ "SortedSetRemoveRangeByRank", () => db.SortedSetRemoveRangeByRank($"{prefix}SortedSet", 0, 1) },
215+
{ "SortedSetRemoveRangeByScore", () => db.SortedSetRemoveRangeByScore($"{prefix}SortedSet", 1, 2) },
216+
{ "SortedSetRemoveRangeByValue", () => db.SortedSetRemoveRangeByValue($"{prefix}SortedSet", 1, 2) },
217+
{ "SortedSetScan", () => db.SortedSetScan($"{prefix}SortedSet", "*", 5) },
218+
{ "SortedSetScore", () => db.SortedSetScore($"{prefix}SortedSet", "element") },
219+
220+
{ "StringAppend", () => db.StringAppend($"{prefix}Key", "value") },
221+
{ "StringBitCount", () => db.StringBitCount($"{prefix}Key") },
222+
{ "StringBitOperation", () => db.StringBitOperation(Bitwise.And, $"{prefix}Key", new RedisKey[] {$"{prefix}Key2" }) },
223+
{ "StringBitPosition", () => db.StringBitPosition($"{prefix}Key", true) },
224+
{ "StringDecrement", () => db.StringDecrement($"{prefix}Key", 5.5) },
225+
{ "StringGet", () => db.StringGet($"{prefix}Key") },
226+
{ "StringGetBit", () => db.StringGetBit($"{prefix}Key", 5) },
227+
{ "StringGetRange", () => db.StringGetRange($"{prefix}Key", 0, 3) },
228+
{ "StringGetSet", () => db.StringGetSet($"{prefix}Key", "value") },
229+
{ "StringGetWithExpiry", () => db.StringGetWithExpiry($"{prefix}Key") },
230+
{ "StringIncrement", () => db.StringIncrement($"{prefix}Key", 7.2) },
231+
{ "StringLength", () => db.StringLength($"{prefix}Key") },
232+
{ "StringSet", () => db.StringSet(new KeyValuePair<RedisKey, RedisValue>[] { new KeyValuePair<RedisKey, RedisValue>($"{prefix}Key", "value") }) },
233+
{ "StringSetBit", () => db.StringSetBit($"{prefix}Key", 0, true) },
234+
{ "StringSetRange", () => db.StringSetRange($"{prefix}Key", 17, "value") },
235+
};
236+
}
237+
118238
private static TupleList<string, Task> RunStackExchangeAsync(string prefix, IDatabaseAsync db)
119239
{
120240
var tasks = new TupleList<string, Func<Task>>()
@@ -225,7 +345,7 @@ private static TupleList<string, Task> RunStackExchangeAsync(string prefix, IDat
225345
{
226346
pending.Add(item.Item1, item.Item2());
227347
}
228-
catch(Exception e)
348+
catch (Exception e)
229349
{
230350
while (e.InnerException != null)
231351
{
@@ -261,20 +381,31 @@ private static void RunCommands(TupleList<string, Func<object>> commands)
261381
{
262382
foreach (var cmd in commands)
263383
{
264-
if (cmd.Item2 == null)
384+
var f = cmd.Item2;
385+
if (f == null)
265386
{
266387
continue;
267388
}
268389

269390
object result;
270391
try
271392
{
272-
result = cmd.Item2();
393+
result = f();
394+
}
395+
catch (Exception e)
396+
{
397+
while (e.InnerException != null)
398+
{
399+
e = e.InnerException;
400+
}
401+
result = e.Message;
273402
}
274-
catch (Exception ex)
403+
404+
if (result is Task task)
275405
{
276-
result = ex.Message;
406+
result = TaskResult(task);
277407
}
408+
278409
Console.WriteLine($"{cmd.Item1}: {result}");
279410
}
280411
}

0 commit comments

Comments
 (0)