isolates_test.dart 1.59 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6 7
import 'dart:io';
import 'package:file/file.dart';
import 'package:file/local.dart';
8
import 'package:flutter/foundation.dart';
9
import 'package:flutter_test/flutter_test.dart';
10
import 'package:platform/platform.dart';
11 12 13 14 15 16 17 18 19

int test1(int value) {
  return value + 1;
}

int test2(int value) {
  throw 2;
}

20 21 22 23 24 25 26 27
Future<int> test1Async(int value) async {
  return value + 1;
}

Future<int> test2Async(int value) async {
  throw 2;
}

28 29 30
void main() {
  test('compute()', () async {
    expect(await compute(test1, 0), 1);
Dan Field's avatar
Dan Field committed
31
    expect(compute(test2, 0), throwsException);
32 33

    expect(await compute(test1Async, 0), 1);
Dan Field's avatar
Dan Field committed
34
    expect(compute(test2Async, 0), throwsException);
35
  }, skip: kIsWeb);
36 37 38 39 40 41 42 43 44 45 46 47 48 49

  test('compute closes all ports', () async {
    // Run a Dart script that calls compute().
    // The Dart process will terminate only if the script exits cleanly with
    // all isolate ports closed.
    const FileSystem fs = LocalFileSystem();
    const Platform platform = LocalPlatform();
    final String flutterRoot = platform.environment['FLUTTER_ROOT']!;
    final String dartPath = fs.path.join(flutterRoot, 'bin', 'cache', 'dart-sdk', 'bin', 'dart');
    final String packageRoot = fs.path.dirname(fs.path.fromUri(platform.script));
    final String scriptPath = fs.path.join(packageRoot, 'test', 'foundation', '_compute_caller.dart');
    final ProcessResult result = await Process.run(dartPath, <String>[scriptPath]);
    expect(result.exitCode, 0);
  }, skip: kIsWeb);
50
}