|
| 1 | +/* |
| 2 | + * Copyright Java Operator SDK Authors |
| 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 | +package io.javaoperatorsdk.operator; |
| 17 | + |
| 18 | +import java.util.Optional; |
| 19 | + |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | + |
| 22 | +import io.javaoperatorsdk.operator.sample.simple.TestCustomResource; |
| 23 | + |
| 24 | +import static org.assertj.core.api.Assertions.assertThat; |
| 25 | +import static org.awaitility.Awaitility.await; |
| 26 | + |
| 27 | +class OperatorKeepAliveTest { |
| 28 | + |
| 29 | + @Test |
| 30 | + void keepsTheJvmAliveWithANonDaemonThreadUntilStopped() { |
| 31 | + var operator = newOperator(); |
| 32 | + |
| 33 | + operator.keepAlive(); |
| 34 | + |
| 35 | + assertThat(keepAliveThread()).hasValueSatisfying(t -> assertThat(t.isDaemon()).isFalse()); |
| 36 | + |
| 37 | + operator.stop(); |
| 38 | + |
| 39 | + await().untilAsserted(() -> assertThat(keepAliveThread()).isEmpty()); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + void callingItAgainDoesNotStartAnotherThread() { |
| 44 | + var operator = newOperator(); |
| 45 | + try { |
| 46 | + operator.keepAlive(); |
| 47 | + operator.keepAlive(); |
| 48 | + |
| 49 | + assertThat(keepAliveThreads()).isEqualTo(1); |
| 50 | + } finally { |
| 51 | + operator.stop(); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void canBeStartedAgainAfterTheOperatorWasStopped() { |
| 57 | + var operator = newOperator(); |
| 58 | + operator.keepAlive(); |
| 59 | + operator.stop(); |
| 60 | + await().untilAsserted(() -> assertThat(keepAliveThread()).isEmpty()); |
| 61 | + |
| 62 | + operator.keepAlive(); |
| 63 | + try { |
| 64 | + assertThat(keepAliveThread()).isPresent(); |
| 65 | + } finally { |
| 66 | + operator.stop(); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + void noThreadIsStartedUnlessRequested() { |
| 72 | + var operator = newOperator(); |
| 73 | + try { |
| 74 | + assertThat(keepAliveThread()).isEmpty(); |
| 75 | + } finally { |
| 76 | + operator.stop(); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private static Operator newOperator() { |
| 81 | + return new Operator(MockKubernetesClient.client(TestCustomResource.class)); |
| 82 | + } |
| 83 | + |
| 84 | + private static long keepAliveThreads() { |
| 85 | + return Thread.getAllStackTraces().keySet().stream() |
| 86 | + .filter(t -> t.getName().equals("josdk-keep-alive") && t.isAlive()) |
| 87 | + .count(); |
| 88 | + } |
| 89 | + |
| 90 | + private static Optional<Thread> keepAliveThread() { |
| 91 | + return Thread.getAllStackTraces().keySet().stream() |
| 92 | + .filter(t -> t.getName().equals("josdk-keep-alive") && t.isAlive()) |
| 93 | + .findFirst(); |
| 94 | + } |
| 95 | +} |
0 commit comments