Add dlog wait function to batch mode 45/206045/3
authorRadoslaw Cybulski <r.cybulski@partner.samsung.com>
Mon, 13 May 2019 13:09:41 +0000 (15:09 +0200)
committerRadoslaw Cybulski <r.cybulski@partner.samsung.com>
Thu, 16 May 2019 09:31:20 +0000 (11:31 +0200)
Change-Id: Iafb018ed236811faa413d5c5cba7556aa72406df

src/batch/BatchRunner.cpp
src/batch/BatchRunner.hpp

index 2c4d7e4..7bf09aa 100644 (file)
@@ -922,19 +922,50 @@ protected:
        bool success = false;
 };
 
+class DlogTTS : public PredicateWaitInterface
+{
+public:
+       DlogTTS(BatchExecutor *executor, std::chrono::milliseconds timeout, std::string pattern)
+               : PredicateWaitInterface(executor, timeout)
+       {
+               {
+                       auto h = self->dlogInfo.lock();
+                       h->searchLine = std::move(pattern);
+                       h->mode = BatchExecutor::DLogInfo::Mode::search;
+               }
+       }
+
+private:
+       bool predicate() override
+       {
+               auto h = self->dlogInfo.lock();
+               auto res = h.waitForCondition(timeout, [&]() {
+                       return h->mode == BatchExecutor::DLogInfo::Mode::found;
+               });
+
+               h->mode = BatchExecutor::DLogInfo::Mode::ignore;
+               if (!res)
+                       throw EvaluationFailure{} << "wait for dlog ('" << pattern << "'): operation timeouted";
+
+               return res;
+       }
+
+       std::string pattern;
+};
+
 class WaitTTS : public PredicateWaitInterface
 {
 public:
        WaitTTS(BatchExecutor *executor, std::chrono::milliseconds timeout, std::string pattern)
                : PredicateWaitInterface(executor, timeout)
        {
+               this->pattern = pattern;
                {
                        auto h = self->ttsInfo.lock();
                        if (!pattern.empty())
                                h->searchLine = std::move(pattern);
                        h->mode = BatchExecutor::TTSInfo::Mode::search;
                }
-               this->pattern = std::move(pattern);
        }
 
 private:
@@ -945,11 +976,9 @@ private:
                        return h->mode == BatchExecutor::TTSInfo::Mode::found;
                });
 
-               if (!res) {
-                       h->mode = BatchExecutor::TTSInfo::Mode::ignore;
-                       throw EvaluationFailure{} << "wait for dlog ('" << pattern << "'): operation timeouted";
-               }
                h->mode = BatchExecutor::TTSInfo::Mode::ignore;
+               if (!res)
+                       throw EvaluationFailure{} << "wait for tts ('" << pattern << "'): operation timeouted";
 
                return res;
        }
@@ -1004,6 +1033,12 @@ private:
 
 void BatchExecutor::insertWaits()
 {
+       auto dlogTTS = [&](std::string pattern, double timeout) -> EvaluationValue {
+               auto impl = std::make_shared<DlogTTS>(this, std::chrono::milliseconds{ static_cast<size_t>(timeout * 1000) }, std::move(pattern));
+               return EvaluationValue{ impl };
+       };
+       variables["dlog"] = EvaluationValueFunction{ std::move(dlogTTS), { { "pattern" }, { "timeout", 5.0 } } };
+
        auto waitTTS = [&](std::string pattern, double timeout) -> EvaluationValue {
                auto impl = std::make_shared<WaitTTS>(this, std::chrono::milliseconds{ static_cast<size_t>(timeout * 1000) }, std::move(pattern));
                return EvaluationValue{ impl };
@@ -1322,6 +1357,14 @@ static void threadFunc(StatPtr result, std::unique_ptr<BatchExecutor> exec, std:
                                }
                        }
                });
+               auto dlogHandler = dlog.registerCallback([&exec](const std::string & txt) {
+                       auto h = exec->dlogInfo.lock();
+                       if (h->mode == BatchExecutor::DLogInfo::Mode::search) {
+                               if (txt.find(h->searchLine) != std::string::npos) {
+                                       h->mode = BatchExecutor::DLogInfo::Mode::found;
+                               }
+                       }
+               });
                exec->outputStream() << "evaluation started\n";
                try {
                        result->evaluate();
index 9baa738..98703a9 100644 (file)
@@ -71,7 +71,12 @@ public:
        struct TTSInfo {
                Optional<std::string> searchLine;
                enum class Mode { ignore, search, found };
-               Mode mode;
+               Mode mode = Mode::ignore;
+       };
+       struct DLogInfo {
+               std::string searchLine;
+               enum class Mode { ignore, search, found };
+               Mode mode = Mode::ignore;
        };
 
        // NOTE: constructor of TestExecutor must be called on main thread
@@ -92,6 +97,7 @@ public:
 
        Monitor<TTSInfo> ttsInfo;
        Monitor<ContextInfo> contextInfo;
+       Monitor<DLogInfo> dlogInfo;
        std::unordered_map<std::string, EvaluationValue> variables;
 
 protected: