From 2b935b87e4525e4948a341bbf9a3ff439eac22fb Mon Sep 17 00:00:00 2001 From: Rahix Date: Wed, 17 Apr 2019 12:49:37 +0200 Subject: [PATCH] Add timeout to call_sync Signed-off-by: Rahix --- README.md | 4 ++-- bc.lua | 11 ++++++++--- tests/timeout.lua | 2 +- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 2c98c4048709..d5faa9c15dea 100644 --- a/README.md +++ b/README.md @@ -146,7 +146,7 @@ bc:finalize() bc:call("some_verb", "param_a_value", "param_b_value") ``` -### `BaseControl:call_sync(verb, timeout, ...)` +### `BaseControl:call_sync([timeout], verb, ...)` Call a verb **synchroneously**. All parameters following `verb` will be given to the remote function. `call_sync` will return the remote function's return value. If timeout is not `nil` or `0`, `call_sync` will error upon @@ -161,7 +161,7 @@ end) bc:finalize() -- This call can either happen locally or on another node -local res = bc:call_sync("stupid_add", nil, 12, 34) +local res = bc:call_sync("stupid_add", 12, 34) print("12 + 34 = "..res) ``` diff --git a/bc.lua b/bc.lua index d8d833cd3d41..efc344fc2ccd 100644 --- a/bc.lua +++ b/bc.lua @@ -352,14 +352,19 @@ function BaseControl:call(verb, ...) end end -function BaseControl:call_sync(verb, ...) +function BaseControl:call_sync(timeout, verb, ...) + local param = {...} + if type(timeout) ~= "number" then + table.insert(param, 1, verb) + timeout, verb = nil, timeout + end if self.local_verbs[verb] ~= nil then - return self.local_verbs[verb](...) + return self.local_verbs[verb](table.unpack(param)) elseif self.remote_verbs[verb] ~= nil then self.network:send(self.remote_verbs[verb], { ty=Message.VerbRequest, verb=verb, - param={...}, + param=param, sync=true, }) local answer = self.network:pull(function(remote, msg) diff --git a/tests/timeout.lua b/tests/timeout.lua index 952224430ae0..627dd45d6d78 100644 --- a/tests/timeout.lua +++ b/tests/timeout.lua @@ -43,7 +43,7 @@ function test_timeout_call() ) assert_error_match("timeout", function() - bc:call_sync("toc1v", 0.1) + bc:call_sync(0.1, "toc1v") end) end