-
Notifications
You must be signed in to change notification settings - Fork 623
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace timeout + timeUnit with Duration (#30)
* replace timeout and timeoutUnit with duration * add changelog
- Loading branch information
Showing
7 changed files
with
82 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
Changelog | ||
========= | ||
|
||
This project follows [semantic versioning](http://semver.org/). | ||
|
||
Version 2.0.0, released YYYY-MM-DD TODO | ||
---------------------------------- | ||
|
||
[Closed Issues](https://github.com/kotlintest/kotlintest/issues?utf8=%E2%9C%93&q=is%3Aclosed+milestone%3A2.0) | ||
|
||
### Added | ||
|
||
nothing | ||
|
||
### Changed | ||
|
||
#### Replaced `timeout` + `timeUnit` with `Duration` ([#29](https://github.com/kotlintest/kotlintest/issues/29)) | ||
|
||
You can now write `config(timeout = 2.seconds)` instead of | ||
`config(timeout = 2, timeoutUnit = TimeUnit.SECONDS)`. | ||
|
||
### Deprecated | ||
|
||
nothing | ||
|
||
### Removed | ||
|
||
nothing | ||
|
||
### Fixed | ||
|
||
nothing |
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,31 @@ | ||
package io.kotlintest | ||
|
||
import java.util.concurrent.TimeUnit | ||
|
||
data class Duration(val amount: Long, val timeUnit: TimeUnit) { | ||
|
||
companion object { | ||
// Actually limited to 9223372036854775807 days, so unless you are very patient, it is unlimited ;-) | ||
val unlimited = Duration(amount = Long.MAX_VALUE, timeUnit = TimeUnit.DAYS) | ||
} | ||
|
||
val Long.days: Duration | ||
get() = Duration(this, TimeUnit.DAYS) | ||
|
||
val Long.hours: Duration | ||
get() = Duration(this, TimeUnit.HOURS) | ||
|
||
val Long.microseconds: Duration | ||
get() = Duration(this, TimeUnit.MICROSECONDS) | ||
|
||
val Long.milliseconds: Duration | ||
get() = Duration(this, TimeUnit.MILLISECONDS) | ||
|
||
val Long.nanoseconds: Duration | ||
get() = Duration(this, TimeUnit.NANOSECONDS) | ||
|
||
val Long.seconds: Duration | ||
get() = Duration(this, TimeUnit.SECONDS) | ||
|
||
val nanoseconds: Long = timeUnit.toNanos(amount) | ||
} |
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 |
---|---|---|
@@ -1,20 +1,19 @@ | ||
package io.kotlintest | ||
|
||
import java.util.concurrent.TimeUnit | ||
|
||
interface Eventually { | ||
|
||
fun eventually(duration: Long, unit: TimeUnit, f: () -> Unit): Unit { | ||
val end = System.nanoTime() + unit.toNanos(duration) | ||
fun eventually(duration: Duration, f: () -> Unit): Unit { | ||
val end = System.nanoTime() + duration.nanoseconds | ||
var times = 0 | ||
while (System.nanoTime() < end) { | ||
try { | ||
f() | ||
return | ||
} catch (e: Exception) { | ||
// ignore and proceed | ||
} | ||
times++ | ||
} | ||
throw TestFailedException("Test failed after $duration $unit; attempted $times times") | ||
throw TestFailedException("Test failed after ${duration.amount} ${duration.timeUnit}; attempted $times times") | ||
} | ||
} |
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
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