Skip to content

Commit

Permalink
Fix ListenerErrorTest
Browse files Browse the repository at this point in the history
Signed-off-by: Jorge Bescos Gascon <jorge.bescos.gascon@oracle.com>
  • Loading branch information
jbescos committed Jan 23, 2024
1 parent c19f226 commit 07a0607
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -16,6 +16,9 @@

package org.glassfish.hk2.runlevel.tests.listener;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;

import javax.annotation.PreDestroy;

import org.glassfish.hk2.runlevel.RunLevel;
Expand All @@ -29,17 +32,22 @@
*/
@RunLevel(5)
public class LevelFiveService {
private boolean preDestroyCalled = false;


private final CountDownLatch latch = new CountDownLatch(1);
private AtomicBoolean preDestroyCalled = new AtomicBoolean(false);

@SuppressWarnings("unused")
@PreDestroy
private void preDestroy() {
preDestroyCalled = true;

preDestroyCalled.set(true);
latch.countDown();
}

/* package */ boolean isPreDestroyCalled() {
return preDestroyCalled;
return preDestroyCalled.get();
}

/* package */ CountDownLatch latch() {
return latch;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -16,6 +16,8 @@

package org.glassfish.hk2.runlevel.tests.listener;

import java.util.concurrent.TimeUnit;

import org.glassfish.hk2.api.Descriptor;
import org.glassfish.hk2.api.MultiException;
import org.glassfish.hk2.api.ServiceLocator;
Expand Down Expand Up @@ -165,9 +167,10 @@ public void testComingDownDoesNotCallOtherServicesNoThreads() {
/**
* Ensures the user can halt the downward level progression if a service
* failed when going down
* @throws InterruptedException
*/
@Test
public void testHaltLevelRegressionOnError() {
public void testHaltLevelRegressionOnError() throws InterruptedException {
ServiceLocator locator = Utilities.getServiceLocator(LevelFiveDownErrorService.class,
LevelFiveService.class,
OnProgressLevelChangerListener.class);
Expand All @@ -192,16 +195,19 @@ public void testHaltLevelRegressionOnError() {
OnProgressLevelChangerListener listener = locator.getService(OnProgressLevelChangerListener.class);

Assert.assertEquals(4, listener.getLatestOnProgress());


levelFiveService.latch().await(100, TimeUnit.MILLISECONDS);

Assert.assertTrue(levelFiveService.isPreDestroyCalled());
}

/**
* Ensures the user can halt the downward level progression if a service
* failed when going down
* @throws InterruptedException
*/
@Test
public void testHaltLevelRegressionOnErrorNoThreads() {
public void testHaltLevelRegressionOnErrorNoThreads() throws InterruptedException {
ServiceLocator locator = Utilities.getServiceLocator(LevelFiveDownErrorService.class,
LevelFiveService.class,
OnProgressLevelChangerListener.class);
Expand All @@ -228,6 +234,8 @@ public void testHaltLevelRegressionOnErrorNoThreads() {

Assert.assertEquals(4, listener.getLatestOnProgress());

levelFiveService.latch().await(100, TimeUnit.MILLISECONDS);

Assert.assertTrue(levelFiveService.isPreDestroyCalled());
}

Expand Down

0 comments on commit 07a0607

Please sign in to comment.