diff options
author | darin@chromium.org <darin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-01-14 16:27:51 +0000 |
---|---|---|
committer | darin@chromium.org <darin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-01-14 16:27:51 +0000 |
commit | 0d37563e4df3e98c429f3bd71816451becbe06d7 (patch) | |
tree | 8724e2770a1eb259d8ffd267e8dc7a587098e59b /mojo/public/tests | |
parent | 0d55b916994bb03c4b2b5ecac90ad77c1124c453 (diff) | |
download | chromium_src-0d37563e4df3e98c429f3bd71816451becbe06d7.zip chromium_src-0d37563e4df3e98c429f3bd71816451becbe06d7.tar.gz chromium_src-0d37563e4df3e98c429f3bd71816451becbe06d7.tar.bz2 |
Mojo: AsyncWaiter and mojo/public/environment
Summary of changes:
o BindingsSupport is gone:
- mojo/public/bindings/lib depends on mojo/public/environment/,
which is also a static library.
- mojo/public/environment provides a default implementation of
MojoAsyncWaiter (replacing the AsyncWait functionality of
BindingsSupport).
- mojo/public/environment provides TLS support for storing the
current Buffer* (replacing the Set/GetCurrentBuffer functionality
of BindingsSupport).
- mojo/public/environment provides the Environment class, formerly
part of mojo/public/utility/
- The standalone implementation of mojo/public/environment/ depends
on mojo/public/utility/ and assumes clients will be instantiating
RunLoops on their threads.
- The chromium-specific implementation of mojo/public/environment/
depends on mojo/common/ and assumes clients will be instantiating
MessageLoops on their threads.
- The chromium-specific implementation of mojo/public/environment/
is divided into two targets: mojo_environment_chromium and
mojo_environment_chromium_impl. The former is a static library and
the latter is a component. (This way all of the state--TLS keys--
associated with the environment is kept in a DSO when using a
component build.)
o RemotePtr and Connector may optionally be parameterized with a
MojoAsyncWaiter*, allowing users to customize how AsyncWait is
implemented for a particular usage of bindings. This is needed by
the GL library so that it can schedule work on an application
defined run loop.
o RunLoop gains a RunUntilIdle method to support tests. This allows us
to delete SimpleBindingsSupport instead of converting it over to an
implementation of MojoAsyncWaiter.
Review URL: https://codereview.chromium.org/134253004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@244739 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'mojo/public/tests')
-rw-r--r-- | mojo/public/tests/bindings/array_unittest.cc | 8 | ||||
-rw-r--r-- | mojo/public/tests/bindings/buffer_unittest.cc | 10 | ||||
-rw-r--r-- | mojo/public/tests/bindings/connector_unittest.cc | 9 | ||||
-rw-r--r-- | mojo/public/tests/bindings/handle_passing_unittest.cc | 14 | ||||
-rw-r--r-- | mojo/public/tests/bindings/remote_ptr_unittest.cc | 8 | ||||
-rw-r--r-- | mojo/public/tests/bindings/sample_service_unittests.cc | 4 | ||||
-rw-r--r-- | mojo/public/tests/bindings/simple_bindings_support.cc | 101 | ||||
-rw-r--r-- | mojo/public/tests/bindings/simple_bindings_support.h | 51 | ||||
-rw-r--r-- | mojo/public/tests/bindings/type_conversion_unittest.cc | 4 | ||||
-rw-r--r-- | mojo/public/tests/environment/async_waiter_unittest.cc (renamed from mojo/public/tests/utility/bindings_support_impl_unittest.cc) | 66 |
10 files changed, 73 insertions, 202 deletions
diff --git a/mojo/public/tests/bindings/array_unittest.cc b/mojo/public/tests/bindings/array_unittest.cc index bb46c67..dac4b50 100644 --- a/mojo/public/tests/bindings/array_unittest.cc +++ b/mojo/public/tests/bindings/array_unittest.cc @@ -3,7 +3,7 @@ // found in the LICENSE file. #include "mojo/public/bindings/lib/array.h" -#include "mojo/public/tests/bindings/simple_bindings_support.h" +#include "mojo/public/environment/environment.h" #include "testing/gtest/include/gtest/gtest.h" namespace mojo { @@ -11,7 +11,7 @@ namespace test { // Tests that basic Array operations work. TEST(ArrayTest, Basic) { - SimpleBindingsSupport bindings_support; + Environment env; // 8 bytes for the array, with 8 bytes left over for elements. internal::FixedBuffer buf(8 + 8*sizeof(char)); @@ -34,7 +34,7 @@ TEST(ArrayTest, Basic) { // Tests that basic Array<bool> operations work, and that it's packed into 1 // bit per element. TEST(ArrayTest, Bool) { - SimpleBindingsSupport bindings_support; + Environment env; // 8 bytes for the array header, with 8 bytes left over for elements. internal::FixedBuffer buf(8 + 3); @@ -57,7 +57,7 @@ TEST(ArrayTest, Bool) { // Tests that Array<Handle> supports transferring handles. TEST(ArrayTest, Handle) { - SimpleBindingsSupport bindings_support; + Environment env; AllocationScope scope; diff --git a/mojo/public/tests/bindings/buffer_unittest.cc b/mojo/public/tests/bindings/buffer_unittest.cc index ab181505..06cb4ca 100644 --- a/mojo/public/tests/bindings/buffer_unittest.cc +++ b/mojo/public/tests/bindings/buffer_unittest.cc @@ -4,7 +4,7 @@ #include "mojo/public/bindings/lib/bindings_serialization.h" #include "mojo/public/bindings/lib/buffer.h" -#include "mojo/public/tests/bindings/simple_bindings_support.h" +#include "mojo/public/environment/environment.h" #include "testing/gtest/include/gtest/gtest.h" namespace mojo { @@ -21,7 +21,7 @@ bool IsZero(void* p_buf, size_t size) { // Tests small and large allocations in ScratchBuffer. TEST(ScratchBufferTest, Basic) { - SimpleBindingsSupport bindings_support; + Environment env; // Test that a small allocation is placed on the stack. internal::ScratchBuffer buf; @@ -42,7 +42,7 @@ TEST(ScratchBufferTest, Basic) { // Tests that Buffer::current() returns the correct value. TEST(ScratchBufferTest, Stacked) { - SimpleBindingsSupport bindings_support; + Environment env; EXPECT_FALSE(Buffer::current()); @@ -61,7 +61,7 @@ TEST(ScratchBufferTest, Stacked) { // Tests that FixedBuffer allocates memory aligned to 8 byte boundaries. TEST(FixedBufferTest, Alignment) { - SimpleBindingsSupport bindings_support; + Environment env; internal::FixedBuffer buf(internal::Align(10) * 2); ASSERT_EQ(buf.size(), 16u * 2); @@ -81,7 +81,7 @@ TEST(FixedBufferTest, Alignment) { // Tests that FixedBuffer::Leak passes ownership to the caller. TEST(FixedBufferTest, Leak) { - SimpleBindingsSupport bindings_support; + Environment env; void* ptr = NULL; void* buf_ptr = NULL; diff --git a/mojo/public/tests/bindings/connector_unittest.cc b/mojo/public/tests/bindings/connector_unittest.cc index 4e1feba..a814ffc 100644 --- a/mojo/public/tests/bindings/connector_unittest.cc +++ b/mojo/public/tests/bindings/connector_unittest.cc @@ -5,11 +5,11 @@ #include <stdlib.h> #include <string.h> -#include "mojo/public/bindings/lib/bindings_support.h" #include "mojo/public/bindings/lib/connector.h" #include "mojo/public/bindings/lib/message_queue.h" +#include "mojo/public/environment/environment.h" #include "mojo/public/system/macros.h" -#include "mojo/public/tests/bindings/simple_bindings_support.h" +#include "mojo/public/utility/run_loop.h" #include "testing/gtest/include/gtest/gtest.h" namespace mojo { @@ -59,7 +59,7 @@ class ConnectorTest : public testing::Test { } void PumpMessages() { - bindings_support_.Process(); + loop_.RunUntilIdle(); } protected: @@ -67,7 +67,8 @@ class ConnectorTest : public testing::Test { ScopedMessagePipeHandle handle1_; private: - SimpleBindingsSupport bindings_support_; + Environment env_; + RunLoop loop_; }; TEST_F(ConnectorTest, Basic) { diff --git a/mojo/public/tests/bindings/handle_passing_unittest.cc b/mojo/public/tests/bindings/handle_passing_unittest.cc index 21b26f2..ca2d2709 100644 --- a/mojo/public/tests/bindings/handle_passing_unittest.cc +++ b/mojo/public/tests/bindings/handle_passing_unittest.cc @@ -3,8 +3,9 @@ // found in the LICENSE file. #include "mojo/public/bindings/lib/remote_ptr.h" -#include "mojo/public/tests/bindings/simple_bindings_support.h" +#include "mojo/public/environment/environment.h" #include "mojo/public/tests/test_support.h" +#include "mojo/public/utility/run_loop.h" #include "mojom/sample_factory.h" #include "testing/gtest/include/gtest/gtest.h" @@ -125,17 +126,18 @@ class SampleFactoryClientImpl : public sample::FactoryClient { } // namespace -class BindingsHandlePassingTest : public testing::Test { +class HandlePassingTest : public testing::Test { public: void PumpMessages() { - bindings_support_.Process(); + loop_.RunUntilIdle(); } private: - SimpleBindingsSupport bindings_support_; + Environment env_; + RunLoop loop_; }; -TEST_F(BindingsHandlePassingTest, Basic) { +TEST_F(HandlePassingTest, Basic) { ScopedMessagePipeHandle pipe0; ScopedMessagePipeHandle pipe1; CreateMessagePipe(&pipe0, &pipe1); @@ -152,7 +154,7 @@ TEST_F(BindingsHandlePassingTest, Basic) { EXPECT_TRUE(factory_client.got_response()); } -TEST_F(BindingsHandlePassingTest, PassInvalid) { +TEST_F(HandlePassingTest, PassInvalid) { ScopedMessagePipeHandle pipe0; ScopedMessagePipeHandle pipe1; CreateMessagePipe(&pipe0, &pipe1); diff --git a/mojo/public/tests/bindings/remote_ptr_unittest.cc b/mojo/public/tests/bindings/remote_ptr_unittest.cc index d990bce..f4f3b23 100644 --- a/mojo/public/tests/bindings/remote_ptr_unittest.cc +++ b/mojo/public/tests/bindings/remote_ptr_unittest.cc @@ -3,7 +3,8 @@ // found in the LICENSE file. #include "mojo/public/bindings/lib/remote_ptr.h" -#include "mojo/public/tests/bindings/simple_bindings_support.h" +#include "mojo/public/environment/environment.h" +#include "mojo/public/utility/run_loop.h" #include "mojom/math_calculator.h" #include "testing/gtest/include/gtest/gtest.h" @@ -89,7 +90,7 @@ class RemotePtrTest : public testing::Test { } void PumpMessages() { - bindings_support_.Process(); + loop_.RunUntilIdle(); } protected: @@ -97,7 +98,8 @@ class RemotePtrTest : public testing::Test { ScopedMessagePipeHandle pipe1_; private: - SimpleBindingsSupport bindings_support_; + Environment env_; + RunLoop loop_; }; TEST_F(RemotePtrTest, EndToEnd) { diff --git a/mojo/public/tests/bindings/sample_service_unittests.cc b/mojo/public/tests/bindings/sample_service_unittests.cc index 3fe8a11..f31b08d 100644 --- a/mojo/public/tests/bindings/sample_service_unittests.cc +++ b/mojo/public/tests/bindings/sample_service_unittests.cc @@ -6,7 +6,7 @@ #include <ostream> #include <string> -#include "mojo/public/tests/bindings/simple_bindings_support.h" +#include "mojo/public/environment/environment.h" #include "mojom/sample_service.h" #include "testing/gtest/include/gtest/gtest.h" @@ -286,7 +286,7 @@ class SimpleMessageReceiver : public mojo::MessageReceiver { } // namespace TEST(BindingsSampleTest, Basic) { - mojo::test::SimpleBindingsSupport bindings_support; + mojo::Environment env; SimpleMessageReceiver receiver; // User has a proxy to a Service somehow. diff --git a/mojo/public/tests/bindings/simple_bindings_support.cc b/mojo/public/tests/bindings/simple_bindings_support.cc deleted file mode 100644 index ceabc1e..0000000 --- a/mojo/public/tests/bindings/simple_bindings_support.cc +++ /dev/null @@ -1,101 +0,0 @@ -// Copyright 2013 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "mojo/public/tests/bindings/simple_bindings_support.h" - -#include <stdlib.h> - -#include <algorithm> - -namespace mojo { -namespace test { - -SimpleBindingsSupport::SimpleBindingsSupport() - : buf_(NULL) { - BindingsSupport::Set(this); -} - -SimpleBindingsSupport::~SimpleBindingsSupport() { - BindingsSupport::Set(NULL); - - for (WaiterList::iterator it = waiters_.begin(); it != waiters_.end(); ++it) - delete *it; -} - -Buffer* SimpleBindingsSupport::SetCurrentBuffer(Buffer* buf) { - // This is a simplistic implementation that assumes it is only ever used from - // a single thread, which is common in tests. - std::swap(buf_, buf); - return buf; -} - -Buffer* SimpleBindingsSupport::GetCurrentBuffer() { - return buf_; -} - -BindingsSupport::AsyncWaitID SimpleBindingsSupport::AsyncWait( - const Handle& handle, - MojoWaitFlags flags, - AsyncWaitCallback* callback) { - Waiter* waiter = new Waiter(); - waiter->handle = handle; - waiter->flags = flags; - waiter->callback = callback; - waiters_.push_back(waiter); - return waiter; -} - -void SimpleBindingsSupport::CancelWait(AsyncWaitID async_wait_id) { - Waiter* waiter = static_cast<Waiter*>(async_wait_id); - - WaiterList::iterator it = waiters_.begin(); - while (it != waiters_.end()) { - if (*it == waiter) { - WaiterList::iterator doomed = it++; - waiters_.erase(doomed); - } else { - ++it; - } - } - - delete waiter; -} - -void SimpleBindingsSupport::Process() { - for (;;) { - typedef std::pair<AsyncWaitCallback*, MojoResult> Result; - std::list<Result> results; - - WaiterList::iterator it = waiters_.begin(); - while (it != waiters_.end()) { - Waiter* waiter = *it; - MojoResult result; - if (IsReady(waiter->handle, waiter->flags, &result)) { - results.push_back(std::make_pair(waiter->callback, result)); - WaiterList::iterator doomed = it++; - waiters_.erase(doomed); - delete waiter; - } else { - ++it; - } - } - - for (std::list<Result>::const_iterator it = results.begin(); - it != results.end(); - ++it) { - it->first->OnHandleReady(it->second); - } - if (results.empty()) - break; - } -} - -bool SimpleBindingsSupport::IsReady(const Handle& handle, MojoWaitFlags flags, - MojoResult* result) { - *result = Wait(handle, flags, 0); - return *result != MOJO_RESULT_DEADLINE_EXCEEDED; -} - -} // namespace test -} // namespace mojo diff --git a/mojo/public/tests/bindings/simple_bindings_support.h b/mojo/public/tests/bindings/simple_bindings_support.h deleted file mode 100644 index e124625..0000000 --- a/mojo/public/tests/bindings/simple_bindings_support.h +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright 2013 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef MOJO_PUBLIC_TESTS_SIMPLE_BINDINGS_SUPPORT_H_ -#define MOJO_PUBLIC_TESTS_SIMPLE_BINDINGS_SUPPORT_H_ - -#include <list> - -#include "mojo/public/bindings/lib/bindings_support.h" - -namespace mojo { -namespace test { - -class SimpleBindingsSupport : public BindingsSupport { - public: - SimpleBindingsSupport(); - virtual ~SimpleBindingsSupport(); - - virtual Buffer* SetCurrentBuffer(Buffer* buf) MOJO_OVERRIDE; - virtual Buffer* GetCurrentBuffer() MOJO_OVERRIDE; - - virtual AsyncWaitID AsyncWait(const Handle& handle, - MojoWaitFlags flags, - AsyncWaitCallback* callback) MOJO_OVERRIDE; - virtual void CancelWait(AsyncWaitID async_wait_id) MOJO_OVERRIDE; - - // This method is called by unit tests to check the status of any handles - // that we are asynchronously waiting on and to dispatch callbacks for any - // handles that are ready. - void Process(); - - private: - bool IsReady(const Handle& handle, MojoWaitFlags flags, MojoResult* result); - - struct Waiter { - Handle handle; - MojoWaitFlags flags; - AsyncWaitCallback* callback; - }; - - typedef std::list<Waiter*> WaiterList; - WaiterList waiters_; - - Buffer* buf_; -}; - -} // namespace test -} // namespace mojo - -#endif // MOJO_PUBLIC_TESTS_SIMPLE_BINDINGS_SUPPORT_H_ diff --git a/mojo/public/tests/bindings/type_conversion_unittest.cc b/mojo/public/tests/bindings/type_conversion_unittest.cc index fb92b60..ad0d1f7 100644 --- a/mojo/public/tests/bindings/type_conversion_unittest.cc +++ b/mojo/public/tests/bindings/type_conversion_unittest.cc @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "mojo/public/tests/bindings/simple_bindings_support.h" +#include "mojo/public/environment/environment.h" #include "mojom/test_structs.h" #include "testing/gtest/include/gtest/gtest.h" @@ -66,7 +66,7 @@ namespace test { class TypeConversionTest : public testing::Test { private: - SimpleBindingsSupport bindings_support_; + Environment env_; }; TEST_F(TypeConversionTest, String) { diff --git a/mojo/public/tests/utility/bindings_support_impl_unittest.cc b/mojo/public/tests/environment/async_waiter_unittest.cc index b2c248c..dd0f397 100644 --- a/mojo/public/tests/utility/bindings_support_impl_unittest.cc +++ b/mojo/public/tests/environment/async_waiter_unittest.cc @@ -1,20 +1,19 @@ -// Copyright 2013 The Chromium Authors. All rights reserved. +// Copyright 2014 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "mojo/public/utility/bindings_support_impl.h" +#include "mojo/public/environment/default_async_waiter.h" -#include "base/basictypes.h" +#include "mojo/public/environment/environment.h" #include "mojo/public/system/core_cpp.h" #include "mojo/public/tests/test_support.h" -#include "mojo/public/utility/environment.h" #include "mojo/public/utility/run_loop.h" #include "testing/gtest/include/gtest/gtest.h" namespace mojo { namespace { -class TestAsyncWaitCallback : public BindingsSupport::AsyncWaitCallback { +class TestAsyncWaitCallback { public: TestAsyncWaitCallback() : result_count_(0), last_result_(MOJO_RESULT_OK) { } @@ -24,10 +23,11 @@ class TestAsyncWaitCallback : public BindingsSupport::AsyncWaitCallback { MojoResult last_result() const { return last_result_; } - // RunLoopHandler: - virtual void OnHandleReady(MojoResult result) OVERRIDE { - result_count_++; - last_result_ = result; + // MojoAsyncWaitCallback: + static void OnHandleReady(void* closure, MojoResult result) { + TestAsyncWaitCallback* self = static_cast<TestAsyncWaitCallback*>(closure); + self->result_count_++; + self->last_result_ = result; } private: @@ -37,9 +37,26 @@ class TestAsyncWaitCallback : public BindingsSupport::AsyncWaitCallback { DISALLOW_COPY_AND_ASSIGN(TestAsyncWaitCallback); }; -class BindingsSupportImplTest : public testing::Test { +MojoAsyncWaitID CallAsyncWait(const Handle& handle, + MojoWaitFlags flags, + TestAsyncWaitCallback* callback) { + MojoAsyncWaiter* waiter = GetDefaultAsyncWaiter(); + return waiter->AsyncWait(waiter, + handle.value(), + flags, + MOJO_DEADLINE_INDEFINITE, + &TestAsyncWaitCallback::OnHandleReady, + callback); +} + +void CallCancelWait(MojoAsyncWaitID wait_id) { + MojoAsyncWaiter* waiter = GetDefaultAsyncWaiter(); + waiter->CancelWait(waiter, wait_id); +} + +class AsyncWaiterTest : public testing::Test { public: - BindingsSupportImplTest() {} + AsyncWaiterTest() {} virtual void SetUp() OVERRIDE { Test::SetUp(); @@ -56,25 +73,26 @@ class BindingsSupportImplTest : public testing::Test { scoped_ptr<Environment> environment_; scoped_ptr<RunLoop> run_loop_; - DISALLOW_COPY_AND_ASSIGN(BindingsSupportImplTest); + DISALLOW_COPY_AND_ASSIGN(AsyncWaiterTest); }; // Verifies AsyncWaitCallback is notified when pipe is ready. -TEST_F(BindingsSupportImplTest, CallbackNotified) { +TEST_F(AsyncWaiterTest, CallbackNotified) { TestAsyncWaitCallback callback; MessagePipe test_pipe; EXPECT_EQ(MOJO_RESULT_OK, test::WriteEmptyMessage(test_pipe.handle1.get())); - BindingsSupport::Get()->AsyncWait(test_pipe.handle0.get(), - MOJO_WAIT_FLAG_READABLE, &callback); + CallAsyncWait(test_pipe.handle0.get(), + MOJO_WAIT_FLAG_READABLE, + &callback); RunLoop::current()->Run(); EXPECT_EQ(1, callback.result_count()); EXPECT_EQ(MOJO_RESULT_OK, callback.last_result()); } // Verifies 2 AsyncWaitCallbacks are notified when there pipes are ready. -TEST_F(BindingsSupportImplTest, TwoCallbacksNotified) { +TEST_F(AsyncWaiterTest, TwoCallbacksNotified) { TestAsyncWaitCallback callback1; TestAsyncWaitCallback callback2; MessagePipe test_pipe1; @@ -84,10 +102,9 @@ TEST_F(BindingsSupportImplTest, TwoCallbacksNotified) { EXPECT_EQ(MOJO_RESULT_OK, test::WriteEmptyMessage(test_pipe2.handle1.get())); - BindingsSupport::Get()->AsyncWait(test_pipe1.handle0.get(), - MOJO_WAIT_FLAG_READABLE, &callback1); - BindingsSupport::Get()->AsyncWait(test_pipe2.handle0.get(), - MOJO_WAIT_FLAG_READABLE, &callback2); + CallAsyncWait(test_pipe1.handle0.get(), MOJO_WAIT_FLAG_READABLE, &callback1); + CallAsyncWait(test_pipe2.handle0.get(), MOJO_WAIT_FLAG_READABLE, &callback2); + RunLoop::current()->Run(); EXPECT_EQ(1, callback1.result_count()); EXPECT_EQ(MOJO_RESULT_OK, callback1.last_result()); @@ -96,14 +113,15 @@ TEST_F(BindingsSupportImplTest, TwoCallbacksNotified) { } // Verifies cancel works. -TEST_F(BindingsSupportImplTest, CancelCallback) { +TEST_F(AsyncWaiterTest, CancelCallback) { TestAsyncWaitCallback callback; MessagePipe test_pipe; EXPECT_EQ(MOJO_RESULT_OK, test::WriteEmptyMessage(test_pipe.handle1.get())); - BindingsSupport::Get()->CancelWait( - BindingsSupport::Get()->AsyncWait(test_pipe.handle0.get(), - MOJO_WAIT_FLAG_READABLE, &callback)); + CallCancelWait( + CallAsyncWait(test_pipe.handle0.get(), + MOJO_WAIT_FLAG_READABLE, + &callback)); RunLoop::current()->Run(); EXPECT_EQ(0, callback.result_count()); } |