@@ -391,7 +391,25 @@ private interface ConsumerLatency {
391391
392392 }
393393
394- private static class VariableConsumerLatency implements ConsumerLatency {
394+ private static boolean latencySleep (long delay ) {
395+ try {
396+ long ms = delay / 1000 ;
397+ Thread .sleep (ms );
398+ return true ;
399+ } catch (InterruptedException e ) {
400+ Thread .currentThread ().interrupt ();
401+ return false ;
402+ }
403+ }
404+
405+ private static boolean latencyBusyWait (long delay ) {
406+ delay = delay * 1000 ;
407+ long start = System .nanoTime ();
408+ while (System .nanoTime () - start < delay ) ;
409+ return true ;
410+ }
411+
412+ private static class VariableConsumerLatency implements ConsumerLatency {
395413
396414 private final ValueIndicator <Long > consumerLatenciesIndicator ;
397415
@@ -400,35 +418,17 @@ private VariableConsumerLatency(ValueIndicator<Long> consumerLatenciesIndicator)
400418 }
401419
402420 @ Override
403- public boolean simulateLatency ()
404- {
421+ public boolean simulateLatency () {
405422 long consumerLatencyInMicroSeconds = consumerLatenciesIndicator .getValue ();
406423 if (consumerLatencyInMicroSeconds <= 0 ) {
407424 return true ;
408425 } else if (consumerLatencyInMicroSeconds >= 1000 ) {
409- return sleep ( );
426+ return latencySleep ( consumerLatencyInMicroSeconds );
410427 } else {
411- return busyWait ( );
428+ return latencyBusyWait ( consumerLatencyInMicroSeconds );
412429 }
413430 }
414431
415- private boolean sleep () {
416- try {
417- long ms = consumerLatenciesIndicator .getValue () / 1000 ;
418- Thread .sleep (ms );
419- return true ;
420- } catch (InterruptedException e ) {
421- Thread .currentThread ().interrupt ();
422- return false ;
423- }
424- }
425-
426- private boolean busyWait () {
427- long delay = consumerLatenciesIndicator .getValue () * 1000 ;
428- long start = System .nanoTime ();
429- while (System .nanoTime () - start < delay );
430- return true ;
431- }
432432 }
433433
434434 private static class NoWaitConsumerLatency implements ConsumerLatency {
@@ -450,14 +450,7 @@ private ThreadSleepConsumerLatency(ValueIndicator<Long> consumerLatenciesIndicat
450450
451451 @ Override
452452 public boolean simulateLatency () {
453- try {
454- long ms = consumerLatenciesIndicator .getValue () / 1000 ;
455- Thread .sleep (ms );
456- return true ;
457- } catch (InterruptedException e ) {
458- Thread .currentThread ().interrupt ();
459- return false ;
460- }
453+ return latencySleep (consumerLatenciesIndicator .getValue ());
461454 }
462455 }
463456
@@ -472,10 +465,7 @@ private BusyWaitConsumerLatency(ValueIndicator<Long> consumerLatenciesIndicator)
472465
473466 @ Override
474467 public boolean simulateLatency () {
475- long delay = consumerLatenciesIndicator .getValue () * 1000 ;
476- long start = System .nanoTime ();
477- while (System .nanoTime () - start < delay );
478- return true ;
468+ return latencyBusyWait (consumerLatenciesIndicator .getValue ());
479469 }
480470 }
481471
0 commit comments