Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 08b574f9 authored by Siarhei Vishniakou's avatar Siarhei Vishniakou
Browse files

Add busy wait for consume() call

We have no guarantee that consume() will find an available event right
away.
If we send an event to InputDispatcher, it may take some time for
dispatcher to process the event. Only when the event is processed can we
expect that it will be written to the socket.

In the test, we currently allow no time for this to complete. For the
upcoming tests, the work performed by dispatcher might be too large, so
the tests would start to fail.

If there is no event available at the socket, WOULD_BLOCK would be
returned. So if we are sure an event is coming, we can busily wait, as
long as the consume call is returning WOULD_BLOCK.

Bug: none
Test: atest inputflinger_tests
The change was tested with a child CL for notifyDeviceReset.

Change-Id: Ib9557c4de03c1b1d4a4eb1573d527f47facbf249
parent 39988e32
Loading
Loading
Loading
Loading
+42 −18
Original line number Original line Diff line number Diff line
@@ -365,17 +365,48 @@ public:


class FakeInputReceiver {
class FakeInputReceiver {
public:
public:
    void consumeEvent(int32_t expectedEventType, int32_t expectedAction, int32_t expectedDisplayId,
    InputEvent* consume() {
                      int32_t expectedFlags) {
        uint32_t consumeSeq;
        uint32_t consumeSeq;
        InputEvent* event;
        InputEvent* event;
        status_t status = mConsumer->consume(&mEventFactory, false /*consumeBatches*/, -1,
            &consumeSeq, &event);


        ASSERT_EQ(OK, status)
        std::chrono::time_point start = std::chrono::steady_clock::now();
                << mName.c_str() << ": consumer consume should return OK.";
        status_t status = WOULD_BLOCK;
        ASSERT_TRUE(event != nullptr)
        while (status == WOULD_BLOCK) {
                << mName.c_str() << ": consumer should have returned non-NULL event.";
            status = mConsumer->consume(&mEventFactory, false /*consumeBatches*/, -1, &consumeSeq,
                                        &event);
            std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
            if (elapsed > 100ms) {
                break;
            }
        }

        if (status == WOULD_BLOCK) {
            // Just means there's no event available.
            return nullptr;
        }

        if (status != OK) {
            ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK.";
            return nullptr;
        }
        if (event == nullptr) {
            ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer";
            return nullptr;
        }

        status = mConsumer->sendFinishedSignal(consumeSeq, handled());
        if (status != OK) {
            ADD_FAILURE() << mName.c_str() << ": consumer sendFinishedSignal should return OK.";
        }
        return event;
    }

    void consumeEvent(int32_t expectedEventType, int32_t expectedAction, int32_t expectedDisplayId,
                      int32_t expectedFlags) {
        InputEvent* event = consume();

        ASSERT_NE(nullptr, event) << mName.c_str()
                                  << ": consumer should have returned non-NULL event.";
        ASSERT_EQ(expectedEventType, event->getType())
        ASSERT_EQ(expectedEventType, event->getType())
                << mName.c_str() << ": event type should match.";
                << mName.c_str() << ": event type should match.";


@@ -398,10 +429,6 @@ public:
                FAIL() << mName.c_str() << ": invalid event type: " << expectedEventType;
                FAIL() << mName.c_str() << ": invalid event type: " << expectedEventType;
            }
            }
        }
        }

        status = mConsumer->sendFinishedSignal(consumeSeq, handled());
        ASSERT_EQ(OK, status)
                << mName.c_str() << ": consumer sendFinishedSignal should return OK.";
    }
    }


    void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
    void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) {
@@ -415,13 +442,10 @@ public:
    }
    }


    void assertNoEvents() {
    void assertNoEvents() {
        uint32_t consumeSeq;
        InputEvent* event = consume();
        InputEvent* event;
        ASSERT_EQ(nullptr, event)
        status_t status = mConsumer->consume(&mEventFactory, false /*consumeBatches*/, -1,
            &consumeSeq, &event);
        ASSERT_NE(OK, status)
                << mName.c_str()
                << mName.c_str()
                << ": should not have received any events, so consume(..) should not return OK.";
                << ": should not have received any events, so consume() should return NULL";
    }
    }


protected:
protected: