test_lazy.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright (C) 2019 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <HidlService.h>
  17. #include <android/hidl/manager/1.2/IClientCallback.h>
  18. #include <gmock/gmock.h>
  19. #include <gtest/gtest.h>
  20. using ::android::hardware::Return;
  21. using ::android::hardware::Void;
  22. using ::android::hidl::base::V1_0::IBase;
  23. using ::android::hidl::manager::implementation::HidlService;
  24. using ::android::hidl::manager::V1_2::IClientCallback;
  25. using ::android::sp;
  26. using ::testing::ElementsAre;
  27. using ::testing::Invoke;
  28. using ::testing::NiceMock;
  29. class RecordingClientCallback : public IClientCallback {
  30. public:
  31. Return<void> onClients(const sp<IBase>& /*base*/, bool clients) override {
  32. stream.push_back(clients);
  33. return Void();
  34. }
  35. std::vector<bool> stream;
  36. };
  37. class MockHidlService : public HidlService {
  38. public:
  39. MockHidlService() : HidlService("fqname", "instance") {}
  40. MOCK_METHOD0(getNodeStrongRefCount, ssize_t());
  41. };
  42. class HidlServiceLazyTest : public ::testing::Test {
  43. public:
  44. // Note that this should include one count for hwservicemanager. A count of
  45. // 1 indicates that hwservicemanager is the only process holding the service.
  46. void setReportedClientCount(ssize_t count) {
  47. mState.mInjectedReportCount = count;
  48. }
  49. // Essentially, the number of times the kernel API would be called
  50. size_t getNumTimesReported() {
  51. return mState.mInjectedTimes;
  52. }
  53. std::unique_ptr<HidlService> makeService() {
  54. auto service = std::make_unique<NiceMock<MockHidlService>>();
  55. ON_CALL(*service, getNodeStrongRefCount()).WillByDefault(Invoke([&]() {
  56. mState.mInjectedTimes++;
  57. return mState.mInjectedReportCount;
  58. }));
  59. return service;
  60. }
  61. protected:
  62. void SetUp() override {
  63. mState = TestState();
  64. }
  65. struct TestState {
  66. ssize_t mInjectedReportCount = -1;
  67. size_t mInjectedTimes = 0;
  68. } mState;
  69. };
  70. TEST_F(HidlServiceLazyTest, NoChange) {
  71. sp<RecordingClientCallback> cb = new RecordingClientCallback;
  72. std::unique_ptr<HidlService> service = makeService();
  73. service->addClientCallback(cb);
  74. setReportedClientCount(1);
  75. for (size_t i = 0; i < 100; i++) {
  76. service->handleClientCallbacks(true /*onInterval*/);
  77. }
  78. ASSERT_THAT(cb->stream, ElementsAre());
  79. }
  80. TEST_F(HidlServiceLazyTest, GetAndDrop) {
  81. sp<RecordingClientCallback> cb = new RecordingClientCallback;
  82. std::unique_ptr<HidlService> service = makeService();
  83. service->addClientCallback(cb);
  84. // some other process has the service
  85. setReportedClientCount(2);
  86. service->handleClientCallbacks(true /*onInterval*/);
  87. ASSERT_THAT(cb->stream, ElementsAre(true));
  88. // just hwservicemanager has the service
  89. setReportedClientCount(1);
  90. service->handleClientCallbacks(true /*onInterval*/);
  91. ASSERT_THAT(cb->stream, ElementsAre(true));
  92. service->handleClientCallbacks(true /*onInterval*/);
  93. ASSERT_THAT(cb->stream, ElementsAre(true, false)); // reported only after two intervals
  94. }
  95. TEST_F(HidlServiceLazyTest, GetGuarantee) {
  96. sp<RecordingClientCallback> cb = new RecordingClientCallback;
  97. std::unique_ptr<HidlService> service = makeService();
  98. service->addClientCallback(cb);
  99. service->guaranteeClient();
  100. setReportedClientCount(1);
  101. service->handleClientCallbacks(false /*onInterval*/);
  102. ASSERT_THAT(cb->stream, ElementsAre(true));
  103. service->handleClientCallbacks(true /*onInterval*/);
  104. ASSERT_THAT(cb->stream, ElementsAre(true));
  105. service->handleClientCallbacks(true /*onInterval*/);
  106. ASSERT_THAT(cb->stream, ElementsAre(true, false)); // reported only after two intervals
  107. }
  108. TEST_F(HidlServiceLazyTest, ManyUpdatesOffInterval) {
  109. sp<RecordingClientCallback> cb = new RecordingClientCallback;
  110. std::unique_ptr<HidlService> service = makeService();
  111. service->addClientCallback(cb);
  112. // Clients can appear and dissappear as many times as necessary, but they are only considered
  113. // dropped when the fixed interval stops.
  114. for (size_t i = 0; i < 100; i++) {
  115. setReportedClientCount(2);
  116. service->handleClientCallbacks(false /*onInterval*/);
  117. setReportedClientCount(1);
  118. service->handleClientCallbacks(false /*onInterval*/);
  119. }
  120. ASSERT_THAT(cb->stream, ElementsAre(true));
  121. service->handleClientCallbacks(true /*onInterval*/);
  122. ASSERT_THAT(cb->stream, ElementsAre(true));
  123. service->handleClientCallbacks(true /*onInterval*/);
  124. ASSERT_THAT(cb->stream, ElementsAre(true, false)); // reported only after two intervals
  125. }
  126. TEST_F(HidlServiceLazyTest, AcquisitionAfterGuarantee) {
  127. sp<RecordingClientCallback> cb = new RecordingClientCallback;
  128. std::unique_ptr<HidlService> service = makeService();
  129. service->addClientCallback(cb);
  130. setReportedClientCount(2);
  131. service->handleClientCallbacks(false /*onInterval*/);
  132. ASSERT_THAT(cb->stream, ElementsAre(true));
  133. setReportedClientCount(1);
  134. service->guaranteeClient();
  135. service->handleClientCallbacks(false /*onInterval*/);
  136. ASSERT_THAT(cb->stream, ElementsAre(true));
  137. service->handleClientCallbacks(true /*onInterval*/);
  138. ASSERT_THAT(cb->stream, ElementsAre(true));
  139. service->handleClientCallbacks(true /*onInterval*/);
  140. ASSERT_THAT(cb->stream, ElementsAre(true, false)); // reported only after two intervals
  141. }
  142. TEST_F(HidlServiceLazyTest, NotificationSentForNewClientCallback) {
  143. sp<RecordingClientCallback> cb = new RecordingClientCallback;
  144. std::unique_ptr<HidlService> service = makeService();
  145. service->addClientCallback(cb);
  146. setReportedClientCount(2);
  147. service->handleClientCallbacks(false /*onInterval*/);
  148. ASSERT_THAT(cb->stream, ElementsAre(true));
  149. sp<RecordingClientCallback> laterCb = new RecordingClientCallback;
  150. service->addClientCallback(laterCb);
  151. ASSERT_THAT(cb->stream, ElementsAre(true));
  152. ASSERT_THAT(laterCb->stream, ElementsAre(true));
  153. setReportedClientCount(1);
  154. service->handleClientCallbacks(true /*onInterval*/);
  155. ASSERT_THAT(cb->stream, ElementsAre(true));
  156. ASSERT_THAT(laterCb->stream, ElementsAre(true));
  157. service->handleClientCallbacks(true /*onInterval*/);
  158. ASSERT_THAT(cb->stream, ElementsAre(true, false)); // reported only after two intervals
  159. ASSERT_THAT(laterCb->stream, ElementsAre(true, false)); // reported only after two intervals
  160. }
  161. TEST_F(HidlServiceLazyTest, ClientWithoutLazy) {
  162. std::unique_ptr<HidlService> service = makeService();
  163. setReportedClientCount(2);
  164. service->handleClientCallbacks(false /*onInterval*/);
  165. // kernel API should not be called
  166. EXPECT_EQ(0u, getNumTimesReported());
  167. }