final class Span extends Serializable
A time span.
A Span is used to express time spans in ScalaTest, in constructs such as the
failAfter method of trait Timeouts,
the timeLimit field of trait
TimeLimitedTests, and
the timeouts of traits Eventually,
and AsyncAssertions. Here's an example:
import org.scalatest.time.Span import org.scalatest.time.Millis import org.scalatest.concurrent.Timeouts._
failAfter(Span(100, Millis)) { // ... }
If you prefer you can mix in or import the members of SpanSugar and place a units value after the timeout value.
Here are some examples:
import org.scalatest.time.SpanSugar._ import org.scalatest.concurrent.Timeouts._
failAfter(100 millis) { // ... }
failAfter(1 second) { // ... }
In addition to expression the numeric value with an Int or a Long, you
can also express it via a Float or Double. Here are some examples:
import org.scalatest.time.Span import org.scalatest.time.Seconds import org.scalatest.concurrent.Timeouts._
failAfter(Span(1.5, Seconds)) { // ... }
import org.scalatest.time.SpanSugar._
failAfter(0.8 seconds) { // ... }
Internally, a Span is expressed in terms of a Long number of nanoseconds. Thus, the maximum
time span that can be represented is Long.MaxValue nanoseconds, or approximately 292 years.
Hopefully these won't be "famous last words," but 292 years should be sufficient for software testing purposes.
Any attempt to create a Span longer than Long.MaxValue nanoseconds will be met with
an IllegalArgumentException:
Span(Long.MaxValue, Nanoseconds) // Produces the longest possible time.Span Span(Long.MaxValue, Seconds) // Produces an IllegalArgumentException
All of class Span's constructors are private. The only way you can create a new Span is
via one of the two apply factory methods in Span's
companion object. Here is a table showing one example of each numeric type and unit value:
Int
|
Long
|
Float
|
Double
|
|---|---|---|---|
| Span(1, Nanosecond) | Span(1L, Nanosecond) | Span(1.0F, Nanosecond) | Span(1.0, Nanosecond) |
| Span(100, Nanoseconds) | Span(100L, Nanoseconds) | Span(99.8F, Nanoseconds) | Span(99.8, Nanoseconds) |
| Span(1, Microsecond) | Span(1L, Microsecond) | Span(1.0F, Microsecond) | Span(1.0, Microsecond) |
| Span(100, Microseconds) | Span(100L, Microseconds) | Span(99.8F, Microseconds) | Span(99.8, Microseconds) |
| Span(1, Millisecond) | Span(1L, Millisecond) | Span(1.0F, Millisecond) | Span(1.0, Millisecond) |
| Span(100, Milliseconds) | Span(100L, Milliseconds) | Span(99.8F, Milliseconds) | Span(99.8, Milliseconds) |
| Span(100, Millis) | Span(100L, Millis) | Span(99.8F, Millis) | Span(99.8, Millis) |
| Span(1, Second) | Span(1L, Second) | Span(1.0F, Second) | Span(1.0, Second) |
| Span(100, Seconds) | Span(100L, Seconds) | Span(99.8F, Seconds) | Span(99.8, Seconds) |
| Span(1, Minute) | Span(1L, Minute) | Span(1.0F, Minute) | Span(1.0, Minute) |
| Span(100, Minutes) | Span(100L, Minutes) | Span(99.8F, Minutes) | Span(99.8, Minutes) |
| Span(1, Hour) | Span(1L, Hour) | Span(1.0F, Hour) | Span(1.0, Hour) |
| Span(100, Hours) | Span(100L, Hours) | Span(99.8F, Hours) | Span(99.8, Hours) |
| Span(1, Day) | Span(1L, Day) | Span(1.0F, Day) | Span(1.0, Day) |
| Span(100, Days) | Span(100L, Days) | Span(99.8F, Days) | Span(99.8, Days) |
Note that because of implicit conversions in the Span companion object, you can use a
scala.concurrent.duration.Duration where a Span is needed, and vice versa.
- Source
- Span.scala
- Alphabetic
- By Inheritance
- Span
- Serializable
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Value Members
- final def !=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def ##: Int
- Definition Classes
- AnyRef → Any
- final def ==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def asInstanceOf[T0]: T0
- Definition Classes
- Any
- def clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.CloneNotSupportedException]) @native()
- final def eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def equals(other: Any): Boolean
Compares another object for equality.
Compares another object for equality.
If the passed object is a
Span, this method will returntrueonly if the otherSpanreturns the exact same value as thisSpanfortotalNanos.- other
the object to compare with this one for equality
- returns
true if the other object is a
Spanwith the sametotalNanosvalue.
- Definition Classes
- Span → AnyRef → Any
- def finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.Throwable])
- final def getClass(): Class[_ <: AnyRef]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- def hashCode(): Int
Returns a hash code for this
Span.Returns a hash code for this
Span.- returns
a hash code based only on the
totalNanosfield.
- Definition Classes
- Span → AnyRef → Any
- final def isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- lazy val millisPart: Long
This time span converted to milliseconds, computed via
totalNanos / 1000000, which truncates off any leftover nanoseconds.This time span converted to milliseconds, computed via
totalNanos / 1000000, which truncates off any leftover nanoseconds.The
millisPartandnanosPartcan be used, for example, when invokingThread.sleep. For example, given aSpannamedspan, you could write:Thread.sleep(span.millisPart, span.nanosPart)
- lazy val nanosPart: Int
The number of nanoseconds remaining when this time span is converted to milliseconds, computed via
(totalNanos % 1000000).toInt.The number of nanoseconds remaining when this time span is converted to milliseconds, computed via
(totalNanos % 1000000).toInt.The
millisPartandnanosPartcan be used, for example, when invokingThread.sleep. For example, given aSpannamedspan, you could write:Thread.sleep(span.millisPart, span.nanosPart)
- final def ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- final def notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
- final def notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
- lazy val prettyString: String
Returns a localized string suitable for presenting to a user that describes the time span represented by this
Span.Returns a localized string suitable for presenting to a user that describes the time span represented by this
Span.For example, for
Span(1, Millisecond), this method would return"1 millisecond". ForSpan(9.99, Seconds), this method would return"9.9 seconds".- returns
a localized string describing this
Span
- def scaledBy(factor: Double): Span
Returns a
Spanrepresenting thisSpanscaled by the passed factor.Returns a
Spanrepresenting thisSpanscaled by the passed factor.The passed
factorcan be any positive number or zero, including fractional numbers. A number greater than one will scale theSpanup to a larger value. A fractional number will scale it down to a smaller value. A factor of 1.0 will cause the exact sameSpanto be returned. A factor of zero will causeSpan.Zeroto be returned.If overflow occurs,
Span.Maxwill be returned. If underflow occurs,Span.Zerowill be returned.- Exceptions thrown
IllegalArgumentExceptionif the passed value is less than zero
- final def synchronized[T0](arg0: => T0): T0
- Definition Classes
- AnyRef
- def toString(): String
Returns a string that looks similar to a factory method call that would have produced this
Span.Returns a string that looks similar to a factory method call that would have produced this
Span.For example, for
Span(1, Millisecond), this method would return"Span(1, Millisecond)". ForSpan(9.99, Seconds), this method would return"Span(9.99, Seconds)".- returns
a string that looks like a factory method call that would produce this
Span
- Definition Classes
- Span → AnyRef → Any
- val totalNanos: Long
The total number of nanoseconds in this time span.
The total number of nanoseconds in this time span.
This number will never be negative, but can be zero.
- final def wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException]) @native()