Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: improve base testsuite #1804

Merged
merged 1 commit into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion spec/unit/common_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe("Common modules", function()
end
end)
it("should serialize blitbuffer", function()
local w, h = 600, 800
local w, h = 75, 100
local bb = Blitbuffer.new(w, h)
local random = math.random
for i = 0, h -1 do
Expand Down
4 changes: 3 additions & 1 deletion spec/unit/mupdf_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ local sample_pdf = "spec/base/unit/data/Alice.pdf"
local paper_pdf = "spec/base/unit/data/Paper.pdf"
local password_pdf = "spec/base/unit/data/testdocument.pdf"
local simple_pdf = "spec/base/unit/data/simple.pdf"
local tmp_pdf = "/tmp/out.pdf"
local simple_pdf_compare = "spec/base/unit/data/simple-out.pdf"
local simple_pdf_annotated_compare = "spec/base/unit/data/simple-out-annotated.pdf"
local simple_pdf_annotation_deleted_compare = "spec/base/unit/data/simple-out-annotation-deleted.pdf"
Expand Down Expand Up @@ -130,6 +129,7 @@ describe("mupdf module", function()
assert.is_not_nil(page)
page:addMarkupAnnotation(annotation_quadpoints, 1, ffi.C.PDF_ANNOT_HIGHLIGHT)
page:close()
local tmp_pdf = os.tmpname()
doc:writeDocument(tmp_pdf)
doc:close()
assert.is_equal(
Expand All @@ -143,6 +143,7 @@ describe("mupdf module", function()
assert.is_not_nil(doc)
local page = doc:openPage(1)
assert.is_not_nil(page)
local tmp_pdf = os.tmpname()
doc:writeDocument(tmp_pdf)
page:addMarkupAnnotation(annotation_quadpoints, 1, ffi.C.PDF_ANNOT_HIGHLIGHT)
local annot = page:getMarkupAnnotation(annotation_quadpoints, 1)
Expand All @@ -164,6 +165,7 @@ describe("mupdf module", function()
local annot = page:getMarkupAnnotation(annotation_quadpoints, 1)
page:updateMarkupAnnotation(annot, "annotation contents")
page:close()
local tmp_pdf = os.tmpname()
doc:writeDocument(tmp_pdf)
doc:close()
assert.is_equal(
Expand Down
77 changes: 46 additions & 31 deletions spec/unit/util_spec.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
local util = require("ffi/util")
local lfs = require("libs/libkoreader-lfs")

local function waitForSubProcessDone(pid)
for i = 1, 20 do
util.usleep(100000)
if util.isSubProcessDone(pid) then
return true
end
end
return false
end

describe("util module", function()
describe("util.gettime", function()

Expand Down Expand Up @@ -110,23 +120,25 @@ describe("util module", function()

describe("util.copyFile", function()
local sample_pdf = "spec/base/unit/data/simple.pdf"
local output_pdf = "spec/base/unit/data/test_util_copy.pdf"

it("should copy properly", function()
local tmp_f = os.tmpname()
local from_f = io.open(sample_pdf, "r")
local from_data = from_f:read("*a")
util.copyFile(sample_pdf, output_pdf)
local to_f = io.open(output_pdf, "r")
util.copyFile(sample_pdf, tmp_f)
local to_f = io.open(tmp_f, "r")
local to_data = to_f:read("*a")
assert.equals(from_data, to_data)
from_f:close()
to_f:close()
os.remove(output_pdf)
os.remove(tmp_f)
end)

it("fail at non-exists files", function()
local err = util.copyFile("/tmp/abc/123/foo/bar/baz/777.pkg", output_pdf)
local tmp_f = os.tmpname()
local err = util.copyFile("/tmp/abc/123/foo/bar/baz/777.pkg", tmp_f)
assert.is_not_nil(err)
os.remove(tmp_f)
end)
end)

Expand Down Expand Up @@ -163,13 +175,11 @@ describe("util module", function()

describe("util.getNonBlockingReadSize", function()
it("should read pipe when data is available", function()
local std_out = io.popen("sleep 3; echo 'done'", "r")
assert.are.equal(util.getNonBlockingReadSize(std_out), 0)
util.sleep(1)
local std_out = io.popen("sleep 1; echo 'done'; sleep 1", "r")
assert.are.equal(util.getNonBlockingReadSize(std_out), 0)
util.sleep(1)
util.usleep(750000)
assert.are.equal(util.getNonBlockingReadSize(std_out), 0)
util.sleep(2)
util.usleep(750000)
assert.are.equal(util.getNonBlockingReadSize(std_out), 5)
local startsec = util.gettime()
local data = std_out:read("*all")
Expand All @@ -188,36 +198,40 @@ describe("util module", function()
end)
assert.truthy(pid)
assert.is_false(util.isSubProcessDone(pid))
util.sleep(2)
assert.is_true(util.isSubProcessDone(pid))
assert.is_true(waitForSubProcessDone(pid))
assert.is.same(var, "var_set_in_parent")
end)
it("should write a file in subprocess", function()
os.remove("/tmp/test_subprocess1.txt")
local tmp_f = os.tmpname()
local pid = util.runInSubProcess(function()
util.sleep(1)
local f = io.open("/tmp/test_subprocess1.txt", "w")
util.usleep(500000)
local f = io.open(tmp_f, "w")
if f then
f:write("test1")
f:close()
end
end)
assert.truthy(pid)
assert.is_false(util.isSubProcessDone(pid))
util.sleep(2)
assert.is_true(util.isSubProcessDone(pid))
local f = io.open("/tmp/test_subprocess1.txt")
assert.is_true(waitForSubProcessDone(pid))
local f = io.open(tmp_f)
assert.is_not_nil(f)
local data = f:read("*all")
f:close()
assert.is.same(data, "test1")
os.remove("/tmp/test_subprocess1.txt")
os.remove(tmp_f)
end)
it("should not write a file in subprocess as we kill it before", function()
os.remove("/tmp/test_subprocess2.txt")
it("should not modify a file in subprocess as we kill it before", function()
local tmp_f = os.tmpname()
local f
f = io.open(tmp_f, "w")
if f then
f:write("test1")
f:close()
end
local pid = util.runInSubProcess(function()
util.sleep(3)
local f = io.open("/tmp/test_subprocess2.txt", "w")
f = io.open(tmp_f, "w")
if f then
f:write("test2")
f:close()
Expand All @@ -227,26 +241,27 @@ describe("util module", function()
assert.is_false(util.isSubProcessDone(pid))
util.sleep(1)
util.terminateSubProcess(pid)
util.sleep(1)
assert.is_true(util.isSubProcessDone(pid))
local f = io.open("/tmp/test_subprocess2.txt")
os.remove("/tmp/test_subprocess2.txt")
assert.is_nil(f)
assert.is_true(waitForSubProcessDone(pid))
f = io.open(tmp_f)
assert.is_not_nil(f)
local data = f:read("*all")
f:close()
assert.is.same(data, "test1")
os.remove(tmp_f)
end)
it("should read data from subprocess via pipe", function()
local pid, parent_fd = util.runInSubProcess(function(pid, child_fd)
util.sleep(1)
util.usleep(500000)
util.writeToFD(child_fd, "test3", true)
end, true)
assert.truthy(pid)
assert.truthy(parent_fd)
assert.is_false(util.isSubProcessDone(pid))
util.sleep(2)
util.sleep(1)
assert.is_true(util.getNonBlockingReadSize(parent_fd) ~= 0)
local data = util.readAllFromFD(parent_fd, true)
assert.is.same(data, "test3")
util.sleep(1)
assert.is_true(util.isSubProcessDone(pid))
assert.is_true(waitForSubProcessDone(pid))
end)
end)

Expand Down
Loading