diff options
author | Karen Arutyunov <karen@codesynthesis.com> | 2020-10-10 17:22:46 +0300 |
---|---|---|
committer | Karen Arutyunov <karen@codesynthesis.com> | 2020-11-06 19:32:09 +0300 |
commit | f41599c8e9435f3dfec60b872c2b4ae31177efdd (patch) | |
tree | 088f8d9bf906e4a2ed734e034699163c9ccc7306 /libbuild2/test/common.cxx | |
parent | ac76a4fd2afff48a0d5db84592babe5cabef3a2c (diff) |
Add support for test timeouts
Diffstat (limited to 'libbuild2/test/common.cxx')
-rw-r--r-- | libbuild2/test/common.cxx | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/libbuild2/test/common.cxx b/libbuild2/test/common.cxx index f50d289..7fdb347 100644 --- a/libbuild2/test/common.cxx +++ b/libbuild2/test/common.cxx @@ -6,6 +6,10 @@ #include <libbuild2/target.hxx> #include <libbuild2/algorithm.hxx> +#include <libbuild2/script/timeout.hxx> + +#include <libbuild2/test/module.hxx> + using namespace std; namespace build2 @@ -215,5 +219,73 @@ namespace build2 return r; } + + optional<timestamp> common:: + operation_deadline () const + { + if (!operation_timeout) + return nullopt; + + duration::rep r (operation_deadline_.load (memory_order_consume)); + + if (r == timestamp_unknown_rep) + { + duration::rep t (timestamp (system_clock::now () + *operation_timeout). + time_since_epoch ().count ()); + + if (operation_deadline_.compare_exchange_strong (r, + t, + memory_order_release, + memory_order_consume)) + r = t; + } + + return timestamp (duration (r)); + } + + // Helpers. + // + optional<timestamp> + operation_deadline (const target& t) + { + optional<timestamp> r; + + for (const scope* s (t.base_scope ().root_scope ()); + s != nullptr; + s = s->parent_scope ()->root_scope ()) + { + if (auto* m = s->find_module<module> (module::name)) + r = earlier (r, m->operation_deadline ()); + } + + return r; + } + + optional<duration> + test_timeout (const target& t) + { + optional<duration> r; + + for (const scope* s (t.base_scope ().root_scope ()); + s != nullptr; + s = s->parent_scope ()->root_scope ()) + { + if (auto* m = s->find_module<module> (module::name)) + r = earlier (r, m->test_timeout); + } + + return r; + } + + optional<timestamp> + test_deadline (const target& t) + { + optional<timestamp> r (operation_deadline (t)); + + if (optional<duration> d = test_timeout (t)) + r = earlier (r, system_clock::now () + *d); + + return r; + } } } |