Deflake ExternalStylusIntegrationTest test case
The integration test was testing a case where the code is expected to do
nothing until a 72ms timeout expires, and then checking that an event is
generated.
To verify this, the test would wait exactly 72ms, and assert there were
no events generated. Since this is an integration test, waiting the
exact amount of time as the timeout used in the code is naturally flaky.
Resolve this by using a smaller timeout that the one used in the code
when enforcing that an event is not generated.
Bug: 304787002
Test: atest ... --rerun-until-failure
Change-Id: Ib0bcc6de149efee575d9fda6482fffaa2edfa15d
diff --git a/services/inputflinger/tests/InputReader_test.cpp b/services/inputflinger/tests/InputReader_test.cpp
index c6536de..c03e82b 100644
--- a/services/inputflinger/tests/InputReader_test.cpp
+++ b/services/inputflinger/tests/InputReader_test.cpp
@@ -1352,6 +1352,9 @@
std::shared_ptr<FakePointerController> mFakePointerController;
+ constexpr static auto EVENT_HAPPENED_TIMEOUT = 2000ms;
+ constexpr static auto EVENT_DID_NOT_HAPPEN_TIMEOUT = 30ms;
+
void SetUp() override {
#if !defined(__ANDROID__)
GTEST_SKIP();
@@ -1383,8 +1386,8 @@
}
void setupInputReader() {
- mTestListener = std::make_unique<TestInputListener>(/*eventHappenedTimeout=*/2000ms,
- /*eventDidNotHappenTimeout=*/30ms);
+ mTestListener = std::make_unique<TestInputListener>(EVENT_HAPPENED_TIMEOUT,
+ EVENT_DID_NOT_HAPPEN_TIMEOUT);
mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
*mTestListener);
@@ -2415,17 +2418,29 @@
mDevice->sendTrackingId(FIRST_TRACKING_ID);
mDevice->sendToolType(MT_TOOL_FINGER);
mDevice->sendDown(centerPoint);
- auto waitUntil = std::chrono::system_clock::now() +
- std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
+ const auto syncTime = std::chrono::system_clock::now();
+ // After 72 ms, the event *will* be generated. If we wait the full 72 ms to check that NO event
+ // is generated in that period, there will be a race condition between the event being generated
+ // and the test's wait timeout expiring. Thus, we wait for a shorter duration in the test, which
+ // will reduce the liklihood of the race condition occurring.
+ const auto waitUntilTimeForNoEvent =
+ syncTime + std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT / 2));
mDevice->sendSync();
- ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled(waitUntil));
+ ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled(waitUntilTimeForNoEvent));
// Since the external stylus did not report a pressure value within the timeout,
// it shows up as a finger pointer.
- ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
- AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
- WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
- WithToolType(ToolType::FINGER), WithDeviceId(touchscreenId), WithPressure(1.f))));
+ const auto waitUntilTimeForEvent = syncTime +
+ std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT)) + EVENT_HAPPENED_TIMEOUT;
+ ASSERT_NO_FATAL_FAILURE(
+ mTestListener->assertNotifyMotionWasCalled(AllOf(WithMotionAction(
+ AMOTION_EVENT_ACTION_DOWN),
+ WithSource(AINPUT_SOURCE_TOUCHSCREEN |
+ AINPUT_SOURCE_STYLUS),
+ WithToolType(ToolType::FINGER),
+ WithDeviceId(touchscreenId),
+ WithPressure(1.f)),
+ waitUntilTimeForEvent));
// Change the pressure on the external stylus. Since the pressure was not present at the start
// of the gesture, it is ignored for now.