// Copyright (c) 2012, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// @dart = 2.9

library timer_cancel_test;

import 'dart:async';

import 'package:async_helper/async_helper.dart';
import 'package:expect/expect.dart';

final ms = const Duration(milliseconds: 1);

main() {
  asyncStart();
  return testSimpleTimer()
      .then((_) => cancelTimerWithSame())
      .then((_) => asyncEnd());
}

Future testSimpleTimer() {
  var cancelTimer = new Timer(ms * 1000, unreachable);
  cancelTimer.cancel();

  var handler = new Completer();
  var repeatHandler = new Completer();
  new Timer(ms * 1000, () {
    cancelTimer.cancel();
    handler.complete();
  });

  cancelTimer = new Timer(ms * 2000, unreachable);
  var repeatTimer = 0;
  new Timer.periodic(ms * 1500, (Timer timer) {
    repeatTimer++;
    timer.cancel();
    Expect.equals(repeatTimer, 1);
    repeatHandler.complete();
  });

  return handler.future.then((_) => repeatHandler.future);
}

Future cancelTimerWithSame() {
  var completer = new Completer();
  var t2;
  var t1 = new Timer(ms * 0, () {
    t2.cancel();
    completer.complete();
  });
  t2 = new Timer(ms * 0, unreachable);
  return completer.future;
}

void unreachable() {
  Expect.fail("should not be reached");
}
