blob: c853bceeccac5a030f74b0940bfa28e139a0a270 [file] [log] [blame]
Josh Gaob582fa32016-02-10 14:49:00 -08001/*
2 * Copyright (C) 2016 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
17#include <gtest/gtest.h>
18
19#include "socket.h"
20#include "sysdeps.h"
21
22class FdeventTest : public ::testing::Test {
23 protected:
24 int dummy = -1;
25
26 static void SetUpTestCase() {
27#if !defined(_WIN32)
28 ASSERT_NE(SIG_ERR, signal(SIGPIPE, SIG_IGN));
29#endif
30 }
31
32 void SetUp() override {
33 fdevent_reset();
34 ASSERT_EQ(0u, fdevent_installed_count());
35 }
36
37 // Register a dummy socket used to wake up the fdevent loop to tell it to die.
38 void PrepareThread() {
39 int dummy_fds[2];
40 if (adb_socketpair(dummy_fds) != 0) {
41 FAIL() << "failed to create socketpair: " << strerror(errno);
42 }
43
44 asocket* dummy_socket = create_local_socket(dummy_fds[1]);
45 if (!dummy_socket) {
46 FAIL() << "failed to create local socket: " << strerror(errno);
47 }
48 dummy_socket->ready(dummy_socket);
49 dummy = dummy_fds[0];
50 }
51
52 void TerminateThread(adb_thread_t thread) {
53 fdevent_terminate_loop();
54 ASSERT_TRUE(WriteFdExactly(dummy, "", 1));
55 ASSERT_TRUE(adb_thread_join(thread));
56 ASSERT_EQ(0, adb_close(dummy));
57 }
58};