-
-
Notifications
You must be signed in to change notification settings - Fork 318
Close #424 - make tests run in coroutine to support asynchronous testing #426
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
local Job = require "plenary.job" | ||
|
||
local Timing = {} | ||
|
||
function Timing:log(name) | ||
self[name] = vim.loop.uptime() | ||
end | ||
|
||
function Timing:check(from, to, min_elapsed) | ||
assert(self[from], "did not log " .. from) | ||
assert(self[to], "did not log " .. to) | ||
local elapsed = self[to] - self[from] | ||
assert( | ||
min_elapsed <= elapsed, | ||
string.format("only took %s to get from %s to %s - expected at least %s", elapsed, from, to, min_elapsed) | ||
) | ||
end | ||
|
||
describe("Async test", function() | ||
it("can resume testing with vim.defer_fn", function() | ||
local co = coroutine.running() | ||
assert(co, "not running inside a coroutine") | ||
|
||
local timing = setmetatable({}, { __index = Timing }) | ||
|
||
vim.defer_fn(function() | ||
coroutine.resume(co) | ||
end, 200) | ||
timing:log "before" | ||
coroutine.yield() | ||
timing:log "after" | ||
timing:check("before", "after", 0.1) | ||
end) | ||
|
||
it("can resume testing from job callback", function() | ||
local co = coroutine.running() | ||
assert(co, "not running inside a coroutine") | ||
|
||
local timing = setmetatable({}, { __index = Timing }) | ||
|
||
Job:new({ | ||
command = "bash", | ||
args = { | ||
"-ce", | ||
[[ | ||
sleep 0.2 | ||
echo hello | ||
sleep 0.2 | ||
echo world | ||
sleep 0.2 | ||
exit 42 | ||
]], | ||
}, | ||
on_stdout = function(_, data) | ||
timing:log(data) | ||
end, | ||
on_exit = function(_, exit_status) | ||
timing:log "exit" | ||
--This is required so that the rest of the test will run in a proper context | ||
vim.schedule(function() | ||
coroutine.resume(co, exit_status) | ||
end) | ||
end, | ||
}):start() | ||
timing:log "job started" | ||
local exit_status = coroutine.yield() | ||
timing:log "job finished" | ||
assert.are.equal(exit_status, 42) | ||
|
||
timing:check("job started", "job finished", 0.3) | ||
timing:check("job started", "hello", 0.1) | ||
timing:check("hello", "world", 0.1) | ||
timing:check("world", "job finished", 0.1) | ||
end) | ||
end) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not just use async.util.sleep() (inside a.it)
Is there any more complex use case that justify this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
async.util.sleep
anda.it
are doing more or less the same behind the scenes. The point of this minimal example was to demonstrate a resuming a coroutine from a callback - something which would usually include things like autocommands or event handlers on Neovim jobs. But these are a bit more complex to set up and activate, so to keep the example simple I used a simpledefer_fn
.