1515package org .apache .geode .internal .monitoring ;
1616
1717import static org .assertj .core .api .Assertions .assertThat ;
18- import static org .junit .Assert .assertFalse ;
19- import static org .junit .Assert .assertTrue ;
18+ import static org .junit .jupiter .api .Assertions .assertFalse ;
19+ import static org .junit .jupiter .api .Assertions .assertNotNull ;
20+ import static org .junit .jupiter .api .Assertions .assertNull ;
21+ import static org .junit .jupiter .api .Assertions .assertTrue ;
2022import static org .mockito .ArgumentMatchers .any ;
2123import static org .mockito .Mockito .mock ;
2224import static org .mockito .Mockito .never ;
2325import static org .mockito .Mockito .times ;
2426import static org .mockito .Mockito .verify ;
2527
26- import org .junit .After ;
27- import org .junit .Before ;
28- import org .junit .Test ;
28+ import org .junit .jupiter .api .AfterEach ;
29+ import org .junit .jupiter .api .Test ;
2930
3031import org .apache .geode .internal .monitoring .ThreadsMonitoring .Mode ;
3132import org .apache .geode .internal .monitoring .executor .AbstractExecutor ;
3233import org .apache .geode .internal .monitoring .executor .FunctionExecutionPooledExecutorGroup ;
34+ import org .apache .geode .internal .monitoring .executor .PooledExecutorGroup ;
3335
3436/**
3537 * Contains simple tests for the {@link org.apache.geode.internal.monitoring.ThreadsMonitoringImpl}.
3638 *
3739 * @since Geode 1.5
3840 */
39- public class ThreadsMonitoringImplJUnitTest {
41+ public class ThreadsMonitoringImplTest {
42+ private static final int TIME_LIMIT_MILLIS = 1000 ;
4043
4144 private ThreadsMonitoringImpl threadsMonitoringImpl ;
4245
43- @ Before
44- public void before () {
45- threadsMonitoringImpl = new ThreadsMonitoringImpl (null , 100000 , 0 , false );
46- }
47-
48- @ After
46+ @ AfterEach
4947 public void after () {
5048 threadsMonitoringImpl .close ();
5149 }
@@ -55,6 +53,7 @@ public void after() {
5553 */
5654 @ Test
5755 public void testStartMonitor () {
56+ threadsMonitoringImpl = new ThreadsMonitoringImpl (null , 100000 , 0 , false );
5857 assertTrue (threadsMonitoringImpl .startMonitor (Mode .FunctionExecutor ));
5958 assertTrue (threadsMonitoringImpl .startMonitor (Mode .PooledExecutor ));
6059 assertTrue (threadsMonitoringImpl .startMonitor (Mode .SerialQueuedExecutor ));
@@ -67,6 +66,7 @@ public void testStartMonitor() {
6766
6867 @ Test
6968 public void verifyMonitorLifeCycle () {
69+ threadsMonitoringImpl = new ThreadsMonitoringImpl (null , 100000 , 0 , false );
7070 assertFalse (threadsMonitoringImpl .isMonitoring ());
7171 threadsMonitoringImpl .startMonitor (Mode .FunctionExecutor );
7272 assertTrue (threadsMonitoringImpl .isMonitoring ());
@@ -76,6 +76,7 @@ public void verifyMonitorLifeCycle() {
7676
7777 @ Test
7878 public void verifyExecutorMonitoringLifeCycle () {
79+ threadsMonitoringImpl = new ThreadsMonitoringImpl (null , 100000 , 0 , false );
7980 AbstractExecutor executor =
8081 threadsMonitoringImpl .createAbstractExecutor (Mode .P2PReaderExecutor );
8182 assertThat (threadsMonitoringImpl .isMonitoring (executor )).isFalse ();
@@ -95,24 +96,28 @@ public void verifyExecutorMonitoringLifeCycle() {
9596
9697 @ Test
9798 public void createAbstractExecutorIsAssociatedWithCallingThread () {
99+ threadsMonitoringImpl = new ThreadsMonitoringImpl (null , 100000 , 0 , false );
98100 AbstractExecutor executor = threadsMonitoringImpl .createAbstractExecutor (Mode .FunctionExecutor );
99101 assertThat (executor .getThreadID ()).isEqualTo (Thread .currentThread ().getId ());
100102 }
101103
102104 @ Test
103105 public void createAbstractExecutorDoesNotSetStartTime () {
106+ threadsMonitoringImpl = new ThreadsMonitoringImpl (null , 100000 , 0 , false );
104107 AbstractExecutor executor = threadsMonitoringImpl .createAbstractExecutor (Mode .FunctionExecutor );
105108 assertThat (executor .getStartTime ()).isEqualTo (0 );
106109 }
107110
108111 @ Test
109112 public void createAbstractExecutorSetsNumIterationsStuckToZero () {
113+ threadsMonitoringImpl = new ThreadsMonitoringImpl (null , 100000 , 0 , false );
110114 AbstractExecutor executor = threadsMonitoringImpl .createAbstractExecutor (Mode .FunctionExecutor );
111115 assertThat (executor .getNumIterationsStuck ()).isEqualTo ((short ) 0 );
112116 }
113117
114118 @ Test
115119 public void createAbstractExecutorSetsExpectedGroupName () {
120+ threadsMonitoringImpl = new ThreadsMonitoringImpl (null , 100000 , 0 , false );
116121 AbstractExecutor executor = threadsMonitoringImpl .createAbstractExecutor (Mode .FunctionExecutor );
117122 assertThat (executor .getGroupName ()).isEqualTo (FunctionExecutionPooledExecutorGroup .GROUPNAME );
118123 }
@@ -122,16 +127,17 @@ public void createAbstractExecutorSetsExpectedGroupName() {
122127 */
123128 @ Test
124129 public void testClosure () {
125- ThreadsMonitoringImpl liveMonitor = new ThreadsMonitoringImpl (null , 100000 , 0 , true );
126- assertTrue ( liveMonitor .getThreadsMonitoringProcess () != null );
127- assertFalse (liveMonitor .isClosed ());
128- liveMonitor .close ();
129- assertTrue (liveMonitor .isClosed ());
130- assertFalse ( liveMonitor .getThreadsMonitoringProcess () != null );
130+ threadsMonitoringImpl = new ThreadsMonitoringImpl (null , 100000 , 0 , true );
131+ assertNotNull ( threadsMonitoringImpl .getThreadsMonitoringProcess ());
132+ assertFalse (threadsMonitoringImpl .isClosed ());
133+ threadsMonitoringImpl .close ();
134+ assertTrue (threadsMonitoringImpl .isClosed ());
135+ assertNull ( threadsMonitoringImpl .getThreadsMonitoringProcess ());
131136 }
132137
133138 @ Test
134139 public void updateThreadStatusCallsSetStartTime () {
140+ threadsMonitoringImpl = new ThreadsMonitoringImpl (null , 100000 , 0 , false );
135141 AbstractExecutor executor = mock (AbstractExecutor .class );
136142 long threadId = Thread .currentThread ().getId ();
137143
@@ -142,8 +148,52 @@ public void updateThreadStatusCallsSetStartTime() {
142148
143149 @ Test
144150 public void updateThreadStatusWithoutExecutorInMapDoesNotCallSetStartTime () {
151+ threadsMonitoringImpl = new ThreadsMonitoringImpl (null , 100000 , 0 , false );
145152 AbstractExecutor executor = mock (AbstractExecutor .class );
146153 threadsMonitoringImpl .updateThreadStatus ();
147154 verify (executor , never ()).setStartTime (any (Long .class ));
148155 }
156+
157+ /**
158+ * Tests that indeed thread is considered stuck when it should
159+ */
160+ @ Test
161+ public void testThreadIsStuck () {
162+ threadsMonitoringImpl = new ThreadsMonitoringImpl (null , 100000 , TIME_LIMIT_MILLIS );
163+
164+ final long threadID = 123456 ;
165+
166+ AbstractExecutor absExtgroup = new PooledExecutorGroup ();
167+ absExtgroup .setStartTime (absExtgroup .getStartTime () - TIME_LIMIT_MILLIS - 1 );
168+
169+ threadsMonitoringImpl .getMonitorMap ().put (threadID , absExtgroup );
170+
171+ assertTrue (threadsMonitoringImpl .getThreadsMonitoringProcess ().mapValidation ());
172+ }
173+
174+ @ Test
175+ public void monitorHandlesDefunctThread () {
176+ threadsMonitoringImpl = new ThreadsMonitoringImpl (null , 100000 , TIME_LIMIT_MILLIS );
177+ final long threadID = Long .MAX_VALUE ;
178+
179+ AbstractExecutor absExtgroup = new PooledExecutorGroup (threadID );
180+ absExtgroup .setStartTime (absExtgroup .getStartTime () - TIME_LIMIT_MILLIS - 1 );
181+
182+ threadsMonitoringImpl .getMonitorMap ().put (threadID , absExtgroup );
183+
184+ assertTrue (threadsMonitoringImpl .getThreadsMonitoringProcess ().mapValidation ());
185+ }
186+
187+ /**
188+ * Tests that indeed thread is NOT considered stuck when it shouldn't
189+ */
190+ @ Test
191+ public void testThreadIsNotStuck () {
192+ threadsMonitoringImpl = new ThreadsMonitoringImpl (null , 100000 , TIME_LIMIT_MILLIS );
193+
194+ threadsMonitoringImpl .startMonitor (Mode .PooledExecutor );
195+
196+ assertFalse (threadsMonitoringImpl .getThreadsMonitoringProcess ().mapValidation ());
197+ }
198+
149199}
0 commit comments