Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

ShutdownTest.java 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * SonarQube Scanner
  3. * Copyright (C) 2011-2016 SonarSource SA
  4. * mailto:contact AT sonarsource DOT 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 License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonarsource.scanner.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 org.sonarsource.scanner.cli.Exit;
  29. import org.sonarsource.scanner.cli.Shutdown;
  30. import static com.jayway.awaitility.Awaitility.await;
  31. import static org.fest.assertions.Assertions.assertThat;
  32. import static org.mockito.Mockito.verify;
  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, true);
  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, true, 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, true, 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. }