This repository was archived by the owner on Dec 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 872
/
Copy pathRedisClientEvalTests.cs
191 lines (162 loc) · 6.3 KB
/
RedisClientEvalTests.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
using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using ServiceStack.Common;
using ServiceStack.Text;
namespace ServiceStack.Redis.Tests
{
[TestFixture, Category("Integration")]
public class RedisClientEvalTests : RedisClientTestsBase
{
public override void OnBeforeEachTest()
{
//base.OnBeforeEachTest();
//Run on local build server
Redis = new RedisClient(TestConfig.SingleHost);
Redis.FlushAll();
}
[Test]
public void Can_Eval_int()
{
var intVal = Redis.ExecLuaAsInt("return 3141591");
Assert.That(intVal, Is.EqualTo(3141591));
}
[Test]
public void Can_EvalSha_int()
{
var luaBody = "return 3141591";
Redis.ExecLuaAsInt(luaBody);
var sha1 = Redis.CalculateSha1(luaBody);
var intVal = Redis.ExecLuaShaAsInt(sha1);
Assert.That(intVal, Is.EqualTo(3141591));
}
[Test]
public void Can_Eval_int_with_args()
{
var intVal = Redis.ExecLuaAsInt("return 3141591", "20", "30", "40");
Assert.That(intVal, Is.EqualTo(3141591));
}
[Test]
public void Can_Eval_int_with_keys_and_args()
{
var intVal = Redis.ExecLuaAsInt("return KEYS[1] + ARGV[1]", new[] { "20" }, new[] { "30", "40" });
Assert.That(intVal, Is.EqualTo(50));
}
[Test]
public void Can_Eval_int2()
{
var intVal = Redis.ExecLuaAsInt("return ARGV[1] + ARGV[2]", "10", "20");
Assert.That(intVal, Is.EqualTo(30));
}
[Test]
public void Can_Eval_string()
{
var strVal = Redis.ExecLuaAsString(@"return 'abc'");
Assert.That(strVal, Is.EqualTo("abc"));
}
[Test]
public void Can_Eval_HelloWorld_string()
{
var strVal = Redis.ExecLuaAsString(@"return 'Hello, ' .. ARGV[1] .. '!'", "Redis Lua");
Assert.That(strVal, Is.EqualTo("Hello, Redis Lua!"));
}
[Test]
public void Can_Eval_string_with_args()
{
var strVal = Redis.ExecLuaAsString(@"return 'abc'", "at", "dot", "com");
Assert.That(strVal, Is.EqualTo("abc"));
}
[Test]
public void Can_Eval_string_with_keys_an_args()
{
var strVal = Redis.ExecLuaAsString(@"return KEYS[1] .. ARGV[1]", new[] { "at" }, new[] { "dot", "com" });
Assert.That(strVal, Is.EqualTo("atdot"));
}
[Test]
public void Can_Eval_multidata_with_args()
{
var strVals = Redis.ExecLuaAsList(@"return {ARGV[1],ARGV[2],ARGV[3]}", "at", "dot", "com");
Assert.That(strVals, Is.EquivalentTo(new List<string> { "at", "dot", "com" }));
}
[Test]
public void Can_Eval_multidata_with_keys_and_args()
{
var strVals = Redis.ExecLuaAsList(@"return {KEYS[1],ARGV[1],ARGV[2]}", new[] { "at" }, new[] { "dot", "com" });
Assert.That(strVals, Is.EquivalentTo(new List<string> { "at", "dot", "com" }));
}
[Test]
public void Can_Load_and_Exec_script()
{
var luaBody = "return 'load script and exec'";
var sha1 = Redis.LoadLuaScript(luaBody);
var result = Redis.ExecLuaShaAsString(sha1);
Assert.That(result, Is.EqualTo("load script and exec"));
}
[Test]
public void Does_flush_all_scripts()
{
var luaBody = "return 'load script and exec'";
var sha1 = Redis.LoadLuaScript(luaBody);
var result = Redis.ExecLuaShaAsString(sha1);
Assert.That(result, Is.EqualTo("load script and exec"));
Redis.RemoveAllLuaScripts();
try
{
result = Redis.ExecLuaShaAsString(sha1);
Assert.Fail("script shouldn't exist");
}
catch (RedisResponseException ex)
{
Assert.That(ex.Message, Does.Contain("NOSCRIPT"));
}
}
[Test]
public void Can_detect_which_scripts_exist()
{
var sha1 = Redis.LoadLuaScript("return 'script1'");
var sha2 = Redis.CalculateSha1("return 'script2'");
var sha3 = Redis.LoadLuaScript("return 'script3'");
Assert.That(Redis.HasLuaScript(sha1));
var existsMap = Redis.WhichLuaScriptsExists(sha1, sha2, sha3);
Assert.That(existsMap[sha1]);
Assert.That(!existsMap[sha2]);
Assert.That(existsMap[sha3]);
}
[Test]
public void Can_create_ZPop_with_lua()
{
var luaBody = @"
local val = redis.call('zrange', KEYS[1], 0, ARGV[1]-1)
if val then redis.call('zremrangebyrank', KEYS[1], 0, ARGV[1]-1) end
return val";
var i = 0;
var alphabet = 26.Times(c => ((char)('A' + c)).ToString());
alphabet.ForEach(x => Redis.AddItemToSortedSet("zalphabet", x, i++));
var letters = Redis.ExecLuaAsList(luaBody, keys: new[] { "zalphabet" }, args: new[] { "3" });
letters.PrintDump();
Assert.That(letters, Is.EquivalentTo(new[] { "A", "B", "C" }));
}
[Test]
public void Can_create_ZRevPop_with_lua()
{
var luaBody = @"
local val = redis.call('zrange', KEYS[1], -ARGV[1], -1)
if val then redis.call('zremrangebyrank', KEYS[1], -ARGV[1], -1) end
return val";
var i = 0;
var alphabet = 26.Times(c => ((char)('A' + c)).ToString());
alphabet.ForEach(x => Redis.AddItemToSortedSet("zalphabet", x, i++));
var letters = Redis.ExecLuaAsList(luaBody, keys: new[] { "zalphabet" }, args: new[] { "3" });
letters.PrintDump();
Assert.That(letters, Is.EquivalentTo(new[] { "X", "Y", "Z" }));
}
[Test]
public void Can_return_DaysOfWeek_as_list()
{
Enum.GetNames(typeof(DayOfWeek)).ToList()
.ForEach(x => Redis.AddItemToList("DaysOfWeek", x));
Redis.ExecLuaAsList("return redis.call('LRANGE', 'DaysOfWeek', 0, -1)").PrintDump();
}
}
}