|
18 | 18 | import io.kubernetes.client.informer.ResourceEventHandler;
|
19 | 19 | import io.kubernetes.client.openapi.models.V1ObjectMeta;
|
20 | 20 | import io.kubernetes.client.openapi.models.V1Pod;
|
| 21 | +import java.time.Duration; |
| 22 | +import java.util.concurrent.Executors; |
21 | 23 | import org.junit.Rule;
|
22 | 24 | import org.junit.Test;
|
23 | 25 | import org.junit.contrib.java.lang.system.EnvironmentVariables;
|
@@ -61,6 +63,45 @@ public void testListenerAddition() throws InterruptedException {
|
61 | 63 | assertTrue(expectDeleteHandler.isSatisfied());
|
62 | 64 | }
|
63 | 65 |
|
| 66 | + @Test |
| 67 | + public void testShutdownGracefully() throws InterruptedException { |
| 68 | + SharedProcessor<V1Pod> sharedProcessor = |
| 69 | + new SharedProcessor<>(Executors.newCachedThreadPool(), Duration.ofSeconds(5)); |
| 70 | + TestWorker<V1Pod> quickWorker = new TestWorker<>(null, 0); |
| 71 | + quickWorker.setTask( |
| 72 | + () -> { |
| 73 | + try { |
| 74 | + // sleep 2s so that it could terminate within timeout(5s) |
| 75 | + Thread.sleep(2000); |
| 76 | + } catch (InterruptedException e) { |
| 77 | + } |
| 78 | + }); |
| 79 | + long before = System.currentTimeMillis(); |
| 80 | + sharedProcessor.addAndStartListener(quickWorker); |
| 81 | + sharedProcessor.stop(); |
| 82 | + // the stopping worker properly blocks the processor's stop call |
| 83 | + assertTrue(System.currentTimeMillis() - before >= 2000); |
| 84 | + |
| 85 | + sharedProcessor = new SharedProcessor<>(Executors.newCachedThreadPool(), Duration.ofSeconds(5)); |
| 86 | + TestWorker<V1Pod> slowWorker = new TestWorker<>(null, 0); |
| 87 | + final boolean[] interrupted = {false}; |
| 88 | + slowWorker.setTask( |
| 89 | + () -> { |
| 90 | + try { |
| 91 | + // sleep 10s so that it could be interrupted by shutdownNow() |
| 92 | + Thread.sleep(10 * 1000); |
| 93 | + } catch (InterruptedException e) { |
| 94 | + interrupted[0] = true; |
| 95 | + } |
| 96 | + }); |
| 97 | + sharedProcessor.addAndStartListener(slowWorker); |
| 98 | + sharedProcessor.stop(); |
| 99 | + // make sure the slow worker has set interrupted[0] = true |
| 100 | + Thread.sleep(1000); |
| 101 | + // the slow worker is interrupted upon timeout |
| 102 | + assertTrue(interrupted[0]); |
| 103 | + } |
| 104 | + |
64 | 105 | private static class ExpectingNoticationHandler<ApiType extends KubernetesObject>
|
65 | 106 | extends ProcessorListener<ApiType> {
|
66 | 107 |
|
@@ -99,4 +140,23 @@ public boolean isSatisfied() {
|
99 | 140 | return satisfied;
|
100 | 141 | }
|
101 | 142 | }
|
| 143 | + |
| 144 | + private static class TestWorker<ApiType extends KubernetesObject> |
| 145 | + extends ProcessorListener<ApiType> { |
| 146 | + |
| 147 | + private Runnable task; |
| 148 | + |
| 149 | + public TestWorker(ResourceEventHandler<ApiType> handler, long resyncPeriod) { |
| 150 | + super(handler, resyncPeriod); |
| 151 | + } |
| 152 | + |
| 153 | + public void setTask(Runnable task) { |
| 154 | + this.task = task; |
| 155 | + } |
| 156 | + |
| 157 | + @Override |
| 158 | + public void run() { |
| 159 | + this.task.run(); |
| 160 | + } |
| 161 | + } |
102 | 162 | }
|
0 commit comments