From f41599c8e9435f3dfec60b872c2b4ae31177efdd Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Sat, 10 Oct 2020 17:22:46 +0300 Subject: Add support for test timeouts --- libbuild2/test/common.cxx | 72 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) (limited to 'libbuild2/test/common.cxx') 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 #include +#include + +#include + using namespace std; namespace build2 @@ -215,5 +219,73 @@ namespace build2 return r; } + + optional 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 + operation_deadline (const target& t) + { + optional 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::name)) + r = earlier (r, m->operation_deadline ()); + } + + return r; + } + + optional + test_timeout (const target& t) + { + optional 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::name)) + r = earlier (r, m->test_timeout); + } + + return r; + } + + optional + test_deadline (const target& t) + { + optional r (operation_deadline (t)); + + if (optional d = test_timeout (t)) + r = earlier (r, system_clock::now () + *d); + + return r; + } } } -- cgit v1.1