Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: test/BaseJsTest.h

Issue 29367522: Issue #4688, #3595 - Web request use scheduled threads; unit tests terminate
Patch Set: Created Dec. 14, 2016, 8:38 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: test/BaseJsTest.h
===================================================================
--- a/test/BaseJsTest.h
+++ b/test/BaseJsTest.h
@@ -18,6 +18,8 @@
#ifndef MOCKS_H
#define MOCKS_H
+#include <condition_variable>
+#include <mutex>
#include <thread>
#include <AdblockPlus.h>
@@ -119,15 +121,49 @@
}
};
+class LazyWaiter
+{
+ std::mutex m;
+ typedef std::unique_lock<std::mutex> UniqueLockType;
+ std::condition_variable cv;
+ bool running;
+
+public:
+ LazyWaiter()
+ : running(true)
+ {}
+
+ void WaitUntilCancelled()
+ {
+ UniqueLockType ul(m);
+ cv.wait(ul, [this]() -> bool { return !running; });
+ }
+
+ void Cancel()
+ {
+ UniqueLockType ul(m);
+ running = false;
+ cv.notify_all();
+ }
+};
+
class LazyWebRequest : public AdblockPlus::WebRequest
{
+ mutable LazyWaiter w;
Eric 2016/12/14 20:53:55 Declared mutable because `GET()` is declared `cons
+
public:
+ LazyWebRequest() {} // = default;
+
AdblockPlus::ServerResponse GET(const std::string& url, const AdblockPlus::HeaderList& requestHeaders) const
{
- while (true)
- std::this_thread::sleep_for(std::chrono::seconds(100));
+ w.WaitUntilCancelled();
return AdblockPlus::ServerResponse();
}
+
+ void Cancel()
+ {
+ w.Cancel();
+ }
};
class LazyLogSystem : public AdblockPlus::LogSystem
« no previous file with comments | « src/WebRequestJsObject.cpp ('k') | test/FilterEngine.cpp » ('j') | test/Prefs.cpp » ('J')

Powered by Google App Engine
This is Rietveld