TimeoutHelper { static int toMillis(long timeout, TimeUnit unit) { if (timeout == 0) { return 0; } int millisecs = (int) unit.toMillis(timeout); if ((millisecs == 0)) { return 1; } if (millisecs < 0) { return Integer.MAX_VALUE; } return millisecs; }    }
@Test public void toMillisLessThanOneMillisec() { assertEquals(1, TimeoutHelper.toMillis(999999, TimeUnit.NANOSECONDS)); assertEquals(1, TimeoutHelper.toMillis(999, TimeUnit.MICROSECONDS)); }
@Test public void toMillisGreaterThanMaxIntMillisecs() { assertEquals(Integer.MAX_VALUE, TimeoutHelper.toMillis(Long.MAX_VALUE, TimeUnit.MILLISECONDS)); assertEquals(Integer.MAX_VALUE, TimeoutHelper.toMillis(Long.MAX_VALUE, TimeUnit.SECONDS)); assertEquals(Integer.MAX_VALUE, TimeoutHelper.toMillis(Integer.MAX_VALUE, TimeUnit.SECONDS)); assertEquals(Integer.MAX_VALUE, TimeoutHelper.toMillis(100000, TimeUnit.DAYS)); }
@Test public void toMillisZero() { assertEquals(0, TimeoutHelper.toMillis(0, TimeUnit.NANOSECONDS)); assertEquals(0, TimeoutHelper.toMillis(0, TimeUnit.MICROSECONDS)); assertEquals(0, TimeoutHelper.toMillis(0, TimeUnit.DAYS)); }
@Test public void toMillisGreaterThanZero() { assertEquals(3, TimeoutHelper.toMillis(3100000, TimeUnit.NANOSECONDS)); assertEquals(4, TimeoutHelper.toMillis(4100, TimeUnit.MICROSECONDS)); }