Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
StackExchange.Redis
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
tsai
StackExchange.Redis
Commits
4edebf2b
Commit
4edebf2b
authored
Jul 02, 2018
by
Nick Craver
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Tests: make Scripting parallel friendly
parent
71d8a60c
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
48 additions
and
31 deletions
+48
-31
Scripting.cs
StackExchange.Redis.Tests/Scripting.cs
+48
-31
No files found.
StackExchange.Redis.Tests/Scripting.cs
View file @
4edebf2b
...
...
@@ -17,18 +17,18 @@ public void TestBasicScripting()
{
Skip
.
IfMissingFeature
(
conn
,
nameof
(
RedisFeatures
.
Scripting
),
f
=>
f
.
Scripting
);
RedisValue
newId
=
Guid
.
NewGuid
().
ToString
();
RedisKey
custK
ey
=
Me
();
RedisKey
k
ey
=
Me
();
var
db
=
conn
.
GetDatabase
();
db
.
KeyDelete
(
custK
ey
);
db
.
HashSet
(
custK
ey
,
"id"
,
123
);
db
.
KeyDelete
(
k
ey
);
db
.
HashSet
(
k
ey
,
"id"
,
123
);
var
wasSet
=
(
bool
)
db
.
ScriptEvaluate
(
"if redis.call('hexists', KEYS[1], 'UniqueId') then return redis.call('hset', KEYS[1], 'UniqueId', ARGV[1]) else return 0 end"
,
new
RedisKey
[]
{
custK
ey
},
new
RedisValue
[]
{
newId
});
new
RedisKey
[]
{
k
ey
},
new
RedisValue
[]
{
newId
});
Assert
.
True
(
wasSet
);
wasSet
=
(
bool
)
db
.
ScriptEvaluate
(
"if redis.call('hexists', KEYS[1], 'UniqueId') then return redis.call('hset', KEYS[1], 'UniqueId', ARGV[1]) else return 0 end"
,
new
RedisKey
[]
{
custK
ey
},
new
RedisValue
[]
{
newId
});
new
RedisKey
[]
{
k
ey
},
new
RedisValue
[]
{
newId
});
Assert
.
False
(
wasSet
);
}
}
...
...
@@ -161,7 +161,9 @@ public void TestCallByHash()
byte
[]
hash
=
server
.
ScriptLoad
(
Script
);
var
db
=
conn
.
GetDatabase
();
RedisKey
[]
keys
=
{
Me
()
};
var
key
=
Me
();
db
.
KeyDelete
(
key
);
RedisKey
[]
keys
=
{
key
};
string
hexHash
=
string
.
Concat
(
hash
.
Select
(
x
=>
x
.
ToString
(
"X2"
)));
Assert
.
Equal
(
"2BAB3B661081DB58BD2341920E0BA7CF5DC77B25"
,
hexHash
);
...
...
@@ -235,18 +237,20 @@ public void LuaScriptWithKeys()
var
script
=
LuaScript
.
Prepare
(
Script
);
var
db
=
conn
.
GetDatabase
();
var
key
=
Me
();
db
.
KeyDelete
(
key
);
var
p
=
new
{
key
=
(
RedisKey
)
"testkey"
,
value
=
123
};
var
p
=
new
{
key
=
(
RedisKey
)
key
,
value
=
123
};
script
.
Evaluate
(
db
,
p
);
var
val
=
db
.
StringGet
(
"testkey"
);
var
val
=
db
.
StringGet
(
key
);
Assert
.
Equal
(
123
,
(
int
)
val
);
// no super clean way to extract this; so just abuse InternalsVisibleTo
script
.
ExtractParameters
(
p
,
null
,
out
RedisKey
[]
keys
,
out
RedisValue
[]
args
);
Assert
.
NotNull
(
keys
);
Assert
.
Single
(
keys
);
Assert
.
Equal
(
"testkey"
,
keys
[
0
]);
Assert
.
Equal
(
key
,
keys
[
0
]);
}
}
...
...
@@ -265,11 +269,13 @@ public void NoInlineReplacement()
Assert
.
Equal
(
"redis.call('set', ARGV[1], 'hello@example')"
,
script
.
ExecutableScript
);
var
db
=
conn
.
GetDatabase
();
var
key
=
Me
();
db
.
KeyDelete
(
key
);
var
p
=
new
{
key
=
(
RedisKey
)
"key"
};
var
p
=
new
{
key
};
script
.
Evaluate
(
db
,
p
);
var
val
=
db
.
StringGet
(
"key"
);
var
val
=
db
.
StringGet
(
key
);
Assert
.
Equal
(
"hello@example"
,
val
);
}
}
...
...
@@ -346,18 +352,20 @@ public void LoadedLuaScriptWithKeys()
var
prepared
=
script
.
Load
(
server
);
var
db
=
conn
.
GetDatabase
();
var
key
=
Me
();
db
.
KeyDelete
(
key
);
var
p
=
new
{
key
=
(
RedisKey
)
"testkey"
,
value
=
123
};
var
p
=
new
{
key
=
(
RedisKey
)
key
,
value
=
123
};
prepared
.
Evaluate
(
db
,
p
);
var
val
=
db
.
StringGet
(
"testkey"
);
var
val
=
db
.
StringGet
(
key
);
Assert
.
Equal
(
123
,
(
int
)
val
);
// no super clean way to extract this; so just abuse InternalsVisibleTo
prepared
.
Original
.
ExtractParameters
(
p
,
null
,
out
RedisKey
[]
keys
,
out
RedisValue
[]
args
);
Assert
.
NotNull
(
keys
);
Assert
.
Single
(
keys
);
Assert
.
Equal
(
"testkey"
,
keys
[
0
]);
Assert
.
Equal
(
key
,
keys
[
0
]);
}
}
...
...
@@ -414,14 +422,16 @@ public void IDatabaseLuaScriptConvenienceMethods()
Skip
.
IfMissingFeature
(
conn
,
nameof
(
RedisFeatures
.
Scripting
),
f
=>
f
.
Scripting
);
var
script
=
LuaScript
.
Prepare
(
Script
);
var
db
=
conn
.
GetDatabase
();
db
.
ScriptEvaluate
(
script
,
new
{
key
=
(
RedisKey
)
"key"
,
value
=
"value"
});
var
val
=
db
.
StringGet
(
"key"
);
var
key
=
Me
();
db
.
KeyDelete
(
key
);
db
.
ScriptEvaluate
(
script
,
new
{
key
=
(
RedisKey
)
key
,
value
=
"value"
});
var
val
=
db
.
StringGet
(
key
);
Assert
.
Equal
(
"value"
,
val
);
var
prepared
=
script
.
Load
(
conn
.
GetServer
(
conn
.
GetEndPoints
()[
0
]));
db
.
ScriptEvaluate
(
prepared
,
new
{
key
=
(
RedisKey
)
"key2"
,
value
=
"value2"
});
var
val2
=
db
.
StringGet
(
"key
2"
);
db
.
ScriptEvaluate
(
prepared
,
new
{
key
=
(
RedisKey
)
(
key
+
"2"
)
,
value
=
"value2"
});
var
val2
=
db
.
StringGet
(
key
+
"
2"
);
Assert
.
Equal
(
"value2"
,
val2
);
}
}
...
...
@@ -437,11 +447,13 @@ public void IServerLuaScriptConvenienceMethods()
var
script
=
LuaScript
.
Prepare
(
Script
);
var
server
=
conn
.
GetServer
(
conn
.
GetEndPoints
()[
0
]);
var
db
=
conn
.
GetDatabase
();
var
key
=
Me
();
db
.
KeyDelete
(
key
);
var
prepared
=
server
.
ScriptLoad
(
script
);
db
.
ScriptEvaluate
(
prepared
,
new
{
key
=
(
RedisKey
)
"key3"
,
value
=
"value3"
});
var
val
=
db
.
StringGet
(
"key3"
);
db
.
ScriptEvaluate
(
prepared
,
new
{
key
=
(
RedisKey
)
key
,
value
=
"value3"
});
var
val
=
db
.
StringGet
(
key
);
Assert
.
Equal
(
"value3"
,
val
);
}
}
...
...
@@ -451,15 +463,16 @@ public void LuaScriptPrefixedKeys()
{
const
string
Script
=
"redis.call('set', @key, @value)"
;
var
prepared
=
LuaScript
.
Prepare
(
Script
);
var
p
=
new
{
key
=
(
RedisKey
)
"key"
,
value
=
"hello"
};
var
key
=
Me
();
var
p
=
new
{
key
=
(
RedisKey
)
key
,
value
=
"hello"
};
// no super clean way to extract this; so just abuse InternalsVisibleTo
prepared
.
ExtractParameters
(
p
,
"prefix-"
,
out
RedisKey
[]
keys
,
out
RedisValue
[]
args
);
Assert
.
NotNull
(
keys
);
Assert
.
Single
(
keys
);
Assert
.
Equal
(
"prefix-
key"
,
keys
[
0
]);
Assert
.
Equal
(
"prefix-
"
+
key
,
keys
[
0
]);
Assert
.
Equal
(
2
,
args
.
Length
);
Assert
.
Equal
(
"prefix-
key"
,
args
[
0
]);
Assert
.
Equal
(
"prefix-
"
+
key
,
args
[
0
]);
Assert
.
Equal
(
"hello"
,
args
[
1
]);
}
...
...
@@ -473,16 +486,18 @@ public void LuaScriptWithWrappedDatabase()
Skip
.
IfMissingFeature
(
conn
,
nameof
(
RedisFeatures
.
Scripting
),
f
=>
f
.
Scripting
);
var
db
=
conn
.
GetDatabase
(
0
);
var
wrappedDb
=
KeyspaceIsolation
.
DatabaseExtensions
.
WithKeyPrefix
(
db
,
"prefix-"
);
var
key
=
Me
();
db
.
KeyDelete
(
key
);
var
prepared
=
LuaScript
.
Prepare
(
Script
);
wrappedDb
.
ScriptEvaluate
(
prepared
,
new
{
key
=
(
RedisKey
)
"mykey"
,
value
=
123
});
var
val1
=
wrappedDb
.
StringGet
(
"mykey"
);
wrappedDb
.
ScriptEvaluate
(
prepared
,
new
{
key
=
(
RedisKey
)
key
,
value
=
123
});
var
val1
=
wrappedDb
.
StringGet
(
key
);
Assert
.
Equal
(
123
,
(
int
)
val1
);
var
val2
=
db
.
StringGet
(
"prefix-
mykey"
);
var
val2
=
db
.
StringGet
(
"prefix-
"
+
key
);
Assert
.
Equal
(
123
,
(
int
)
val2
);
var
val3
=
db
.
StringGet
(
"mykey"
);
var
val3
=
db
.
StringGet
(
key
);
Assert
.
True
(
val3
.
IsNull
);
}
}
...
...
@@ -497,17 +512,19 @@ public void LoadedLuaScriptWithWrappedDatabase()
Skip
.
IfMissingFeature
(
conn
,
nameof
(
RedisFeatures
.
Scripting
),
f
=>
f
.
Scripting
);
var
db
=
conn
.
GetDatabase
(
0
);
var
wrappedDb
=
KeyspaceIsolation
.
DatabaseExtensions
.
WithKeyPrefix
(
db
,
"prefix2-"
);
var
key
=
Me
();
db
.
KeyDelete
(
key
);
var
server
=
conn
.
GetServer
(
conn
.
GetEndPoints
()[
0
]);
var
prepared
=
LuaScript
.
Prepare
(
Script
).
Load
(
server
);
wrappedDb
.
ScriptEvaluate
(
prepared
,
new
{
key
=
(
RedisKey
)
"mykey"
,
value
=
123
});
var
val1
=
wrappedDb
.
StringGet
(
"mykey"
);
wrappedDb
.
ScriptEvaluate
(
prepared
,
new
{
key
=
(
RedisKey
)
key
,
value
=
123
});
var
val1
=
wrappedDb
.
StringGet
(
key
);
Assert
.
Equal
(
123
,
(
int
)
val1
);
var
val2
=
db
.
StringGet
(
"prefix2-
mykey"
);
var
val2
=
db
.
StringGet
(
"prefix2-
"
+
key
);
Assert
.
Equal
(
123
,
(
int
)
val2
);
var
val3
=
db
.
StringGet
(
"mykey"
);
var
val3
=
db
.
StringGet
(
key
);
Assert
.
True
(
val3
.
IsNull
);
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment