in finagle-redis/src/it/scala/com/twitter/finagle/redis/commands/script/ScriptClientIntegrationSuite.scala [88:171]
def testScriptAndSha[T, X](
testName: String,
scripts: Seq[Buf],
cast: Reply => T,
testCase: (String, Seq[Buf], Client => (Buf, Seq[Buf], Seq[Buf]) => Future[T]) => X
) = {
val sha1s = scripts.map(SHA1hex)
def noSha1sExist(client: Client) = {
assert(!Await.result(client.scriptExists(sha1s: _*)).reduce(_ || _))
}
def allSha1sExist(client: Client) = {
assert(Await.result(client.scriptExists(sha1s: _*)).reduce(_ && _))
}
test("In " + testName + ", scriptExists should return false in the beginning") {
withRedisClient {
noSha1sExist
}
}
testCase(
testName,
scripts,
{ client => (s, keys, argv) => client.eval(s, keys, argv) map cast })
test(
"In " + testName + ", scriptExists should return true for executed scripts; and return false after scriptFlush"
) {
withRedisClient { client =>
allSha1sExist(client)
Await.result(client.scriptFlush())
noSha1sExist(client)
}
}
test(
"In " + testName + ", scriptLoad should return SHA1 hex string of scripts; and make scriptExists return true"
) {
withRedisClient { client =>
val digests = Await.result(Future.collect(scripts map { s => client.scriptLoad(s) }))
assert(digests == sha1s)
allSha1sExist(client)
}
}
testCase(
testName + " SHA w/o fallback",
scripts,
{ client => (s, keys, argv) =>
client.evalSha(SHA1hex(s), keys, argv) map cast
})
test(
testName + " SHA w/o fallback should make scriptsExists return true; and scriptFlush should make scriptExists return false"
) {
withRedisClient { client =>
allSha1sExist(client)
Await.result(client.scriptFlush())
noSha1sExist(client)
}
}
testCase(
testName + " SHA w/ fallback",
scripts,
{ client => (s, keys, argv) =>
client.evalSha(SHA1hex(s), s, keys, argv) map cast
})
test(
testName + " SHA w/ fallback should make scriptsExists return true; and scriptFlush should make scriptExists return false again"
) {
withRedisClient { client =>
allSha1sExist(client)
Await.result(client.scriptFlush())
noSha1sExist(client)
}
}
}