diff options
author | Adrian Salido <salidoa@google.com> | 2017-02-16 10:29:46 -0800 |
---|---|---|
committer | Sean Paul <seanpaul@chromium.org> | 2017-04-25 14:41:03 -0400 |
commit | fa37f67815c29aaa17501569a19037b18512db99 (patch) | |
tree | 5e717fc08e35e30774802eb7da7979a06c16375a /tests | |
parent | ac8741504befec1d8aa2067a6eb5c2088bc84160 (diff) |
drm_hwcomposer: refactor Worker
Make use of standard library mutex and conditions which simplifies use
of condition variables and benefits from things like scoped locking.
Also add tests to make sure it runs as expected.
Change-Id: Iaf92e17e1f6757dce490eddee61f84cb1f953b0c
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Android.mk | 12 | ||||
-rw-r--r-- | tests/worker_test.cpp | 110 |
2 files changed, 122 insertions, 0 deletions
diff --git a/tests/Android.mk b/tests/Android.mk new file mode 100644 index 0000000..5bbda93 --- /dev/null +++ b/tests/Android.mk @@ -0,0 +1,12 @@ +LOCAL_PATH := $(call my-dir) + +include $(CLEAR_VARS) + +LOCAL_SRC_FILES := \ + worker_test.cpp + +LOCAL_MODULE := hwc-drm-tests +LOCAL_STATIC_LIBRARIES := libdrmhwc_utils +LOCAL_C_INCLUDES := external/drm_hwcomposer + +include $(BUILD_NATIVE_TEST) diff --git a/tests/worker_test.cpp b/tests/worker_test.cpp new file mode 100644 index 0000000..38f91db --- /dev/null +++ b/tests/worker_test.cpp @@ -0,0 +1,110 @@ +#include <gtest/gtest.h> +#include <hardware/hardware.h> + +#include <chrono> + +#include "worker.h" + +using android::Worker; + +struct TestWorker : public Worker { + TestWorker() + : Worker("test-worker", HAL_PRIORITY_URGENT_DISPLAY), + value(0), + enabled_(false) { + } + + int Init() { + return InitWorker(); + } + + void Routine() { + Lock(); + if (!enabled_) { + int ret = WaitForSignalOrExitLocked(); + if (ret == -EINTR) { + Unlock(); + return; + } + // should only reached here if it was enabled + if (!enabled_) + printf("Shouldn't reach here while disabled %d %d\n", value, ret); + } + value++; + Unlock(); + } + + void Control(bool enable) { + bool changed = false; + Lock(); + if (enabled_ != enable) { + enabled_ = enable; + changed = true; + } + Unlock(); + + if (enable && changed) + Signal(); + } + + int value; + + private: + bool enabled_; +}; + +struct WorkerTest : public testing::Test { + TestWorker worker; + + virtual void SetUp() { + worker.Init(); + } + + void small_delay() { + std::this_thread::sleep_for(std::chrono::milliseconds(20)); + } +}; + +TEST_F(WorkerTest, test_worker) { + // already isInitialized so should fail + ASSERT_TRUE(worker.initialized()); + + int val = worker.value; + small_delay(); + + // value shouldn't change when isInitialized + ASSERT_EQ(val, worker.value); + + worker.Control(true); + small_delay(); + + // while locked, value shouldn't be changing + worker.Lock(); + val = worker.value; + small_delay(); + ASSERT_EQ(val, worker.value); + worker.Unlock(); + + small_delay(); + // value should be different now + ASSERT_NE(val, worker.value); + + worker.Control(false); + worker.Lock(); + val = worker.value; + worker.Unlock(); + small_delay(); + + // value should be same + ASSERT_EQ(val, worker.value); + + worker.Exit(); + ASSERT_FALSE(worker.initialized()); +} + +TEST_F(WorkerTest, exit_while_running) { + worker.Control(true); + + std::this_thread::sleep_for(std::chrono::milliseconds(50)); + worker.Exit(); +} |