local lu = require('luaunit')
local cache = require('cache')
local log = require('mc.logging')
local utils = require('mc.utils')
TestCache = {}
local old_log_raise = log.raise
function TestCache:setUp()
log.raise = function() end
end
function TestCache:tearDown()
log.raise = old_log_raise
end
function TestCache:test_flush_queue()
local cache_obj = cache.new({})
local count = 0
local args1 = {
fetch = {
cb = function()
count = count + 10
end,
run_again = function() end,
},
offset = 0xffffffff,
}
local args2 = {
fetch = {
cb = function()
count = count + 1
end,
},
}
local args3 = {
fetch = {
cb = function()
count = count + 3
end,
},
}
local args4 = {
fetches = {
{
cb = function()
count = count + 100
end,
},
},
}
cache_obj:read(args1)
cache_obj:write(args2)
cache_obj:write(args3)
cache_obj:write(args3)
cache_obj:plugin_request(args4)
lu.assertEquals(cache_obj.queue_len, { cpld_upg = 0, plugin = 1, read = 1, write = 3 })
lu.assertEquals(cache_obj.queue_over_flag, { cpld_upg = false, plugin = false, read = false, write = false })
cache_obj:process_plugin(function() end)
cache_obj:flush_queue('err')
lu.assertEquals(cache_obj.queue_len, { cpld_upg = 0, plugin = 0, read = 0, write = 0 })
lu.assertIsNil(cache_obj.read_queue)
lu.assertIsNil(cache_obj.write_queue)
lu.assertIsNil(cache_obj.plugin_queue)
lu.assertEquals(count, 14)
end
function TestCache:test_read_normal()
local cache_obj = cache.new({ channel_id = 1 })
local count = 0
local args1 = {
fetch = {
cb = function()
count = count + 1
end,
unexecuted = false,
},
offset = 0x1,
length = 4,
type = 'type1',
}
cache_obj:read(args1, false)
lu.assertEquals(cache_obj.queue_len.read, 1)
lu.assertEquals(#cache_obj.read_queue, 1)
local index = utils.get_table_node(cache_obj.read_queue_index, 0x1, 4, 'type1')
lu.assertEquals(index, 1)
end
function TestCache:test_read_head_insertion()
local cache_obj = cache.new({ channel_id = 1 })
local count = 0
local args1 = {
fetch = {
cb = function()
count = count + 1
end,
},
offset = 0x1,
length = 4,
type = 'type1',
}
cache_obj:read(args1, false)
local args2 = {
fetch = {
cb = function()
count = count + 2
end,
},
offset = 0x2,
length = 4,
type = 'type1',
}
cache_obj:read(args2, true)
lu.assertEquals(cache_obj.queue_len.read, 2)
lu.assertEquals(cache_obj.read_queue[1].fetch.cb, args2.fetch.cb)
lu.assertEquals(cache_obj.read_queue[2].fetch.cb, args1.fetch.cb)
local index1 = utils.get_table_node(cache_obj.read_queue_index, 0x1, 4, 'type1')
local index2 = utils.get_table_node(cache_obj.read_queue_index, 0x2, 4, 'type1')
lu.assertEquals(index1, 2)
lu.assertEquals(index2, 1)
end
function TestCache:test_read_duplicate_merge()
local cache_obj = cache.new({ channel_id = 1 })
local count = 0
local args1 = {
fetch = {
cb = function()
count = count + 1
end,
},
offset = 0x1,
length = 4,
type = 'type1',
}
cache_obj:read(args1, false)
local args2 = {
fetch = {
cb = function()
count = count + 2
end,
},
offset = 0x1,
length = 4,
type = 'type1',
}
cache_obj:read(args2, false)
lu.assertEquals(cache_obj.queue_len.read, 2)
lu.assertEquals(#cache_obj.read_queue, 1)
lu.assertEquals(#cache_obj.read_queue[1].fetches, 2)
end
function TestCache:test_read_request_merging()
local cache_obj = cache.new({
channel_id = 1,
name = 'test_chip',
})
local call_count = 0
local callback = function()
call_count = call_count + 1
end
local base_args = {
offset = 0x1234,
length = 4,
type = 'test_type',
fetch = {
cb = callback,
unexecuted = true,
},
}
local requests = {}
for i = 1, 5 do
requests[i] = {
offset = base_args.offset,
length = base_args.length,
type = base_args.type,
fetch = {
cb = base_args.fetch.cb,
unexecuted = true,
},
}
cache_obj:read(requests[i], false)
end
lu.assertEquals(cache_obj.queue_len.read, 5)
lu.assertEquals(#cache_obj.read_queue, 1)
lu.assertEquals(#cache_obj.read_queue[1].fetches, 5)
local index = utils.get_table_node(cache_obj.read_queue_index, base_args.offset, base_args.length, base_args.type)
lu.assertEquals(index, 1)
cache_obj:process_read(function(args)
for _, fetch in ipairs(args.fetches) do
fetch.cb()
end
end)
lu.assertEquals(call_count, 5)
end
function TestCache:test_mixed_read_requests()
local cache_obj = cache.new({
channel_id = 1,
name = 'test_chip',
})
local call_counts = {
normal = 0,
special = 0,
rpc = 0,
}
local normal_args1 = {
fetch = {
cb = function()
call_counts.normal = call_counts.normal + 1
end,
},
offset = 0x1,
length = 4,
type = 'normal',
}
local special_args = {
fetch = {
cb = function()
call_counts.special = call_counts.special + 1
end,
},
offset = 0xffffffff,
}
local rpc_args = {
fetch = {
cb = function()
call_counts.rpc = call_counts.rpc + 1
end,
},
offset = 0x2,
protocol_flag = true,
}
local normal_args2 = {
fetch = {
cb = function()
call_counts.normal = call_counts.normal + 1
end,
},
offset = 0x3,
length = 4,
type = 'normal',
}
cache_obj:read(normal_args1, false)
cache_obj:read(special_args, true)
cache_obj:read(rpc_args, false)
cache_obj:read(normal_args2, false)
lu.assertEquals(cache_obj.queue_len.read, 4)
lu.assertEquals(#cache_obj.read_queue, 4)
lu.assertEquals(cache_obj.read_queue[1].offset, 0xffffffff)
lu.assertEquals(cache_obj.read_queue[2].offset, 0x1)
lu.assertIsTrue(cache_obj.read_queue[3].protocol_flag)
lu.assertEquals(cache_obj.read_queue[4].offset, 0x3)
lu.assertEquals(utils.get_table_node(cache_obj.read_queue_index, 0x1, 4, 'normal'), 2)
lu.assertEquals(utils.get_table_node(cache_obj.read_queue_index, 0x3, 4, 'normal'), 4)
lu.assertIsNil(utils.get_table_node(cache_obj.read_queue_index, 0xffffffff, nil, nil))
lu.assertIsNil(utils.get_table_node(cache_obj.read_queue_index, 0x2, nil, nil))
cache_obj:process_read(function(args)
if args.fetches then
for _, f in ipairs(args.fetches) do
f.cb()
end
else
args.fetch.cb()
end
end)
lu.assertEquals(call_counts.normal, 2)
lu.assertEquals(call_counts.special, 1)
lu.assertEquals(call_counts.rpc, 1)
end
function TestCache:test_read_rpc_special()
local cache_obj = cache.new({ channel_id = 1 })
local count = 0
local args1 = {
fetch = {
cb = function()
count = count + 1
end,
},
offset = 0xffffffff,
protocol_flag = true,
}
cache_obj:read(args1, true)
lu.assertEquals(cache_obj.queue_len.read, 1)
lu.assertEquals(cache_obj.read_queue[1].offset, 0xffffffff)
lu.assertEquals(cache_obj.read_queue_index, {})
end
function TestCache:test_read_with_parent()
local parent = cache.new({
channel_id = 0,
name = 'parent_chip',
})
parent.child_queue = {}
parent.child_queue_index = {}
local child1 = cache.new({
channel_id = 1,
name = 'child_chip_1',
})
local child2 = cache.new({
channel_id = 1,
name = 'child_chip_2',
})
child1:set_parent(parent)
child2:set_parent(parent)
local args = {
fetch = { cb = function() end },
offset = 0x1,
length = 4,
type = 'type1',
}
child1:read(args, true)
lu.assertEquals(#parent.child_queue[1], 1)
child2:read(args, false)
lu.assertEquals(#parent.child_queue[1], 2)
lu.assertNotNil(parent.child_queue_index[1]['child_chip_1'])
lu.assertNotNil(parent.child_queue_index[1]['child_chip_2'])
end