|
| 1 | +/* |
| 2 | +Copyright 2026 The Kubernetes Authors. |
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +Unless required by applicable law or agreed to in writing, software |
| 8 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +See the License for the specific language governing permissions and |
| 11 | +limitations under the License. |
| 12 | +*/ |
| 13 | +package io.kubernetes.client.extended.workqueue; |
| 14 | + |
| 15 | +import static org.assertj.core.api.Assertions.assertThat; |
| 16 | + |
| 17 | +import io.kubernetes.client.extended.controller.DefaultController; |
| 18 | +import io.kubernetes.client.extended.controller.builder.ControllerBuilder; |
| 19 | +import io.kubernetes.client.extended.controller.builder.DefaultControllerBuilder; |
| 20 | +import io.kubernetes.client.extended.controller.reconciler.Result; |
| 21 | +import io.kubernetes.client.extended.wait.Wait; |
| 22 | +import io.kubernetes.client.extended.workqueue.ratelimiter.DefaultControllerRateLimiter; |
| 23 | +import java.lang.reflect.Field; |
| 24 | +import java.time.Duration; |
| 25 | +import java.util.concurrent.CountDownLatch; |
| 26 | +import java.util.concurrent.ExecutorService; |
| 27 | +import java.util.concurrent.Executors; |
| 28 | +import java.util.concurrent.Future; |
| 29 | +import java.util.concurrent.TimeUnit; |
| 30 | +import org.junit.jupiter.api.Test; |
| 31 | +import org.junit.jupiter.params.ParameterizedTest; |
| 32 | +import org.junit.jupiter.params.provider.ValueSource; |
| 33 | + |
| 34 | +class DefaultDelayingQueueShutdownTest { |
| 35 | + |
| 36 | + @ParameterizedTest |
| 37 | + @ValueSource(booleans = {false, true}) |
| 38 | + void defaultDelayingQueueTerminates(boolean delayed) throws Exception { |
| 39 | + assertOwnedExecutorTerminates(new DefaultDelayingQueue<>(), delayed); |
| 40 | + } |
| 41 | + |
| 42 | + @ParameterizedTest |
| 43 | + @ValueSource(booleans = {false, true}) |
| 44 | + void defaultRateLimitingQueueTerminates(boolean delayed) throws Exception { |
| 45 | + assertOwnedExecutorTerminates(new DefaultRateLimitingQueue<>(), delayed); |
| 46 | + } |
| 47 | + |
| 48 | + private void assertOwnedExecutorTerminates(DefaultDelayingQueue<String> queue, boolean delayed) |
| 49 | + throws Exception { |
| 50 | + ExecutorService executor = ownedExecutor(queue); |
| 51 | + try { |
| 52 | + if (delayed) { |
| 53 | + queue.addAfter("later", Duration.ofDays(1)); |
| 54 | + assertThat( |
| 55 | + Wait.poll( |
| 56 | + Duration.ofMillis(10), |
| 57 | + Duration.ofSeconds(5), |
| 58 | + () -> queue.waitingForAddQueue.isEmpty())) |
| 59 | + .isTrue(); |
| 60 | + } |
| 61 | + queue.shutDown(); |
| 62 | + queue.shutDown(); |
| 63 | + assertThat(queue.isShuttingDown()).isTrue(); |
| 64 | + assertThat(executor.isShutdown()).isTrue(); |
| 65 | + assertThat(executor.awaitTermination(5, TimeUnit.SECONDS)).isTrue(); |
| 66 | + queue.addAfter("ignored", Duration.ZERO); |
| 67 | + assertThat(queue.get()).isNull(); |
| 68 | + } finally { |
| 69 | + queue.shutDown(); |
| 70 | + executor.shutdownNow(); |
| 71 | + assertThat(executor.awaitTermination(5, TimeUnit.SECONDS)).isTrue(); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + @ParameterizedTest |
| 76 | + @ValueSource(ints = {0, 1, 2}) |
| 77 | + void suppliedExecutorRemainsUsable(int constructor) throws Exception { |
| 78 | + ExecutorService executor = Executors.newSingleThreadExecutor(); |
| 79 | + DefaultDelayingQueue<String> queue = suppliedQueue(executor, constructor); |
| 80 | + try { |
| 81 | + queue.addAfter("later", Duration.ofDays(1)); |
| 82 | + assertThat( |
| 83 | + Wait.poll( |
| 84 | + Duration.ofMillis(10), |
| 85 | + Duration.ofSeconds(5), |
| 86 | + () -> queue.waitingForAddQueue.isEmpty())) |
| 87 | + .isTrue(); |
| 88 | + queue.shutDown(); |
| 89 | + queue.shutDown(); |
| 90 | + assertThat(executor.isShutdown()).isFalse(); |
| 91 | + // On a single-thread executor this can complete only after the waiting loop exits. |
| 92 | + assertThat(executor.submit(() -> "still usable").get(5, TimeUnit.SECONDS)) |
| 93 | + .isEqualTo("still usable"); |
| 94 | + } finally { |
| 95 | + queue.shutDown(); |
| 96 | + executor.shutdownNow(); |
| 97 | + assertThat(executor.awaitTermination(5, TimeUnit.SECONDS)).isTrue(); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + void shutdownBeforeWaitingTaskStartsDoesNotInterruptOtherWork() throws Exception { |
| 103 | + ExecutorService executor = Executors.newSingleThreadExecutor(); |
| 104 | + CountDownLatch started = new CountDownLatch(1); |
| 105 | + CountDownLatch release = new CountDownLatch(1); |
| 106 | + Future<Boolean> unrelated = |
| 107 | + executor.submit( |
| 108 | + () -> { |
| 109 | + started.countDown(); |
| 110 | + return release.await(5, TimeUnit.SECONDS); |
| 111 | + }); |
| 112 | + DefaultDelayingQueue<String> queue = new DefaultDelayingQueue<>(executor); |
| 113 | + try { |
| 114 | + assertThat(started.await(5, TimeUnit.SECONDS)).isTrue(); |
| 115 | + queue.shutDown(); |
| 116 | + queue.shutDown(); |
| 117 | + Field field = DefaultDelayingQueue.class.getDeclaredField("waitingTask"); |
| 118 | + field.setAccessible(true); |
| 119 | + assertThat(((Future<?>) field.get(queue)).isCancelled()).isTrue(); |
| 120 | + assertThat(unrelated.isDone()).isFalse(); |
| 121 | + release.countDown(); |
| 122 | + assertThat(unrelated.get(5, TimeUnit.SECONDS)).isTrue(); |
| 123 | + assertThat(executor.submit(() -> "still usable").get(5, TimeUnit.SECONDS)) |
| 124 | + .isEqualTo("still usable"); |
| 125 | + } finally { |
| 126 | + release.countDown(); |
| 127 | + queue.shutDown(); |
| 128 | + executor.shutdownNow(); |
| 129 | + assertThat(executor.awaitTermination(5, TimeUnit.SECONDS)).isTrue(); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + void controllerShutdownTerminatesQueueExecutor() throws Exception { |
| 135 | + DefaultController controller = |
| 136 | + (DefaultController) |
| 137 | + ControllerBuilder.defaultBuilder(null) |
| 138 | + .withReconciler(request -> new Result(false)) |
| 139 | + .build(); |
| 140 | + ExecutorService executor = |
| 141 | + ownedExecutor((DefaultDelayingQueue<?>) controller.getWorkQueue()); |
| 142 | + try { |
| 143 | + controller.shutdown(); |
| 144 | + controller.shutdown(); |
| 145 | + assertThat(executor.awaitTermination(5, TimeUnit.SECONDS)).isTrue(); |
| 146 | + } finally { |
| 147 | + controller.shutdown(); |
| 148 | + executor.shutdownNow(); |
| 149 | + assertThat(executor.awaitTermination(5, TimeUnit.SECONDS)).isTrue(); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + void replacingControllerQueueTerminatesOriginalExecutor() throws Exception { |
| 155 | + DefaultControllerBuilder builder = ControllerBuilder.defaultBuilder(null); |
| 156 | + Field field = DefaultControllerBuilder.class.getDeclaredField("workQueue"); |
| 157 | + field.setAccessible(true); |
| 158 | + DefaultDelayingQueue<?> queue = (DefaultDelayingQueue<?>) field.get(builder); |
| 159 | + ExecutorService executor = ownedExecutor(queue); |
| 160 | + try { |
| 161 | + builder.withWorkQueue(null); |
| 162 | + assertThat(executor.awaitTermination(5, TimeUnit.SECONDS)).isTrue(); |
| 163 | + } finally { |
| 164 | + queue.shutDown(); |
| 165 | + executor.shutdownNow(); |
| 166 | + assertThat(executor.awaitTermination(5, TimeUnit.SECONDS)).isTrue(); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + private static ExecutorService ownedExecutor(DefaultDelayingQueue<?> queue) throws Exception { |
| 171 | + Field field = DefaultDelayingQueue.class.getDeclaredField("ownedWaitingWorker"); |
| 172 | + field.setAccessible(true); |
| 173 | + return (ExecutorService) field.get(queue); |
| 174 | + } |
| 175 | + |
| 176 | + private static DefaultDelayingQueue<String> suppliedQueue( |
| 177 | + ExecutorService executor, int constructor) { |
| 178 | + switch (constructor) { |
| 179 | + case 0: |
| 180 | + return new DefaultDelayingQueue<>(executor); |
| 181 | + case 1: |
| 182 | + return new DefaultRateLimitingQueue<>(executor); |
| 183 | + default: |
| 184 | + return new DefaultRateLimitingQueue<>(executor, new DefaultControllerRateLimiter<>()); |
| 185 | + } |
| 186 | + } |
| 187 | +} |
0 commit comments