AsyncSignalHandlerTest.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Copyright 2017-present Facebook, Inc.
  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 <folly/io/async/AsyncSignalHandler.h>
  17. #include <folly/io/async/EventBase.h>
  18. #include <folly/portability/GTest.h>
  19. using namespace folly;
  20. namespace {
  21. class TestSignalHandler : public AsyncSignalHandler {
  22. public:
  23. using AsyncSignalHandler::AsyncSignalHandler;
  24. void signalReceived(int /* signum */) noexcept override {
  25. called = true;
  26. }
  27. bool called{false};
  28. };
  29. } // namespace
  30. TEST(AsyncSignalHandler, basic) {
  31. EventBase evb;
  32. TestSignalHandler handler{&evb};
  33. handler.registerSignalHandler(SIGUSR1);
  34. kill(getpid(), SIGUSR1);
  35. EXPECT_FALSE(handler.called);
  36. evb.loopOnce(EVLOOP_NONBLOCK);
  37. EXPECT_TRUE(handler.called);
  38. }
  39. TEST(AsyncSignalHandler, attachEventBase) {
  40. TestSignalHandler handler{nullptr};
  41. EXPECT_FALSE(handler.getEventBase());
  42. EventBase evb;
  43. handler.attachEventBase(&evb);
  44. EXPECT_EQ(&evb, handler.getEventBase());
  45. handler.registerSignalHandler(SIGUSR1);
  46. kill(getpid(), SIGUSR1);
  47. EXPECT_FALSE(handler.called);
  48. evb.loopOnce(EVLOOP_NONBLOCK);
  49. EXPECT_TRUE(handler.called);
  50. handler.unregisterSignalHandler(SIGUSR1);
  51. handler.detachEventBase();
  52. EXPECT_FALSE(handler.getEventBase());
  53. }