Skip to content

Commit

Permalink
Merge f1772eb into 83e6cd3
Browse files Browse the repository at this point in the history
  • Loading branch information
kbaum committed Oct 1, 2019
2 parents 83e6cd3 + f1772eb commit 82f6494
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 22 deletions.
2 changes: 1 addition & 1 deletion lib/coverband/adapters/base.rb
Expand Up @@ -40,7 +40,7 @@ def coverage(_local_type = nil)
end

def size_in_mib
format('%.2f', (size.to_f / 2**20))
format('%<size>.2f', size: (size.to_f / 2**20))
end

def save_report(_report)
Expand Down
28 changes: 7 additions & 21 deletions lib/coverband/adapters/hash_redis_store.rb
Expand Up @@ -127,27 +127,13 @@ def save_report_script_input(key:, file:, file_hash:, data:, report_time:, updat
end

def hash_incr_script
@hash_incr_script ||= @redis.script(:load, <<~LUA)
local first_updated_at = table.remove(ARGV, 1)
local last_updated_at = table.remove(ARGV, 1)
local file = table.remove(ARGV, 1)
local file_hash = table.remove(ARGV, 1)
local ttl = table.remove(ARGV, 1)
local file_length = table.remove(ARGV, 1)
local hash_key = table.remove(KEYS, 1)
redis.call('HMSET', hash_key, '#{LAST_UPDATED_KEY}', last_updated_at, '#{FILE_KEY}', file, '#{FILE_HASH}', file_hash, '#{FILE_LENGTH_KEY}', file_length)
redis.call('HSETNX', hash_key, '#{FIRST_UPDATED_KEY}', first_updated_at)
for i, key in ipairs(KEYS) do
if ARGV[i] == '-1' then
redis.call("HSET", hash_key, key, ARGV[i])
else
redis.call("HINCRBY", hash_key, key, ARGV[i])
end
end
if ttl ~= '-1' then
redis.call("EXPIRE", hash_key, ttl)
end
LUA
@hash_incr_script ||= @redis.script(:load, lua_script_content)
end

def lua_script_content
File.read(File.join(
File.dirname(__FILE__), '../../../lua/lib/persist-coverage.lua'
))
end

def values_from_redis(local_type, files)
Expand Down
19 changes: 19 additions & 0 deletions lua/lib/persist-coverage.lua
@@ -0,0 +1,19 @@
local first_updated_at = table.remove(ARGV, 1)
local last_updated_at = table.remove(ARGV, 1)
local file = table.remove(ARGV, 1)
local file_hash = table.remove(ARGV, 1)
local ttl = table.remove(ARGV, 1)
local file_length = table.remove(ARGV, 1)
local hash_key = table.remove(KEYS, 1)
redis.call('HMSET', hash_key, 'last_updated_at', last_updated_at, 'file', file, 'file_hash', file_hash, 'file_length', file_length)
redis.call('HSETNX', hash_key, 'first_updated_at', first_updated_at)
for i, key in ipairs(KEYS) do
if ARGV[i] == '-1' then
redis.call("HSET", hash_key, key, ARGV[i])
else
redis.call("HINCRBY", hash_key, key, ARGV[i])
end
end
if ttl ~= '-1' then
redis.call("EXPIRE", hash_key, ttl)
end
20 changes: 20 additions & 0 deletions lua/test/harness.lua
@@ -0,0 +1,20 @@
inspect = require "inspect"
require './lua/test/redis-call'


KEYS = {}
ARGV = {}


function call_redis_script(script, keys, argv)
-- This may not be strictly necessary
for k,v in pairs(ARGV) do ARGV[k] = nil end
for k,v in pairs(KEYS) do KEYS[k] = nil end

for k,v in pairs(keys) do table.insert(KEYS, v) end
for k,v in pairs(argv) do table.insert(ARGV, v) end

return dofile('./lua/lib/' .. script)
end

return call_redis_script;
47 changes: 47 additions & 0 deletions lua/test/redis-call.lua
@@ -0,0 +1,47 @@
redis = require 'redis'

-- If you have some different host/port change it here
local host = "127.0.0.1"
local port = 6379


client = redis.connect(host, port)

-- Workaround for absence of redis.call
redis.call = function(cmd, ...)
local arg={...}
local args_string = ''
for i,v in ipairs(arg) do
args_string = args_string .. ' ' .. v
end
cmd = string.lower(cmd)
print(cmd .. args_string)
--local result = assert(load('return client:'.. cmd ..'(...)'))(...)
local result = assert(loadstring('return client:'.. cmd ..'(...)'))(...)

-- The redis-lua library returns some values differently to how `redis.call` works inside redis.
-- this makes the responses look like those from the builtin redis
local response_lookup = {
type = function() return { ["ok"]= result } end,
sadd = function() return tonumber(result) end,
zrange = function()
if type(result) == "table" and type(result[1]) == "table" then
-- Deal with WITHSCORES...
local new_result = {}
for k,v in pairs(result) do
table.insert(new_result, v[1])
table.insert(new_result, v[2])
end
return new_result;
end

return result;
end
}

if response_lookup[cmd] then
return response_lookup[cmd]()
end

return result;
end
26 changes: 26 additions & 0 deletions lua/test/test-persist-coverage.lua
@@ -0,0 +1,26 @@
local call_redis_script = require "./lua/test/harness";

describe("incr-and-stor", function()

-- Flush the database before running the tests
before_each(function()
redis.call('FLUSHDB')
end)

it("should add single items", function()
local args = { 1569453853, 1569453853, "./dog.rb", "abcd", '-1', 3, 0, 1, 2 }
local keys = {"coverband_hash_3_3.coverband_test.runtime../dog.rb.abcd", 0, 1, 2 }
call_redis_script('persist-coverage.lua', keys , args );
results = redis.call('HGETALL', "coverband_hash_3_3.coverband_test.runtime../dog.rb.abcd")
assert.are.same({
["0"] = "0",
["1"] = "1",
["2"] = "2",
file = "./dog.rb",
file_hash = "abcd",
file_length = "3",
first_updated_at = "1569453853",
last_updated_at = "1569453853"
}, results)
end)
end)

0 comments on commit 82f6494

Please sign in to comment.