1818
1919package org .apache .hadoop .yarn .event ;
2020
21- import java .lang .reflect .Field ;
22- import java .lang .reflect .Modifier ;
2321import java .util .HashMap ;
2422import java .util .HashSet ;
2523import java .util .Map ;
2927
3028import org .junit .jupiter .api .Test ;
3129import org .junit .jupiter .api .Timeout ;
32- import org .slf4j .Logger ;
33-
3430import org .apache .hadoop .conf .Configuration ;
3531import org .apache .hadoop .metrics2 .AbstractMetric ;
3632import org .apache .hadoop .metrics2 .MetricsRecord ;
3733import org .apache .hadoop .metrics2 .impl .MetricsCollectorImpl ;
3834import org .apache .hadoop .metrics2 .lib .DefaultMetricsSystem ;
3935import org .apache .hadoop .test .GenericTestUtils ;
40- import org .apache .hadoop .test .ReflectionUtils ;
4136import org .apache .hadoop .yarn .conf .YarnConfiguration ;
4237import org .apache .hadoop .yarn .exceptions .YarnRuntimeException ;
4338import org .apache .hadoop .yarn .metrics .GenericEventTypeMetrics ;
4641import static org .junit .jupiter .api .Assertions .assertEquals ;
4742import static org .junit .jupiter .api .Assertions .assertTrue ;
4843import static org .junit .jupiter .api .Assertions .fail ;
49- import static org .mockito .Mockito .atLeastOnce ;
5044import static org .mockito .Mockito .doThrow ;
5145import static org .mockito .Mockito .mock ;
5246import static org .mockito .Mockito .spy ;
53- import static org .mockito .Mockito .verify ;
5447import static org .mockito .Mockito .when ;
5548
5649public class TestAsyncDispatcher {
@@ -178,19 +171,10 @@ void testPrintDispatcherEventDetails() throws Exception {
178171 YarnConfiguration conf = new YarnConfiguration ();
179172 conf .setInt (YarnConfiguration .
180173 YARN_DISPATCHER_PRINT_EVENTS_INFO_THRESHOLD , 5000 );
181- Logger log = mock (Logger .class );
182- AsyncDispatcher dispatcher = new AsyncDispatcher ();
174+ AsyncDispatcherForTest dispatcher = new AsyncDispatcherForTest ();
183175 dispatcher .init (conf );
184176
185- Field logger = AsyncDispatcher .class .getDeclaredField ("LOG" );
186- logger .setAccessible (true );
187- Field modifiers = ReflectionUtils .getModifiersField ();
188- modifiers .setAccessible (true );
189- modifiers .setInt (logger , logger .getModifiers () & ~Modifier .FINAL );
190- Object oldLog = logger .get (null );
191-
192177 try {
193- logger .set (null , log );
194178 dispatcher .register (TestEnum .class , new TestHandler ());
195179 dispatcher .start ();
196180
@@ -201,11 +185,9 @@ void testPrintDispatcherEventDetails() throws Exception {
201185 }
202186 Thread .sleep (2000 );
203187 //Make sure more than one event to take
204- verify (log , atLeastOnce ()).
205- info ("Latest dispatch event type: TestEventType" );
188+ assertEquals (dispatcher .getLastTriggered (), "Latest dispatch event type: TestEventType" );
206189 } finally {
207190 //... restore logger object
208- logger .set (null , oldLog );
209191 dispatcher .stop ();
210192 }
211193 }
@@ -224,19 +206,10 @@ public void testPrintDispatcherEventDetailsAvoidDeadLoopInternal()
224206 YarnConfiguration conf = new YarnConfiguration ();
225207 conf .setInt (YarnConfiguration .
226208 YARN_DISPATCHER_PRINT_EVENTS_INFO_THRESHOLD , 10 );
227- Logger log = mock (Logger .class );
228209 AsyncDispatcher dispatcher = new AsyncDispatcher ();
229210 dispatcher .init (conf );
230211
231- Field logger = AsyncDispatcher .class .getDeclaredField ("LOG" );
232- logger .setAccessible (true );
233- Field modifiers = ReflectionUtils .getModifiersField ();
234- modifiers .setAccessible (true );
235- modifiers .setInt (logger , logger .getModifiers () & ~Modifier .FINAL );
236- Object oldLog = logger .get (null );
237-
238212 try {
239- logger .set (null , log );
240213 dispatcher .register (TestEnum .class , new TestHandler (0 ));
241214 dispatcher .start ();
242215
@@ -247,8 +220,6 @@ public void testPrintDispatcherEventDetailsAvoidDeadLoopInternal()
247220 }
248221 Thread .sleep (3000 );
249222 } finally {
250- //... restore logger object
251- logger .set (null , oldLog );
252223 dispatcher .stop ();
253224 }
254225 }
@@ -419,4 +390,18 @@ void testDispatcherMetricsHistogram() throws Exception {
419390 }
420391
421392 }
393+
394+ private class AsyncDispatcherForTest extends AsyncDispatcher {
395+ private String lastTriggered = "" ;
396+
397+ @ Override
398+ protected void logTriggered (String message ) {
399+ LOG .info (message );
400+ lastTriggered = message ;
401+ }
402+
403+ public String getLastTriggered () {
404+ return lastTriggered ;
405+ }
406+ };
422407}
0 commit comments