Span

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.

Companion:
object
trait Serializable
class Object
trait Matchable
class Any

Value members

Concrete methods

override 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 return true only if the other Span returns the exact same value as this Span for totalNanos.

Value parameters:
other

the object to compare with this one for equality

Returns:

true if the other object is a Span with the same totalNanos value.

Definition Classes
Any
override 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 totalNanos field.

Definition Classes
Any
def scaledBy(factor: Double): Span

Returns a Span representing this Span scaled by the passed factor.

Returns a Span representing this Span scaled by the passed factor.

The passed factor can be any positive number or zero, including fractional numbers. A number greater than one will scale the Span up to a larger value. A fractional number will scale it down to a smaller value. A factor of 1.0 will cause the exact same Span to be returned. A factor of zero will cause Span.Zero to be returned.

If overflow occurs, Span.Max will be returned. If underflow occurs, Span.Zero will be returned.

Throws:
IllegalArgumentException

if the passed value is less than zero

override 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)". For Span(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
Any

Concrete fields

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 millisPart and nanosPart can be used, for example, when invoking Thread.sleep. For example, given a Span named span, 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 millisPart and nanosPart can be used, for example, when invoking Thread.sleep. For example, given a Span named span, you could write:

Thread.sleep(span.millisPart, span.nanosPart)
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". For Span(9.99, Seconds), this method would return "9.9 seconds".

Returns:

a localized string describing this Span

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.