aboutsummaryrefslogtreecommitdiff
path: root/tests/lazy_test/main.cpp
blob: 222d8092f95706e87ff296d2840fbcddfce49e84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
 * Copyright (C) 2019, The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <sys/eventfd.h>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <numeric>
#include <string>
#include <thread>

#include <sys/epoll.h>
#include <unistd.h>

#include <ILazyTestService.h>
#include <ILazyTestServiceCb.h>
#include <binder/IPCThreadState.h>
#include <binder/IServiceManager.h>
#include <gtest/gtest.h>
#include <utils/String8.h>

using ::ILazyTestService;
using ::android::IBinder;
using ::android::IPCThreadState;
using ::android::IServiceManager;
using ::android::sp;
using ::android::String16;
using ::android::base::unique_fd;
using ::android::os::ParcelFileDescriptor;

std::vector<String16> gServiceNames;
static constexpr size_t SHUTDOWN_WAIT_MS = 10000;
static constexpr size_t CALLBACK_SHUTDOWN_WAIT_MS = 5000;

sp<IBinder> waitForService(const String16& name) {
  sp<IServiceManager> manager;
  manager = android::defaultServiceManager();
  EXPECT_NE(manager, nullptr);

  return manager->waitForService(name);
}

bool isServiceRunning(const String16& name) {
  sp<IServiceManager> manager;
  manager = android::defaultServiceManager();
  EXPECT_NE(manager, nullptr);

  return manager->checkService(name) != nullptr;
}

class AidlLazyTest : public ::testing::Test {
 protected:
  sp<IServiceManager> manager;

  void SetUp() override {
    manager = android::defaultServiceManager();
    ASSERT_NE(manager, nullptr);

    for (size_t i = 0; i < gServiceNames.size(); i++) {
      ASSERT_FALSE(isServiceRunning(gServiceNames.at(i)))
          << "Service '" << android::String8(gServiceNames.at(i)) << "' is already running. "
          << "Please ensure this is implemented as a lazy service, then kill all "
          << "clients of this service and try again.";
    }
  }

  void TearDown() override {
    std::cout << "Waiting " << SHUTDOWN_WAIT_MS << " milliseconds before checking that the "
              << "service has shut down." << std::endl;
    IPCThreadState::self()->flushCommands();
    usleep(SHUTDOWN_WAIT_MS * 1000);
    for (size_t i = 0; i < gServiceNames.size(); i++) {
      ASSERT_FALSE(isServiceRunning(gServiceNames.at(i))) << "Service failed to shut down.";
    }
  }
};

static constexpr size_t NUM_IMMEDIATE_GETS = 100;
TEST_F(AidlLazyTest, GetRelease) {
  size_t nServices = gServiceNames.size();

  for (size_t i = 0; i < nServices * NUM_IMMEDIATE_GETS; i++) {
    IPCThreadState::self()->flushCommands();
    sp<IBinder> service = waitForService(gServiceNames.at(i % nServices));
    ASSERT_NE(service.get(), nullptr);
    ASSERT_EQ(service->pingBinder(), android::NO_ERROR);
  }
}

static std::vector<size_t> waitMs(size_t numTimes, size_t maxWait) {
  std::vector<size_t> times(numTimes);
  for (size_t i = 0; i < numTimes; i++) {
    times.at(i) = (size_t)(rand() % (maxWait + 1));
  }
  return times;
}

static void testWithTimes(const std::vector<size_t>& waitMs, bool beforeGet) {
  size_t nServices = gServiceNames.size();
  for (size_t i = 0; i < waitMs.size(); i++) {
    IPCThreadState::self()->flushCommands();
    if (beforeGet) {
      std::cout << "Thread waiting " << waitMs.at(i) << " while not holding service." << std::endl;
      usleep(waitMs.at(i) * 1000);
    }

    sp<IBinder> service = waitForService(gServiceNames.at(i % nServices));

    if (!beforeGet) {
      std::cout << "Thread waiting " << waitMs.at(i) << " while holding service." << std::endl;
      usleep(waitMs.at(i) * 1000);
    }

    ASSERT_NE(service.get(), nullptr);
    ASSERT_EQ(service->pingBinder(), android::NO_ERROR);
  }
}

static constexpr size_t NUM_TIMES_GET_RELEASE = 5;
static constexpr size_t MAX_WAITING_DURATION_MS = 10000;
static constexpr size_t NUM_CONCURRENT_THREADS = 3;
static void testConcurrentThreadsWithDelays(bool delayBeforeGet) {
  size_t nServices = gServiceNames.size();
  std::vector<std::vector<size_t>> threadWaitTimes(NUM_CONCURRENT_THREADS);
  int maxWait = 0;
  for (size_t i = 0; i < threadWaitTimes.size(); i++) {
    threadWaitTimes.at(i) = waitMs(NUM_TIMES_GET_RELEASE * nServices, MAX_WAITING_DURATION_MS);
    int totalWait = std::accumulate(threadWaitTimes.at(i).begin(), threadWaitTimes.at(i).end(), 0);
    maxWait = std::max(maxWait, totalWait);
  }
  std::cout << "Additional runtime expected from sleeps: " << maxWait << " millisecond(s)."
            << std::endl;

  std::vector<std::thread> threads(NUM_CONCURRENT_THREADS);
  for (size_t i = 0; i < threads.size(); i++) {
    threads.at(i) = std::thread(testWithTimes, threadWaitTimes.at(i), delayBeforeGet);
  }

  for (auto& thread : threads) {
    thread.join();
  }
}

TEST_F(AidlLazyTest, GetConcurrentWithWaitBefore) {
  testConcurrentThreadsWithDelays(true);
}

TEST_F(AidlLazyTest, GetConcurrentWithWaitAfter) {
  testConcurrentThreadsWithDelays(false);
}

class AidlLazyRegistrarTest : public ::testing::Test {
 protected:
  const String16 serviceName = String16("aidl_lazy_test_1");
  void SetUp() override {
    if (std::find(gServiceNames.begin(), gServiceNames.end(), serviceName) == gServiceNames.end()) {
      GTEST_SKIP() << "Persistence test requires special instance: " << serviceName;
    }
  }
};

template <typename T>
sp<T> waitForLazyTestService(String16 name) {
  sp<T> service = android::waitForService<T>(name);
  EXPECT_NE(service, nullptr);
  return service;
}

TEST_F(AidlLazyRegistrarTest, ForcedPersistenceTest) {
  sp<ILazyTestService> service;
  for (int i = 0; i < 2; i++) {
    service = waitForLazyTestService<ILazyTestService>(serviceName);
    EXPECT_TRUE(service->forcePersist(i == 0).isOk());
    service = nullptr;

    std::cout << "Waiting " << SHUTDOWN_WAIT_MS << " milliseconds before checking whether the "
              << "service is still running." << std::endl;
    IPCThreadState::self()->flushCommands();
    usleep(SHUTDOWN_WAIT_MS * 1000);

    if (i == 0) {
      ASSERT_TRUE(isServiceRunning(serviceName)) << "Service shut down when it shouldn't have.";
    } else {
      ASSERT_FALSE(isServiceRunning(serviceName)) << "Service failed to shut down.";
    }
  }
}

TEST_F(AidlLazyRegistrarTest, ActiveServicesCountCallbackTest) {
  const String16 cbServiceName = String16("aidl_lazy_cb_test");

  int efd = eventfd(0, 0);
  ASSERT_GE(efd, 0) << "Failed to create eventfd";

  unique_fd uniqueEventFd(efd);
  ParcelFileDescriptor parcelEventFd(std::move(uniqueEventFd));

  sp<ILazyTestServiceCb> service;
  service = waitForLazyTestService<ILazyTestServiceCb>(cbServiceName);
  ASSERT_TRUE(service->setEventFd(parcelEventFd).isOk());
  service = nullptr;

  IPCThreadState::self()->flushCommands();

  std::cout << "Waiting " << SHUTDOWN_WAIT_MS << " milliseconds for callback completion "
            << "notification." << std::endl;

  int epollFd = epoll_create1(EPOLL_CLOEXEC);
  ASSERT_GE(epollFd, 0) << "Failed to create epoll";
  unique_fd epollUniqueFd(epollFd);

  const int EPOLL_MAX_EVENTS = 1;
  struct epoll_event event, events[EPOLL_MAX_EVENTS];

  event.events = EPOLLIN;
  event.data.fd = efd;
  int rc = epoll_ctl(epollFd, EPOLL_CTL_ADD, efd, &event);
  ASSERT_GE(rc, 0) << "Failed to add fd to epoll";

  rc = TEMP_FAILURE_RETRY(epoll_wait(epollFd, events, EPOLL_MAX_EVENTS, SHUTDOWN_WAIT_MS));
  ASSERT_NE(rc, 0) << "Service shutdown timeout";
  ASSERT_GT(rc, 0) << "Error waiting for service shutdown notification";

  eventfd_t counter;
  rc = TEMP_FAILURE_RETRY(eventfd_read(parcelEventFd.get(), &counter));
  ASSERT_GE(rc, 0) << "Failed to get callback completion notification from service";
  ASSERT_EQ(counter, 1);

  std::cout << "Waiting " << CALLBACK_SHUTDOWN_WAIT_MS
            << " milliseconds before checking whether the "
            << "service is still running." << std::endl;

  usleep(CALLBACK_SHUTDOWN_WAIT_MS * 1000);

  ASSERT_FALSE(isServiceRunning(serviceName)) << "Service failed to shut down.";
}

int main(int argc, char** argv) {
  ::testing::InitGoogleTest(&argc, argv);

  srand(time(nullptr));

  if (argc < 2) {
    // If the user does not specify any service to test, default to these test interfaces
    gServiceNames.push_back(String16("aidl_lazy_test_1"));
    gServiceNames.push_back(String16("aidl_lazy_test_2"));
  } else {
    for (int i = 1; i < argc; i++) {
      gServiceNames.push_back(String16(argv[i]));
    }
  }

  android::ProcessState::self()->startThreadPool();

  return RUN_ALL_TESTS();
}