// This test checks if we can close sockets in CLOSE_WAIT state.
TEST_F(LocalSocketTest, close_socket_in_CLOSE_WAIT_state) {
    std::string error;
    int listen_fd = network_inaddr_any_server(5038, SOCK_STREAM, &error);
    ASSERT_GE(listen_fd, 0);

    adb_thread_t client_thread;
    ASSERT_TRUE(adb_thread_create(reinterpret_cast<void (*)(void*)>(ClientThreadFunc), nullptr,
                                  &client_thread));

    struct sockaddr addr;
    socklen_t alen;
    alen = sizeof(addr);
    int accept_fd = adb_socket_accept(listen_fd, &addr, &alen);
    ASSERT_GE(accept_fd, 0);
    CloseRdHupSocketArg arg;
    arg.socket_fd = accept_fd;

    PrepareThread();
    adb_thread_t thread;
    ASSERT_TRUE(adb_thread_create(reinterpret_cast<void (*)(void*)>(CloseRdHupSocketThreadFunc),
                                  &arg, &thread));

    // Wait until the fdevent_loop() starts.
    adb_sleep_ms(SLEEP_FOR_FDEVENT_IN_MS);
    EXPECT_EQ(1u + GetAdditionalLocalSocketCount(), fdevent_installed_count());

    // Wait until the client closes its socket.
    ASSERT_TRUE(adb_thread_join(client_thread));

    adb_sleep_ms(SLEEP_FOR_FDEVENT_IN_MS);
    ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count());
    TerminateThread(thread);
}
TEST(sysdeps_thread, exit) {
    adb_thread_t thread;
    ASSERT_TRUE(adb_thread_create(
        [](void*) {
            adb_thread_exit();
            for (;;) continue;
        },
        nullptr, &thread));
    ASSERT_TRUE(adb_thread_join(thread));
}
Exemple #3
0
void cleanup_all() {
  int err = 0;
  int i = 0;
  D("Killing threads!\n");
  int len = __adb_threads_active->length;
  for (i = 0; i < len; i++) {
    adb_thread_t *thread = __adb_threads_active->base[i];
    D("Killing thread: %d, %p\n", i, thread);

    /*err = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
    if (err < 0) {
      D("ERROR: set cancel state %d\n", err);
    }

    err = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
    if (err < 0) {
      D("ERROR: set cancel type %d\n", err);
    }*/

    err = adb_thread_cancel(*thread);
    if (err < 0) {
      D("ERROR: cancelling %d\n", err);
    }

    err = adb_thread_join(*thread);
    if (err < 0) {
      D("ERROR: joining %d\n", err);
    }

    free(thread);
    free(__adb_tags_active->base[i]);
    D("Freed thread: %d\n", i);
  }
  free_adb_thread_ptr_array_list(__adb_threads_active);
  free_str_array_list(__adb_tags_active);
  D("Killed all threads!\n");
  D("Freeing data\n");
  cleanup_transport();
  D("Done freeing data\n");
  D("Removing all listeners on sockets\n");
  remove_all_listeners();
  D("Done removing all listeners\n");
#ifdef WIN32
    fclose(LOG_FILE); // close the log
    _cleanup_winsock(); // cleanup sockets
#endif
}
TEST(sysdeps_thread, join) {
    std::atomic<int> counter(0);
    std::vector<adb_thread_t> threads(500);
    for (size_t i = 0; i < threads.size(); ++i) {
        ASSERT_TRUE(adb_thread_create(increment_atomic_int, &counter, &threads[i]));
    }

    int current = counter.load();
    ASSERT_GE(current, 0);
    // Make sure that adb_thread_create actually creates threads, and doesn't do something silly
    // like synchronously run the function passed in. The sleep in increment_atomic_int should be
    // enough to keep this from being flakey.
    ASSERT_LT(current, 500);

    for (const auto& thread : threads) {
        ASSERT_TRUE(adb_thread_join(thread));
    }

    ASSERT_EQ(500, counter.load());
}