retry_test.dart 2.63 KB
Newer Older
1 2 3 4 5
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:test/test.dart';
6
import 'package:quiver/time.dart';
7
import 'package:quiver/testing/async.dart';
8
import 'package:quiver/testing/time.dart';
9 10 11

import 'package:flutter_driver/src/retry.dart';

12
void main() {
13
  group('retry', () {
14 15 16 17 18 19 20 21 22 23 24 25 26
    FakeAsync fakeAsync;

    setUp(() {
      fakeAsync = new FakeAsync();
      Clock fakeClock = fakeAsync.getClock(new DateTime.now());
      stopwatchFactory = () {
        return new FakeStopwatch(
          () => fakeClock.now().millisecondsSinceEpoch,
          1000
        );
      };
    });

27
    test('retries until succeeds', () {
28
      fakeAsync.run((_) {
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
        int retryCount = 0;

        expect(
          retry(
            () async {
              retryCount++;
              if (retryCount < 2) {
                throw 'error';
              } else {
                return retryCount;
              }
            },
            new Duration(milliseconds: 30),
            new Duration(milliseconds: 10)
          ),
          completion(2)
        );

        fakeAsync.elapse(new Duration(milliseconds: 50));

        // Check that we didn't retry more times than necessary
        expect(retryCount, 2);
      });
    });

54 55 56 57 58 59 60 61 62 63 64
    test('obeys predicates', () {
      fakeAsync.run((_) {
        int retryCount = 0;

        expect(
          // The predicate requires that the returned value is 2, so we expect
          // that `retry` keeps trying until the counter reaches 2.
          retry(
            () async => retryCount++,
            new Duration(milliseconds: 30),
            new Duration(milliseconds: 10),
Ian Hickson's avatar
Ian Hickson committed
65
            predicate: (int value) => value == 2
66 67 68 69 70 71 72 73
          ),
          completion(2)
        );

        fakeAsync.elapse(new Duration(milliseconds: 50));
      });
    });

74
    test('times out returning last error', () async {
75 76 77 78 79
      fakeAsync.run((_) {
        bool timedOut = false;
        int retryCount = 0;
        dynamic lastError;
        dynamic lastStackTrace;
80

81 82 83 84 85 86 87
        retry(
          () {
            retryCount++;
            throw 'error';
          },
          new Duration(milliseconds: 7),
          new Duration(milliseconds: 2)
Ian Hickson's avatar
Ian Hickson committed
88
        ).catchError((dynamic error, dynamic stackTrace) {
89 90 91 92 93 94
          timedOut = true;
          lastError = error;
          lastStackTrace = stackTrace;
        });

        fakeAsync.elapse(new Duration(milliseconds: 10));
95

96 97 98 99 100 101
        expect(timedOut, isTrue);
        expect(lastError, 'error');
        expect(lastStackTrace, isNotNull);
        expect(retryCount, 4);
      });
    });
102 103
  });
}