<commit_before>// Copyright (c) 2016, 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.

import 'dart:async';

import 'package:test/test.dart';
import 'package:angular/di.dart';
import 'package:angular_test/src/errors.dart';
import 'package:angular_test/src/frontend/stabilizer.dart';

void main() {
  group('$NgZoneStabilizer', () {
    NgZone ngZone;
    NgZoneStabilizer ngZoneStabilizer;

    setUp(() {
      ngZone = NgZone();
      ngZoneStabilizer = NgZoneStabilizer(ngZone);
    });

    test('should forward a synchronous error during update', () async {
      expect(ngZoneStabilizer.update(() {
        throw StateError('Test');
      }), throwsStateError);
    });

    test('should forward a synchronous error while stabilizng', () async {
      expect(ngZoneStabilizer.stabilize(runAndTrackSideEffects: () {
        throw StateError('Test');
      }), throwsStateError);
    });

    test('should forward an asynchronous error during update', () async {
      expect(ngZoneStabilizer.update(() {
        scheduleMicrotask(() {
          throw StateError('Test');
        });
      }), throwsStateError);
    });

    test('should forward an asynchronous error while stabilizing', () async {
      expect(ngZoneStabilizer.stabilize(runAndTrackSideEffects: () {
        scheduleMicrotask(() {
          throw StateError('Test');
        });
      }), throwsStateError);
    });

    test('should forward an asynchronous error via timer', () async {
      expect(ngZoneStabilizer.update(() {
        Timer.run(() {
          throw StateError('Test');
        });
      }), throwsStateError);
    });

    test('should forward an asynchronus error via timer stabilizing', () async {
      expect(ngZoneStabilizer.stabilize(runAndTrackSideEffects: () {
        Timer.run(() {
          throw StateError('Test');
        });
      }), throwsStateError);
    });

    test('should forward an asynchronus error via delayed future stabilizing',
        () async {
      expect(ngZoneStabilizer.stabilize(runAndTrackSideEffects: () {
        Future.delayed(Duration(milliseconds: 100), () {
          throw StateError('Test');
        });
      }), throwsStateError);
    });

    test('should forward an asynchronous error via late stabilizing', () async {
      expect(ngZoneStabilizer.stabilize(runAndTrackSideEffects: () {
        Timer.run(() {
          scheduleMicrotask(() {
            Timer.run(() {
              scheduleMicrotask(() {
                throw StateError('Test');
              });
            });
          });
        });
      }), throwsStateError);
    });

    test('should forward an asynchrnous error in the far future', () async {
      expect(ngZoneStabilizer.stabilize(runAndTrackSideEffects: () async {
        for (var i = 0; i < 20; i++) {
          await Future(() {});
          await Future.value();
        }
        throw StateError('Test');
      }), throwsStateError);
    });

    test('should stabilize existing events', () async {
      var asyncEventsCompleted = false;
      ngZone.run(() async {
        for (var i = 0; i < 20; i++) {
          await Future(() {});
          await Future.value();
        }
        asyncEventsCompleted = true;
      });
      expect(asyncEventsCompleted, isFalse);
      await ngZoneStabilizer.stabilize();
      expect(asyncEventsCompleted, isTrue);
    });

    test('should throw if stabilization never occurs', () async {
      expect(
        ngZoneStabilizer.stabilize(
          runAndTrackSideEffects: () {
            // Just enough asynchronous events to exceed the threshold; not 1:1.
            var timersRemaining = 10;

            void runTimer() {
              if (--timersRemaining > 0) {
                scheduleMicrotask(() => Timer.run(runTimer));
              }
            }

            runTimer();
          },<commit_msg>Fix unawaited_futures lint warnings by adding missing "await"s.<commit_after>// Copyright (c) 2016, 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.

import 'dart:async';

import 'package:pedantic/pedantic.dart';
import 'package:test/test.dart';
import 'package:angular/di.dart';
import 'package:angular_test/src/errors.dart';
import 'package:angular_test/src/frontend/stabilizer.dart';

void main() {
  group('$NgZoneStabilizer', () {
    NgZone ngZone;
    NgZoneStabilizer ngZoneStabilizer;

    setUp(() {
      ngZone = NgZone();
      ngZoneStabilizer = NgZoneStabilizer(ngZone);
    });

    test('should forward a synchronous error during update', () async {
      expect(ngZoneStabilizer.update(() {
        throw StateError('Test');
      }), throwsStateError);
    });

    test('should forward a synchronous error while stabilizng', () async {
      expect(ngZoneStabilizer.stabilize(runAndTrackSideEffects: () {
        throw StateError('Test');
      }), throwsStateError);
    });

    test('should forward an asynchronous error during update', () async {
      expect(ngZoneStabilizer.update(() {
        scheduleMicrotask(() {
          throw StateError('Test');
        });
      }), throwsStateError);
    });

    test('should forward an asynchronous error while stabilizing', () async {
      expect(ngZoneStabilizer.stabilize(runAndTrackSideEffects: () {
        scheduleMicrotask(() {
          throw StateError('Test');
        });
      }), throwsStateError);
    });

    test('should forward an asynchronous error via timer', () async {
      expect(ngZoneStabilizer.update(() {
        Timer.run(() {
          throw StateError('Test');
        });
      }), throwsStateError);
    });

    test('should forward an asynchronus error via timer stabilizing', () async {
      expect(ngZoneStabilizer.stabilize(runAndTrackSideEffects: () {
        Timer.run(() {
          throw StateError('Test');
        });
      }), throwsStateError);
    });

    test('should forward an asynchronus error via delayed future stabilizing',
        () async {
      expect(ngZoneStabilizer.stabilize(runAndTrackSideEffects: () {
        Future.delayed(Duration(milliseconds: 100), () {
          throw StateError('Test');
        });
      }), throwsStateError);
    });

    test('should forward an asynchronous error via late stabilizing', () async {
      expect(ngZoneStabilizer.stabilize(runAndTrackSideEffects: () {
        Timer.run(() {
          scheduleMicrotask(() {
            Timer.run(() {
              scheduleMicrotask(() {
                throw StateError('Test');
              });
            });
          });
        });
      }), throwsStateError);
    });

    test('should forward an asynchrnous error in the far future', () async {
      expect(ngZoneStabilizer.stabilize(runAndTrackSideEffects: () async {
        for (var i = 0; i < 20; i++) {
          await Future(() {});
          await Future.value();
        }
        throw StateError('Test');
      }), throwsStateError);
    });

    test('should stabilize existing events', () async {
      var asyncEventsCompleted = false;
      unawaited(ngZone.run(() async {
        for (var i = 0; i < 20; i++) {
          await Future(() {});
          await Future.value();
        }
        asyncEventsCompleted = true;
      }));
      expect(asyncEventsCompleted, isFalse);
      await ngZoneStabilizer.stabilize();
      expect(asyncEventsCompleted, isTrue);
    });

    test('should throw if stabilization never occurs', () async {
      expect(
        ngZoneStabilizer.stabilize(
          runAndTrackSideEffects: () {
            // Just enough asynchronous events to exceed the threshold; not 1:1.
            var timersRemaining = 10;

            void runTimer() {
              if (--timersRemaining > 0) {
                scheduleMicrotask(() => Timer.run(runTimer));
              }
            }

            runTimer();
          },
{'fraction_non_alphanumeric': 0.1280366881507189, 'fraction_numerical': 0.004462072384729797, 'mean_word_length': 2.4292392690182747, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5928506', 'n_tokens_mistral': 2262, 'n_tokens_neox': 2149, 'n_words': 519}