OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library test.frontend.timeout; |
| 6 |
| 7 /// A class representing a modification to the default timeout for a test. |
| 8 /// |
| 9 /// By default, a test will time out after 30 seconds. With [new Timeout], that |
| 10 /// can be overridden entirely; with [new Timeout.factor], it can be scaled |
| 11 /// relative to the default. |
| 12 class Timeout { |
| 13 /// A constant indicating that a test should never time out. |
| 14 static const none = const Timeout._none(); |
| 15 |
| 16 /// The timeout duration. |
| 17 /// |
| 18 /// If set, this overrides the default duration entirely. It's `null` for |
| 19 /// timeouts with a non-null [scaleFactor] and for [Timeout.none]. |
| 20 final Duration duration; |
| 21 |
| 22 /// The timeout factor. |
| 23 /// |
| 24 /// The default timeout will be multiplied by this to get the new timeout. |
| 25 /// Thus a factor of 2 means that the test will take twice as long to time |
| 26 /// out, and a factor of 0.5 means that it will time out twice as quickly. |
| 27 /// |
| 28 /// This is `null` for timeouts with a non-null [duration] and for |
| 29 /// [Timeout.none]. |
| 30 final num scaleFactor; |
| 31 |
| 32 /// Declares an absolute timeout that overrides the default. |
| 33 const Timeout(this.duration) |
| 34 : scaleFactor = null; |
| 35 |
| 36 /// Declares a relative timeout that scales the default. |
| 37 const Timeout.factor(this.scaleFactor) |
| 38 : duration = null; |
| 39 |
| 40 const Timeout._none() |
| 41 : scaleFactor = null, |
| 42 duration = null; |
| 43 |
| 44 /// Returns a new [Timeout] that merges [this] with [other]. |
| 45 /// |
| 46 /// If [other] declares a [duration], that takes precedence. Otherwise, this |
| 47 /// timeout's [duration] or [factor] are multiplied by [other]'s [factor]. |
| 48 Timeout merge(Timeout other) { |
| 49 if (this == none || other == none) return none; |
| 50 if (other.duration != null) return new Timeout(other.duration); |
| 51 if (duration != null) return new Timeout(duration * other.scaleFactor); |
| 52 return new Timeout.factor(scaleFactor * other.scaleFactor); |
| 53 } |
| 54 |
| 55 /// Returns a new [Duration] from applying [this] to [base]. |
| 56 /// |
| 57 /// If this is [none], returns `null`. |
| 58 Duration apply(Duration base) { |
| 59 if (this == none) return null; |
| 60 return duration == null ? base * scaleFactor : duration; |
| 61 } |
| 62 } |
OLD | NEW |