LCOV - code coverage report
Current view: top level - test-0.12.24+8/lib/src/frontend - timeout.dart (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 11 42 26.2 %
Date: 2017-10-10 20:17:03 Functions: 0 0 -

          Line data    Source code
       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             : import 'package:string_scanner/string_scanner.dart';
       6             : 
       7             : /// A regular expression that matches text until a letter or whitespace.
       8             : ///
       9             : /// This is intended to scan through a number without actually encoding the full
      10             : /// Dart number grammar. It doesn't stop on "e" because that can be a component
      11             : /// of numbers.
      12             : final _untilUnit = new RegExp(r"[^a-df-z\s]+", caseSensitive: false);
      13             : 
      14             : /// A regular expression that matches a time unit.
      15             : final _unit = new RegExp(r"([um]s|[dhms])", caseSensitive: false);
      16             : 
      17             : /// A regular expression that matches a section of whitespace.
      18             : final _whitespace = new RegExp(r"\s+");
      19             : 
      20             : /// A class representing a modification to the default timeout for a test.
      21             : ///
      22             : /// By default, a test will time out after 30 seconds. With [new Timeout], that
      23             : /// can be overridden entirely; with [new Timeout.factor], it can be scaled
      24             : /// relative to the default.
      25             : class Timeout {
      26             :   /// A constant indicating that a test should never time out.
      27             :   static const none = const Timeout._none();
      28             : 
      29             :   /// The timeout duration.
      30             :   ///
      31             :   /// If set, this overrides the default duration entirely. It's `null` for
      32             :   /// timeouts with a non-null [scaleFactor] and for [Timeout.none].
      33             :   final Duration duration;
      34             : 
      35             :   /// The timeout factor.
      36             :   ///
      37             :   /// The default timeout will be multiplied by this to get the new timeout.
      38             :   /// Thus a factor of 2 means that the test will take twice as long to time
      39             :   /// out, and a factor of 0.5 means that it will time out twice as quickly.
      40             :   ///
      41             :   /// This is `null` for timeouts with a non-null [duration] and for
      42             :   /// [Timeout.none].
      43             :   final num scaleFactor;
      44             : 
      45             :   /// Declares an absolute timeout that overrides the default.
      46           0 :   const Timeout(this.duration) : scaleFactor = null;
      47             : 
      48             :   /// Declares a relative timeout that scales the default.
      49           5 :   const Timeout.factor(this.scaleFactor) : duration = null;
      50             : 
      51             :   const Timeout._none()
      52             :       : scaleFactor = null,
      53           5 :         duration = null;
      54             : 
      55             :   /// Parse the timeout from a user-provided string.
      56             :   ///
      57             :   /// This supports the following formats:
      58             :   ///
      59             :   /// * `Number "x"`, which produces a relative timeout with the given scale
      60             :   ///   factor.
      61             :   ///
      62             :   /// * `(Number ("d" | "h" | "m" | "s" | "ms" | "us") (" ")?)+`, which produces
      63             :   ///   an absolute timeout with the duration given by the sum of the given
      64             :   ///   units.
      65             :   ///
      66             :   /// * `"none"`, which produces [Timeout.none].
      67             :   ///
      68             :   /// Throws a [FormatException] if [timeout] is not in a valid format
      69             :   factory Timeout.parse(String timeout) {
      70           0 :     var scanner = new StringScanner(timeout);
      71             : 
      72             :     // First check for the string "none".
      73           0 :     if (scanner.scan("none")) {
      74           0 :       scanner.expectDone();
      75             :       return Timeout.none;
      76             :     }
      77             : 
      78             :     // Scan a number. This will be either a time unit or a scale factor.
      79           0 :     scanner.expect(_untilUnit, name: "number");
      80           0 :     var number = double.parse(scanner.lastMatch[0]);
      81             : 
      82             :     // A number followed by "x" is a scale factor.
      83           0 :     if (scanner.scan("x") || scanner.scan("X")) {
      84           0 :       scanner.expectDone();
      85           0 :       return new Timeout.factor(number);
      86             :     }
      87             : 
      88             :     // Parse time units until none are left. The condition is in the middle of
      89             :     // the loop because we've already parsed the first number.
      90             :     var microseconds = 0.0;
      91             :     while (true) {
      92           0 :       scanner.expect(_unit, name: "unit");
      93           0 :       microseconds += _microsecondsFor(number, scanner.lastMatch[0]);
      94             : 
      95           0 :       scanner.scan(_whitespace);
      96             : 
      97             :       // Scan the next number, if it's avaialble.
      98           0 :       if (!scanner.scan(_untilUnit)) break;
      99           0 :       number = double.parse(scanner.lastMatch[0]);
     100             :     }
     101             : 
     102           0 :     scanner.expectDone();
     103           0 :     return new Timeout(new Duration(microseconds: microseconds.round()));
     104             :   }
     105             : 
     106             :   /// Returns the number of microseconds in [number] [unit]s.
     107             :   static double _microsecondsFor(double number, String unit) {
     108             :     switch (unit) {
     109           0 :       case "d":
     110           0 :         return number * 24 * 60 * 60 * 1000000;
     111           0 :       case "h":
     112           0 :         return number * 60 * 60 * 1000000;
     113           0 :       case "m":
     114           0 :         return number * 60 * 1000000;
     115           0 :       case "s":
     116           0 :         return number * 1000000;
     117           0 :       case "ms":
     118           0 :         return number * 1000;
     119           0 :       case "us":
     120             :         return number;
     121             :       default:
     122           0 :         throw new ArgumentError("Unknown unit $unit.");
     123             :     }
     124             :   }
     125             : 
     126             :   /// Returns a new [Timeout] that merges [this] with [other].
     127             :   ///
     128             :   /// [Timeout.none] takes precedence over everything. If timeout is
     129             :   /// [Timeout.none] and [other] declares a [duration], that takes precedence.
     130             :   /// Otherwise, this timeout's [duration] or [factor] are multiplied by
     131             :   /// [other]'s [factor].
     132             :   Timeout merge(Timeout other) {
     133          10 :     if (this == none || other == none) return none;
     134           5 :     if (other.duration != null) return new Timeout(other.duration);
     135           5 :     if (duration != null) return new Timeout(duration * other.scaleFactor);
     136          20 :     return new Timeout.factor(scaleFactor * other.scaleFactor);
     137             :   }
     138             : 
     139             :   /// Returns a new [Duration] from applying [this] to [base].
     140             :   ///
     141             :   /// If this is [none], returns `null`.
     142             :   Duration apply(Duration base) {
     143           5 :     if (this == none) return null;
     144          15 :     return duration == null ? base * scaleFactor : duration;
     145             :   }
     146             : 
     147           0 :   int get hashCode => duration.hashCode ^ 5 * scaleFactor.hashCode;
     148             : 
     149             :   bool operator ==(other) =>
     150           5 :       other is Timeout &&
     151          15 :       other.duration == duration &&
     152          15 :       other.scaleFactor == scaleFactor;
     153             : 
     154             :   String toString() {
     155           0 :     if (duration != null) return duration.toString();
     156           0 :     if (scaleFactor != null) return "${scaleFactor}x";
     157             :     return "none";
     158             :   }
     159             : }

Generated by: LCOV version 1.13