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.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * SonarQube Runner - CLI - Distribution
  3. * Copyright (C) 2011 SonarSource
  4. * sonarqube@googlegroups.com
  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.cli;
  21. import com.jayway.awaitility.Duration;
  22. import java.util.concurrent.Callable;
  23. import java.util.concurrent.TimeUnit;
  24. import org.junit.Before;
  25. import org.junit.Test;
  26. import org.mockito.Mock;
  27. import org.mockito.MockitoAnnotations;
  28. import static com.jayway.awaitility.Awaitility.await;
  29. import static org.fest.assertions.Assertions.assertThat;
  30. import static org.mockito.Mockito.verify;
  31. public class ShutdownTest {
  32. @Mock
  33. private Exit exit;
  34. private Shutdown shutdown;
  35. @Before
  36. public void setUp() {
  37. MockitoAnnotations.initMocks(this);
  38. shutdown = new Shutdown(exit, true);
  39. }
  40. @Test
  41. public void testShutdown() {
  42. shutdown.exit(3);
  43. verify(exit).exit(3);
  44. }
  45. @Test(timeout = 60_000)
  46. public void testWaitReady() throws InterruptedException {
  47. shutdown = new Shutdown(exit, true, 100_000);
  48. shutdown.signalReady(false);
  49. assertThat(shutdown.shouldExit()).isFalse();
  50. final Thread t = new HookCaller();
  51. t.start();
  52. await().atMost(Duration.TWO_SECONDS).pollDelay(50, TimeUnit.MILLISECONDS).until(new Callable<Boolean>() {
  53. @Override
  54. public Boolean call() throws Exception {
  55. return t.isAlive();
  56. }
  57. });
  58. assertThat(shutdown.shouldExit()).isTrue();
  59. shutdown.signalReady(true);
  60. t.join();
  61. }
  62. @Test(timeout = 60_000)
  63. public void testTimeout() throws InterruptedException {
  64. shutdown = new Shutdown(exit, true, 0);
  65. Thread t = new HookCaller();
  66. t.start();
  67. t.join();
  68. }
  69. private class HookCaller extends Thread {
  70. @Override
  71. public void run() {
  72. shutdown.hook.run();
  73. }
  74. }
  75. }