diff options
author | gjoranv <gv@verizonmedia.com> | 2020-12-15 16:20:54 +0100 |
---|---|---|
committer | gjoranv <gv@verizonmedia.com> | 2020-12-15 16:20:54 +0100 |
commit | cbd03ebf7b571348c7b4eff762ddeebdc41e3e2c (patch) | |
tree | 1e28b006079c3911876d21c4cebbb0218df98bbd /container-disc/src | |
parent | 0fc476b0a59950a75330d78fa3d96386dc0f572e (diff) |
Make the deconstruct executor private again.
- Add helper method in test to wait for deconstruct to finish.
Diffstat (limited to 'container-disc/src')
-rw-r--r-- | container-disc/src/main/java/com/yahoo/container/jdisc/component/Deconstructor.java | 3 | ||||
-rw-r--r-- | container-disc/src/test/java/com/yahoo/container/jdisc/component/DeconstructorTest.java | 43 |
2 files changed, 27 insertions, 19 deletions
diff --git a/container-disc/src/main/java/com/yahoo/container/jdisc/component/Deconstructor.java b/container-disc/src/main/java/com/yahoo/container/jdisc/component/Deconstructor.java index 55c7954c16c..b5f9e674502 100644 --- a/container-disc/src/main/java/com/yahoo/container/jdisc/component/Deconstructor.java +++ b/container-disc/src/main/java/com/yahoo/container/jdisc/component/Deconstructor.java @@ -46,8 +46,7 @@ public class Deconstructor implements ComponentDeconstructor { // are deconstructed, to prevent shutting down while deconstruct is in progress. } - // TODO: make private again - final ScheduledExecutorService executor = + private final ScheduledExecutorService executor = Executors.newScheduledThreadPool(2, ThreadFactoryFactory.getThreadFactory("component-deconstructor")); private final Mode mode; diff --git a/container-disc/src/test/java/com/yahoo/container/jdisc/component/DeconstructorTest.java b/container-disc/src/test/java/com/yahoo/container/jdisc/component/DeconstructorTest.java index 0b6dbf2c091..eef5b191e75 100644 --- a/container-disc/src/test/java/com/yahoo/container/jdisc/component/DeconstructorTest.java +++ b/container-disc/src/test/java/com/yahoo/container/jdisc/component/DeconstructorTest.java @@ -10,8 +10,9 @@ import org.junit.Before; import org.junit.Test; import java.time.Duration; +import java.time.Instant; import java.util.List; -import java.util.concurrent.TimeUnit; +import java.util.function.Supplier; import static java.util.Collections.emptyList; import static java.util.Collections.singleton; @@ -29,13 +30,20 @@ public class DeconstructorTest { } @Test + public void deconstruct_is_synchronous_in_shutdown_mode() { + deconstructor = new Deconstructor(Deconstructor.Mode.SHUTDOWN); + + var slowDeconstructComponent = new SlowDeconstructComponent(); + deconstructor.deconstruct(List.of(slowDeconstructComponent), emptyList()); + assertTrue(slowDeconstructComponent.destructed); + } + + @Test public void require_abstract_component_destructed() throws InterruptedException { TestAbstractComponent abstractComponent = new TestAbstractComponent(); - - // Done by executor, so it takes some time even with a 0 delay. deconstructor.deconstruct(List.of(abstractComponent), emptyList()); - deconstructor.executor.shutdown(); - deconstructor.executor.awaitTermination(1, TimeUnit.MINUTES); + + waitForDeconstructToComplete(() -> abstractComponent.destructed); assertTrue(abstractComponent.destructed); } @@ -43,17 +51,9 @@ public class DeconstructorTest { public void require_provider_destructed() throws InterruptedException { TestProvider provider = new TestProvider(); deconstructor.deconstruct(List.of(provider), emptyList()); - deconstructor.executor.shutdown(); - deconstructor.executor.awaitTermination(1, TimeUnit.MINUTES); - assertTrue(provider.destructed); - } - @Test - public void deconstruct_is_synchronous_in_shutdown_mode() { - deconstructor = new Deconstructor(Deconstructor.Mode.SHUTDOWN); - var slowDeconstructComponent = new SlowDeconstructComponent(); - deconstructor.deconstruct(List.of(slowDeconstructComponent), emptyList()); - assertTrue(slowDeconstructComponent.destructed); + waitForDeconstructToComplete(() -> provider.destructed); + assertTrue(provider.destructed); } @Test @@ -68,11 +68,19 @@ public class DeconstructorTest { var bundle = new UninstallableMockBundle(); // Done by executor, so it takes some time even with a 0 delay. deconstructor.deconstruct(emptyList(), singleton(bundle)); - deconstructor.executor.shutdown(); - deconstructor.executor.awaitTermination(1, TimeUnit.MINUTES); + + waitForDeconstructToComplete(() -> bundle.uninstalled); assertTrue(bundle.uninstalled); } + // Deconstruct is async in RECONFIG mode, so must wait even with a zero delay. + private void waitForDeconstructToComplete(Supplier<Boolean> destructed) throws InterruptedException { + var end = Instant.now().plusSeconds(30); + while (! destructed.get() && Instant.now().isBefore(end)) { + Thread.sleep(10); + } + } + private static class TestAbstractComponent extends AbstractComponent { boolean destructed = false; @Override public void deconstruct() { destructed = true; } @@ -112,4 +120,5 @@ public class DeconstructorTest { uninstalled = true; } } + } |