You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ShutdownTest.java 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * SonarQube Runner - Distribution
  3. * Copyright (C) 2011 SonarSource
  4. * dev@sonar.codehaus.org
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
  19. */
  20. package org.sonar.runner;
  21. import static org.mockito.Mockito.verify;
  22. import static org.fest.assertions.Assertions.assertThat;
  23. import org.mockito.MockitoAnnotations;
  24. import org.mockito.Mock;
  25. import org.junit.Test;
  26. import org.junit.Before;
  27. public class ShutdownTest {
  28. @Mock
  29. private Exit exit;
  30. private Shutdown shutdown;
  31. @Before
  32. public void setUp() {
  33. MockitoAnnotations.initMocks(this);
  34. shutdown = new Shutdown(exit);
  35. }
  36. @Test
  37. public void testShutdown() {
  38. shutdown.exit(3);
  39. verify(exit).exit(3);
  40. }
  41. @Test(timeout = 60_000)
  42. public void testWaitReady() throws InterruptedException {
  43. shutdown = new Shutdown(exit, 100_000);
  44. shutdown.signalReady(false);
  45. assertThat(shutdown.shouldExit()).isFalse();
  46. Thread t = new HookCaller();
  47. t.start();
  48. Thread.sleep(1000);
  49. assertThat(t.isAlive()).isTrue();
  50. assertThat(shutdown.shouldExit()).isTrue();
  51. shutdown.signalReady(true);
  52. t.join();
  53. }
  54. @Test(timeout = 60_000)
  55. public void testTimeout() throws InterruptedException {
  56. shutdown = new Shutdown(exit, 0);
  57. Thread t = new HookCaller();
  58. t.start();
  59. t.join();
  60. }
  61. private class HookCaller extends Thread {
  62. @Override
  63. public void run() {
  64. shutdown.hook.run();
  65. }
  66. }
  67. }