Skip to content

Commit

Permalink
Add set_timeout method
Browse files Browse the repository at this point in the history
  • Loading branch information
mah0x211 committed Mar 28, 2024
1 parent 0d9dc97 commit 67bb8a6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
8 changes: 8 additions & 0 deletions reader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
local find = string.find
local match = string.match
local sub = string.sub
local type = type
local isfile = require('io.isfile')
local fopen = require('io.fopen')
local fileno = require('io.fileno')
Expand Down Expand Up @@ -57,6 +58,13 @@ function Reader:getfd()
return self.fd
end

--- set_timeout
--- @param sec? number
function Reader:set_timeout(sec)
assert(sec == nil or type(sec) == 'number', 'sec must be number or nil')
self.waitsec = sec
end

--- close
--- @return boolean ok
--- @return any err
Expand Down
14 changes: 14 additions & 0 deletions test/reader_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,16 @@ function testcase.read_with_timeout()
assert.is_true(again)
assert.is_true(t >= .5 and t < .6)

-- test change timeout to 0.1 second
r:set_timeout(.1)
t = gettime()
s, err, again = r:read()
t = gettime() - t
assert.is_nil(err)
assert.is_nil(s)
assert.is_true(again)
assert.is_true(t >= .1 and t < .2)

-- test that read line from pipe
pw:write('hello\nio-reader\nworld!\n')
s, err, again = r:read()
Expand All @@ -186,6 +196,10 @@ function testcase.read_with_timeout()
assert.is_nil(s)
assert.is_nil(err)
assert.is_nil(again)

-- test that throws an error if invalid sec argument
err = assert.throws(r.set_timeout, r, true)
assert.match(err, 'sec must be number or nil')
end

function testcase.close()
Expand Down

0 comments on commit 67bb8a6

Please sign in to comment.