Skip to content

Commit 1571877

Browse files
committed
COH-21718 Port CE related changes to verify the ServiceMonitor thread closes in DefaultCacheServerTest (14.1.1.0 --> ce/14.1.1.0)
[git-p4: depot-paths = "//dev/coherence-ce/release/coherence-ce-v14.1.1.0/": change = 81330]
1 parent ed5fd85 commit 1571877

File tree

5 files changed

+75
-44
lines changed

5 files changed

+75
-44
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright (c) 2020, Oracle and/or its affiliates.
3+
*
4+
* Licensed under the Universal Permissive License v 1.0 as shown at
5+
* http://oss.oracle.com/licenses/upl.
6+
*/
7+
package util;
8+
9+
import java.util.Collection;
10+
import java.util.HashSet;
11+
12+
/**
13+
* Thread related helpers.
14+
*
15+
* @author phf 2020.08.27
16+
*/
17+
public class ThreadHelper
18+
{
19+
/**
20+
* Get a collection of threads whose names start with the given prefix.
21+
*
22+
* @param sPrefix the thread name prefix
23+
*
24+
* @return the thread
25+
*/
26+
public static Collection<Thread> getThreadsByPrefix(String sPrefix)
27+
{
28+
ThreadGroup threadGroup = Thread.currentThread().getThreadGroup();
29+
30+
ThreadGroup parentThreadGroup;
31+
while ((parentThreadGroup = threadGroup.getParent()) != null)
32+
{
33+
threadGroup = parentThreadGroup;
34+
}
35+
36+
Thread[] aThreads = new Thread[threadGroup.activeCount() + 1];
37+
threadGroup.enumerate(aThreads);
38+
39+
Collection<Thread> colThreads = new HashSet<>();
40+
for (Thread thread : aThreads)
41+
{
42+
String sName = thread == null ? null : thread.getName();
43+
44+
if (sName != null && sName.startsWith(sPrefix))
45+
{
46+
colThreads.add(thread);
47+
}
48+
}
49+
50+
return colThreads;
51+
}
52+
}

prj/coherence/src/test/java/com/tangosol/net/DefaultCacheServerTest.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import com.oracle.coherence.common.base.Blocking;
1818

19+
import org.junit.After;
1920
import org.junit.Assert;
2021
import org.junit.Test;
2122

@@ -24,14 +25,24 @@
2425
import java.util.List;
2526
import java.util.Map;
2627

28+
import util.ThreadHelper;
29+
2730
import static com.oracle.bedrock.deferred.DeferredHelper.invoking;
2831
import static org.hamcrest.Matchers.is;
2932
import static org.junit.Assert.*;
3033
import static org.mockito.Mockito.*;
3134

32-
3335
public class DefaultCacheServerTest
3436
{
37+
/**
38+
* COH-21718 - Ensure the DCS ServiceMonitor thread has stopped.
39+
*/
40+
@After
41+
public void cleanup()
42+
{
43+
Eventually.assertDeferred(() -> ThreadHelper.getThreadsByPrefix("ServiceMonitor").isEmpty(), is(true));
44+
}
45+
3546
/**
3647
* Ensure a new {@link DefaultCacheServer} instance with a null
3748
* {@link ConfigurableCacheFactory} instance fails and passes with a

prj/coherence/src/test/java/com/tangosol/util/fsm/DynamicEventTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import org.junit.After;
1919
import org.junit.Test;
2020

21+
import util.ThreadHelper;
22+
2123
import static com.oracle.bedrock.testsupport.deferred.Eventually.within;
2224
import static org.hamcrest.Matchers.is;
2325
import static org.junit.Assert.assertThat;
@@ -35,7 +37,7 @@ public class DynamicEventTest
3537
@After
3638
public void cleanup()
3739
{
38-
Eventually.assertDeferred(() -> NonBlockingFiniteStateMachineTest.getThreadsByName(FSM_NAME_PREFIX).isEmpty(), is(true));
40+
Eventually.assertDeferred(() -> ThreadHelper.getThreadsByPrefix(FSM_NAME_PREFIX).isEmpty(), is(true));
3941
}
4042

4143
/**

prj/coherence/src/test/java/com/tangosol/util/fsm/NonBlockingFiniteStateMachineAnnotationTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import org.junit.After;
2727
import org.junit.Test;
2828

29+
import util.ThreadHelper;
30+
2931
import static org.hamcrest.Matchers.is;
3032
import static org.junit.Assert.assertThat;
3133

@@ -42,7 +44,7 @@ public class NonBlockingFiniteStateMachineAnnotationTest
4244
@After
4345
public void cleanup()
4446
{
45-
Eventually.assertDeferred(() -> NonBlockingFiniteStateMachineTest.getThreadsByName(FSM_NAME_PREFIX).isEmpty(), is(true));
47+
Eventually.assertDeferred(() -> ThreadHelper.getThreadsByPrefix(FSM_NAME_PREFIX).isEmpty(), is(true));
4648
}
4749

4850
/**

prj/coherence/src/test/java/com/tangosol/util/fsm/NonBlockingFiniteStateMachineTest.java

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@
2323
import org.junit.After;
2424
import org.junit.Test;
2525

26-
import java.util.Collection;
2726
import java.util.EnumSet;
28-
import java.util.HashSet;
2927

3028
import java.util.concurrent.TimeUnit;
3129
import java.util.concurrent.atomic.AtomicInteger;
3230

31+
import util.ThreadHelper;
32+
3333
import static org.hamcrest.core.Is.is;
3434
import static org.junit.Assert.assertThat;
3535
import static org.junit.Assert.assertTrue;
@@ -47,7 +47,7 @@ public class NonBlockingFiniteStateMachineTest
4747
@After
4848
public void cleanup()
4949
{
50-
Eventually.assertDeferred(() -> getThreadsByName(FSM_NAME_PREFIX).isEmpty(), is(true));
50+
Eventually.assertDeferred(() -> ThreadHelper.getThreadsByPrefix(FSM_NAME_PREFIX).isEmpty(), is(true));
5151
}
5252

5353
/**
@@ -367,47 +367,11 @@ public void testFSMWorkerThread()
367367

368368
machine.start();
369369

370-
Eventually.assertDeferred(() -> getThreadsByName(FSM_NAME_PREFIX).size(), is(1));
370+
Eventually.assertDeferred(() -> ThreadHelper.getThreadsByPrefix(FSM_NAME_PREFIX).size(), is(1));
371371

372372
machine.stop();
373373

374-
Eventually.assertDeferred(() -> getThreadsByName(FSM_NAME_PREFIX).isEmpty(), is(true));
375-
}
376-
377-
// ----- helper methods ------------------------------------------------
378-
379-
/**
380-
* Get a collection of threads whose names start with the given prefix.
381-
*
382-
* @param sPrefix the thread name prefix
383-
*
384-
* @return the thread
385-
*/
386-
public static Collection<Thread> getThreadsByName(String sPrefix)
387-
{
388-
ThreadGroup threadGroup = Thread.currentThread().getThreadGroup();
389-
390-
ThreadGroup parentThreadGroup;
391-
while ((parentThreadGroup = threadGroup.getParent()) != null)
392-
{
393-
threadGroup = parentThreadGroup;
394-
}
395-
396-
Thread[] aThreads = new Thread[threadGroup.activeCount() + 1];
397-
threadGroup.enumerate(aThreads);
398-
399-
Collection<Thread> colThreads = new HashSet<>();
400-
for (Thread thread : aThreads)
401-
{
402-
String sName = thread == null ? null : thread.getName();
403-
404-
if (sName != null && sName.startsWith(sPrefix))
405-
{
406-
colThreads.add(thread);
407-
}
408-
}
409-
410-
return colThreads;
374+
Eventually.assertDeferred(() -> ThreadHelper.getThreadsByPrefix(FSM_NAME_PREFIX).isEmpty(), is(true));
411375
}
412376

413377
// ----- constants ------------------------------------------------------

0 commit comments

Comments
 (0)