Commit 39b37a56 authored by Yutaka Hirano's avatar Yutaka Hirano Committed by Commit Bot

Remove unused test utilities from DataConsumerHandleTestUtil

Bug: 894819
Change-Id: I9d1af51eff828564aa748742355e84695b6a1de7
Reviewed-on: https://chromium-review.googlesource.com/c/1442340
Commit-Queue: Yutaka Hirano <yhirano@chromium.org>
Reviewed-by: default avatarMakoto Shimazu <shimazu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#626944}
parent 97ac6fc2
...@@ -12,90 +12,6 @@ ...@@ -12,90 +12,6 @@
namespace blink { namespace blink {
namespace {
class WaitingHandle final : public WebDataConsumerHandle {
private:
class ReaderImpl final : public WebDataConsumerHandle::Reader {
public:
WebDataConsumerHandle::Result BeginRead(const void** buffer,
WebDataConsumerHandle::Flags,
size_t* available) override {
*available = 0;
*buffer = nullptr;
return kShouldWait;
}
WebDataConsumerHandle::Result EndRead(size_t) override {
return kUnexpectedError;
}
};
std::unique_ptr<Reader> ObtainReader(
Client*,
scoped_refptr<base::SingleThreadTaskRunner>) override {
return std::make_unique<ReaderImpl>();
}
const char* DebugName() const override { return "WaitingHandle"; }
};
} // namespace
DataConsumerHandleTestUtil::Thread::Thread(
const ThreadCreationParams& params,
InitializationPolicy initialization_policy)
: thread_(WebThreadSupportingGC::Create(params)),
initialization_policy_(initialization_policy),
waitable_event_(std::make_unique<WaitableEvent>()) {
thread_->PostTask(FROM_HERE, CrossThreadBind(&Thread::Initialize,
CrossThreadUnretained(this)));
waitable_event_->Wait();
}
DataConsumerHandleTestUtil::Thread::~Thread() {
thread_->PostTask(FROM_HERE, CrossThreadBind(&Thread::Shutdown,
CrossThreadUnretained(this)));
waitable_event_->Wait();
}
void DataConsumerHandleTestUtil::Thread::Initialize() {
DCHECK(thread_->IsCurrentThread());
if (initialization_policy_ >= kScriptExecution) {
isolate_holder_ = std::make_unique<gin::IsolateHolder>(
scheduler::GetSingleThreadTaskRunnerForTesting(),
gin::IsolateHolder::IsolateType::kTest);
GetIsolate()->Enter();
}
thread_->InitializeOnThread();
if (initialization_policy_ >= kScriptExecution) {
v8::HandleScope handle_scope(GetIsolate());
script_state_ = ScriptState::Create(
v8::Context::New(GetIsolate()),
DOMWrapperWorld::Create(GetIsolate(),
DOMWrapperWorld::WorldType::kTesting));
}
if (initialization_policy_ >= kWithExecutionContext) {
execution_context_ = MakeGarbageCollected<NullExecutionContext>();
}
waitable_event_->Signal();
}
void DataConsumerHandleTestUtil::Thread::Shutdown() {
DCHECK(thread_->IsCurrentThread());
execution_context_ = nullptr;
if (script_state_) {
script_state_->DisposePerContextData();
}
script_state_ = nullptr;
thread_->ShutdownOnThread();
if (isolate_holder_) {
GetIsolate()->Exit();
GetIsolate()->RequestGarbageCollectionForTesting(
GetIsolate()->kFullGarbageCollection);
isolate_holder_ = nullptr;
}
waitable_event_->Signal();
}
class DataConsumerHandleTestUtil::ReplayingHandle::ReaderImpl final class DataConsumerHandleTestUtil::ReplayingHandle::ReaderImpl final
: public Reader { : public Reader {
public: public:
...@@ -264,9 +180,4 @@ void DataConsumerHandleTestUtil::ReplayingHandle::Add(const Command& command) { ...@@ -264,9 +180,4 @@ void DataConsumerHandleTestUtil::ReplayingHandle::Add(const Command& command) {
context_->Add(command); context_->Add(command);
} }
std::unique_ptr<WebDataConsumerHandle>
DataConsumerHandleTestUtil::CreateWaitingDataConsumerHandle() {
return std::make_unique<WaitingHandle>();
}
} // namespace blink } // namespace blink
...@@ -34,306 +34,6 @@ class DataConsumerHandleTestUtil { ...@@ -34,306 +34,6 @@ class DataConsumerHandleTestUtil {
STATIC_ONLY(DataConsumerHandleTestUtil); STATIC_ONLY(DataConsumerHandleTestUtil);
public: public:
class NoopClient final : public WebDataConsumerHandle::Client {
DISALLOW_NEW();
public:
void DidGetReadable() override {}
};
// Thread has a WebThreadSupportingGC. It initializes / shutdowns
// additional objects based on the given policy. The constructor and the
// destructor blocks during the setup and the teardown.
class Thread final {
USING_FAST_MALLOC(Thread);
public:
// Initialization policy of a thread.
enum InitializationPolicy {
// Only garbage collection is supported.
kGarbageCollection,
// Creating an isolate in addition to GarbageCollection.
kScriptExecution,
// Creating an execution context in addition to ScriptExecution.
kWithExecutionContext,
};
Thread(const ThreadCreationParams&,
InitializationPolicy = kGarbageCollection);
~Thread();
WebThreadSupportingGC* GetThread() { return thread_.get(); }
ExecutionContext* GetExecutionContext() { return execution_context_.Get(); }
ScriptState* GetScriptState() { return script_state_; }
v8::Isolate* GetIsolate() { return isolate_holder_->isolate(); }
private:
void Initialize();
void Shutdown();
std::unique_ptr<WebThreadSupportingGC> thread_;
const InitializationPolicy initialization_policy_;
std::unique_ptr<WaitableEvent> waitable_event_;
Persistent<NullExecutionContext> execution_context_;
std::unique_ptr<gin::IsolateHolder> isolate_holder_;
Persistent<ScriptState> script_state_;
};
class ThreadingTestBase : public ThreadSafeRefCounted<ThreadingTestBase> {
public:
virtual ~ThreadingTestBase() {}
class ThreadHolder;
class Context : public ThreadSafeRefCounted<Context> {
public:
static scoped_refptr<Context> Create() {
return base::AdoptRef(new Context);
}
void RecordAttach(const String& handle) {
MutexLocker locker(logging_mutex_);
result_.Append("A reader is attached to ");
result_.Append(handle);
result_.Append(" on ");
result_.Append(CurrentThreadName());
result_.Append(".\n");
}
void RecordDetach(const String& handle) {
MutexLocker locker(logging_mutex_);
result_.Append("A reader is detached from ");
result_.Append(handle);
result_.Append(" on ");
result_.Append(CurrentThreadName());
result_.Append(".\n");
}
String Result() {
MutexLocker locker(logging_mutex_);
return result_.ToString();
}
void RegisterThreadHolder(ThreadHolder* holder) {
MutexLocker locker(holder_mutex_);
DCHECK(!holder_);
holder_ = holder;
}
void UnregisterThreadHolder() {
MutexLocker locker(holder_mutex_);
DCHECK(holder_);
holder_ = nullptr;
}
void PostTaskToReadingThread(const base::Location& location,
CrossThreadClosure task) {
MutexLocker locker(holder_mutex_);
DCHECK(holder_);
holder_->ReadingThread()->PostTask(location, std::move(task));
}
void PostTaskToUpdatingThread(const base::Location& location,
CrossThreadClosure task) {
MutexLocker locker(holder_mutex_);
DCHECK(holder_);
holder_->UpdatingThread()->PostTask(location, std::move(task));
}
private:
Context() : holder_(nullptr) {}
String CurrentThreadName() {
MutexLocker locker(holder_mutex_);
if (holder_) {
if (holder_->ReadingThread()->IsCurrentThread())
return "the reading thread";
if (holder_->UpdatingThread()->IsCurrentThread())
return "the updating thread";
}
return "an unknown thread";
}
// Protects |m_result|.
Mutex logging_mutex_;
StringBuilder result_;
// Protects |m_holder|.
Mutex holder_mutex_;
// Because Context outlives ThreadHolder, holding a raw pointer
// here is safe.
ThreadHolder* holder_;
};
// The reading/updating threads are alive while ThreadHolder is alive.
class ThreadHolder {
DISALLOW_NEW();
public:
ThreadHolder(ThreadingTestBase* test)
: context_(test->context_),
reading_thread_(std::make_unique<Thread>(
ThreadCreationParams(WebThreadType::kTestThread)
.SetThreadNameForTest("reading thread"))),
updating_thread_(std::make_unique<Thread>(
ThreadCreationParams(WebThreadType::kTestThread)
.SetThreadNameForTest("updating thread"))) {
context_->RegisterThreadHolder(this);
}
~ThreadHolder() { context_->UnregisterThreadHolder(); }
WebThreadSupportingGC* ReadingThread() {
return reading_thread_->GetThread();
}
WebThreadSupportingGC* UpdatingThread() {
return updating_thread_->GetThread();
}
private:
scoped_refptr<Context> context_;
std::unique_ptr<Thread> reading_thread_;
std::unique_ptr<Thread> updating_thread_;
};
class ReaderImpl final : public WebDataConsumerHandle::Reader {
USING_FAST_MALLOC(ReaderImpl);
public:
ReaderImpl(const String& name, scoped_refptr<Context> context)
: name_(name.IsolatedCopy()), context_(std::move(context)) {
context_->RecordAttach(name_.IsolatedCopy());
}
~ReaderImpl() override { context_->RecordDetach(name_.IsolatedCopy()); }
using Result = WebDataConsumerHandle::Result;
using Flags = WebDataConsumerHandle::Flags;
Result BeginRead(const void**, Flags, size_t*) override {
return WebDataConsumerHandle::kShouldWait;
}
Result EndRead(size_t) override {
return WebDataConsumerHandle::kUnexpectedError;
}
private:
const String name_;
scoped_refptr<Context> context_;
};
class DataConsumerHandle final : public WebDataConsumerHandle {
USING_FAST_MALLOC(DataConsumerHandle);
public:
static std::unique_ptr<WebDataConsumerHandle> Create(
const String& name,
scoped_refptr<Context> context) {
return base::WrapUnique(
new DataConsumerHandle(name, std::move(context)));
}
private:
DataConsumerHandle(const String& name, scoped_refptr<Context> context)
: name_(name.IsolatedCopy()), context_(std::move(context)) {}
std::unique_ptr<Reader> ObtainReader(
Client*,
scoped_refptr<base::SingleThreadTaskRunner> task_runner) override {
return std::make_unique<ReaderImpl>(name_, context_);
}
const char* DebugName() const override {
return "ThreadingTestBase::DataConsumerHandle";
}
const String name_;
scoped_refptr<Context> context_;
};
void ResetReader() { reader_ = nullptr; }
void SignalDone() { waitable_event_->Signal(); }
String Result() { return context_->Result(); }
void PostTaskToReadingThread(const base::Location& location,
CrossThreadClosure task) {
context_->PostTaskToReadingThread(location, std::move(task));
}
void PostTaskToUpdatingThread(const base::Location& location,
CrossThreadClosure task) {
context_->PostTaskToUpdatingThread(location, std::move(task));
}
void PostTaskToReadingThreadAndWait(const base::Location& location,
CrossThreadClosure task) {
PostTaskToReadingThread(location, std::move(task));
waitable_event_->Wait();
}
void PostTaskToUpdatingThreadAndWait(const base::Location& location,
CrossThreadClosure task) {
PostTaskToUpdatingThread(location, std::move(task));
waitable_event_->Wait();
}
protected:
ThreadingTestBase() : context_(Context::Create()) {}
scoped_refptr<Context> context_;
std::unique_ptr<WebDataConsumerHandle::Reader> reader_;
std::unique_ptr<WaitableEvent> waitable_event_;
NoopClient client_;
};
class ThreadingHandleNotificationTest : public ThreadingTestBase,
public WebDataConsumerHandle::Client {
public:
using Self = ThreadingHandleNotificationTest;
static scoped_refptr<Self> Create() { return base::AdoptRef(new Self); }
void Run(std::unique_ptr<WebDataConsumerHandle> handle) {
ThreadHolder holder(this);
waitable_event_ = std::make_unique<WaitableEvent>();
handle_ = std::move(handle);
PostTaskToReadingThreadAndWait(
FROM_HERE,
CrossThreadBind(&Self::ObtainReader, WrapRefCounted(this)));
}
private:
ThreadingHandleNotificationTest() = default;
void ObtainReader() {
reader_ = handle_->ObtainReader(
this, scheduler::GetSingleThreadTaskRunnerForTesting());
}
void DidGetReadable() override {
PostTaskToReadingThread(
FROM_HERE, CrossThreadBind(&Self::ResetReader, WrapRefCounted(this)));
PostTaskToReadingThread(
FROM_HERE, CrossThreadBind(&Self::SignalDone, WrapRefCounted(this)));
}
std::unique_ptr<WebDataConsumerHandle> handle_;
};
class ThreadingHandleNoNotificationTest
: public ThreadingTestBase,
public WebDataConsumerHandle::Client {
public:
using Self = ThreadingHandleNoNotificationTest;
static scoped_refptr<Self> Create() { return base::AdoptRef(new Self); }
void Run(std::unique_ptr<WebDataConsumerHandle> handle) {
ThreadHolder holder(this);
waitable_event_ = std::make_unique<WaitableEvent>();
handle_ = std::move(handle);
PostTaskToReadingThreadAndWait(
FROM_HERE,
CrossThreadBind(&Self::ObtainReader, WrapRefCounted(this)));
}
private:
ThreadingHandleNoNotificationTest() = default;
void ObtainReader() {
reader_ = handle_->ObtainReader(
this, scheduler::GetSingleThreadTaskRunnerForTesting());
reader_ = nullptr;
PostTaskToReadingThread(
FROM_HERE, CrossThreadBind(&Self::SignalDone, WrapRefCounted(this)));
}
void DidGetReadable() override { NOTREACHED(); }
std::unique_ptr<WebDataConsumerHandle> handle_;
};
class Command final { class Command final {
DISALLOW_NEW(); DISALLOW_NEW();
...@@ -423,9 +123,6 @@ class DataConsumerHandleTestUtil { ...@@ -423,9 +123,6 @@ class DataConsumerHandleTestUtil {
scoped_refptr<Context> context_; scoped_refptr<Context> context_;
}; };
static std::unique_ptr<WebDataConsumerHandle>
CreateWaitingDataConsumerHandle();
}; };
} // namespace blink } // namespace blink
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment