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

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