/* * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ #include #include #include #include #include #include namespace facebook { namespace flipper { namespace test { using folly::EventBase; class FlipperConnectionManagerImplTerminationTest : public ::testing::Test { protected: std::shared_ptr state; std::shared_ptr contextStore; std::unique_ptr sonarScheduler; std::unique_ptr connectionScheduler; void SetUp() override { // Folly singletons must be registered before they are used. // Without this, test fails in phabricator. folly::SingletonVault::singleton()->registrationComplete(); state = std::make_shared(); contextStore = std::make_shared(); sonarScheduler = std::make_unique(new EventBase()); connectionScheduler = std::make_unique(new EventBase()); } }; TEST_F( FlipperConnectionManagerImplTerminationTest, testNullEventBaseGetsRejected) { try { auto instance = std::make_shared( FlipperInitConfig{DeviceData{}, nullptr, connectionScheduler.get()}, state, contextStore); FAIL(); } catch (std::invalid_argument& e) { // Pass test } try { auto instance = std::make_shared( FlipperInitConfig{DeviceData{}, sonarScheduler.get(), nullptr}, state, contextStore); FAIL(); } catch (std::invalid_argument& e) { // Pass test } } TEST_F( FlipperConnectionManagerImplTerminationTest, testNonStartedEventBaseDoesntHang) { auto config = FlipperInitConfig{ DeviceData{}, sonarScheduler.get(), connectionScheduler.get()}; auto instance = std::make_shared( config, state, contextStore); instance->start(); } TEST_F( FlipperConnectionManagerImplTerminationTest, testStartedEventBaseDoesntHang) { auto flipperEventBase = new EventBase(); auto connectionEventBase = new EventBase(); auto flipperThread = std::thread([flipperEventBase]() { flipperEventBase->loopForever(); }); auto connectionThread = std::thread( [connectionEventBase]() { connectionEventBase->loopForever(); }); auto localSonarScheduler = std::make_unique(flipperEventBase); auto localConnectionScheduler = std::make_unique(connectionEventBase); auto config = FlipperInitConfig{ DeviceData{}, localSonarScheduler.get(), localConnectionScheduler.get()}; auto instance = std::make_shared( config, state, contextStore); instance->start(); flipperEventBase->terminateLoopSoon(); connectionEventBase->terminateLoopSoon(); flipperThread.join(); connectionThread.join(); } } // namespace test } // namespace flipper } // namespace facebook