From cbd03ebf7b571348c7b4eff762ddeebdc41e3e2c Mon Sep 17 00:00:00 2001 From: gjoranv Date: Tue, 15 Dec 2020 16:20:54 +0100 Subject: Make the deconstruct executor private again. - Add helper method in test to wait for deconstruct to finish. --- .../jdisc/component/DeconstructorTest.java | 43 +++++++++++++--------- 1 file changed, 26 insertions(+), 17 deletions(-) (limited to 'container-disc/src/test') 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; @@ -28,14 +29,21 @@ public class DeconstructorTest { deconstructor = new Deconstructor(Deconstructor.Mode.RECONFIG, Duration.ZERO); } + @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 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; } } + } -- cgit v1.2.3