-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize the code in
TimeBounds
. (#270)
* fix: parameters checking rule in TimeBounds. * feat: add `TimeBounds.afterNow` and tests for `TimeBounds` * style: Optimize code style in `TimeBounds`. * rename `TimeBounds.afterNow` to `TimeBounds.expiresAfter`.
- Loading branch information
Showing
2 changed files
with
109 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package org.stellar.sdk; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class TimeBoundsTest { | ||
@Test | ||
public void testSetTimeBoundsNegativeMinTime() { | ||
try { | ||
new TimeBounds(-1, 300); | ||
fail(); | ||
} catch (IllegalArgumentException e) { | ||
assertEquals("minTime cannot be negative", e.getMessage()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testSetTimeBoundsNegativeMaxTime() { | ||
try { | ||
new TimeBounds(1, -300); | ||
fail(); | ||
} catch (IllegalArgumentException e) { | ||
assertEquals("maxTime cannot be negative", e.getMessage()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testSetTimeBoundsMinTimeGreatThanMaxTime() { | ||
try { | ||
new TimeBounds(300, 1); | ||
fail(); | ||
} catch (IllegalArgumentException e) { | ||
assertEquals("minTime must be >= maxTime", e.getMessage()); | ||
} | ||
} | ||
|
||
@Test | ||
public void TestSetTimeoutInfinite() { | ||
TimeBounds timebounds = new TimeBounds(300, 0); | ||
assertEquals(300, timebounds.getMinTime()); | ||
assertEquals(0, timebounds.getMaxTime()); | ||
} | ||
|
||
@Test | ||
public void TestSetTimeoutInfiniteBothZero() { | ||
TimeBounds timebounds = new TimeBounds(0, 0); | ||
assertEquals(0, timebounds.getMinTime()); | ||
assertEquals(0, timebounds.getMaxTime()); | ||
} | ||
|
||
@Test | ||
public void TestSetTimeout() { | ||
TimeBounds timebounds = new TimeBounds(1, 300); | ||
assertEquals(1, timebounds.getMinTime()); | ||
assertEquals(300, timebounds.getMaxTime()); | ||
} | ||
|
||
@Test | ||
public void TestSetTimeoutMinEqualMax() { | ||
TimeBounds timebounds = new TimeBounds(300, 300); | ||
assertEquals(300, timebounds.getMinTime()); | ||
assertEquals(300, timebounds.getMaxTime()); | ||
} | ||
|
||
@Test | ||
public void TestTimeoutWithTimeout() { | ||
long timeout = 300; | ||
TimeBounds timebounds = TimeBounds.expiresAfter(timeout); | ||
long now = System.currentTimeMillis() / 1000L; | ||
assertEquals(0, timebounds.getMinTime()); | ||
assertTrue(timebounds.getMaxTime() - timeout <= now && timebounds.getMaxTime() - timeout >= now - 1); | ||
} | ||
} | ||
|