Skip to content

Commit

Permalink
[jnigen] Fix flaky tests (#814)
Browse files Browse the repository at this point in the history
* Close #547
  • Loading branch information
HosseinYousefi authored Nov 21, 2023
1 parent 8d4a241 commit 3153a35
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 53 deletions.
6 changes: 6 additions & 0 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
'package:ffigen':
- 'pkgs/ffigen/**'

'package:jnigen':
- 'pkgs/jnigen/**'

'package:jni':
- 'pkgs/jni/**'

'package:native_assets_builder':
- 'pkgs/native_assets_builder/**'

Expand Down
88 changes: 56 additions & 32 deletions pkgs/jni/example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ packages:
dependency: transitive
description:
name: collection
sha256: f092b211a4319e98e5ff58223576de6c2803db36221657b46c82574721240687
sha256: ee67cb0715911d28db6bf4af1026078bd6f0128b07a5f66fb2ed94ec6783c09a
url: "https://pub.dev"
source: hosted
version: "1.17.2"
version: "1.18.0"
convert:
dependency: transitive
description:
Expand All @@ -77,10 +77,10 @@ packages:
dependency: transitive
description:
name: coverage
sha256: "2fb815080e44a09b85e0f2ca8a820b15053982b2e714b59267719e8a9ff17097"
sha256: ac86d3abab0f165e4b8f561280ff4e066bceaac83c424dd19f1ae2c2fcd12ca9
url: "https://pub.dev"
source: hosted
version: "1.6.3"
version: "1.7.1"
crypto:
dependency: transitive
description:
Expand Down Expand Up @@ -117,10 +117,10 @@ packages:
dependency: transitive
description:
name: file
sha256: "1b92bec4fc2a72f59a8e15af5f52cd441e4a7860b49499d69dfa817af20e925d"
sha256: "5fc22d7c25582e38ad9a8515372cd9a93834027aacf1801cf01164dac0ffa08c"
url: "https://pub.dev"
source: hosted
version: "6.1.4"
version: "7.0.0"
flutter:
dependency: "direct main"
description: flutter
Expand Down Expand Up @@ -186,6 +186,14 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
intl:
dependency: transitive
description:
name: intl
sha256: "3bc132a9dbce73a7e4a21a17d06e1878839ffbf975568bc875c60537824b0c4d"
url: "https://pub.dev"
source: hosted
version: "0.18.1"
io:
dependency: transitive
description:
Expand All @@ -200,7 +208,7 @@ packages:
path: ".."
relative: true
source: path
version: "0.6.1"
version: "0.8.0-wip"
js:
dependency: transitive
description:
Expand All @@ -209,6 +217,22 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.6.7"
leak_tracker:
dependency: transitive
description:
name: leak_tracker
sha256: "7e108028e3d258667d079986da8c0bc32da4cb57431c2af03b1dc1038621a9dc"
url: "https://pub.dev"
source: hosted
version: "9.0.13"
leak_tracker_testing:
dependency: transitive
description:
name: leak_tracker_testing
sha256: b06739349ec2477e943055aea30172c5c7000225f79dad4702e2ec0eda79a6ff
url: "https://pub.dev"
source: hosted
version: "1.0.5"
lints:
dependency: transitive
description:
Expand Down Expand Up @@ -237,18 +261,18 @@ packages:
dependency: transitive
description:
name: material_color_utilities
sha256: "9528f2f296073ff54cb9fee677df673ace1218163c3bc7628093e7eed5203d41"
sha256: "0e0a020085b65b6083975e499759762399b4475f766c21668c4ecca34ea74e5a"
url: "https://pub.dev"
source: hosted
version: "0.5.0"
version: "0.8.0"
meta:
dependency: transitive
description:
name: meta
sha256: "3c74dbf8763d36539f114c799d8a2d87343b5067e9d796ca22b5eb8437090ee3"
sha256: d584fa6707a52763a52446f02cc621b077888fb63b93bbcb1143a7be5a0c0c04
url: "https://pub.dev"
source: hosted
version: "1.9.1"
version: "1.11.0"
mime:
dependency: transitive
description:
Expand Down Expand Up @@ -285,10 +309,10 @@ packages:
dependency: transitive
description:
name: platform
sha256: "4a451831508d7d6ca779f7ac6e212b4023dd5a7d08a27a63da33756410e32b76"
sha256: "0a279f0707af40c890e80b1e9df8bb761694c074ba7e1d4ab1bc4b728e200b59"
url: "https://pub.dev"
source: hosted
version: "3.1.0"
version: "3.1.3"
plugin_platform_interface:
dependency: transitive
description:
Expand All @@ -309,10 +333,10 @@ packages:
dependency: transitive
description:
name: process
sha256: "53fd8db9cec1d37b0574e12f07520d582019cb6c44abf5479a01505099a34a09"
sha256: "266ca5be5820feefc777793d0a583acfc8c40834893c87c00c6c09e2cf58ea42"
url: "https://pub.dev"
source: hosted
version: "4.2.4"
version: "5.0.1"
pub_semver:
dependency: transitive
description:
Expand Down Expand Up @@ -386,18 +410,18 @@ packages:
dependency: transitive
description:
name: stack_trace
sha256: c3c7d8edb15bee7f0f74debd4b9c5f3c2ea86766fe4178eb2a18eb30a0bdaed5
sha256: "73713990125a6d93122541237550ee3352a2d84baad52d375a4cad2eb9b7ce0b"
url: "https://pub.dev"
source: hosted
version: "1.11.0"
version: "1.11.1"
stream_channel:
dependency: transitive
description:
name: stream_channel
sha256: "83615bee9045c1d322bbbd1ba209b7a749c2cbcdcb3fdd1df8eb488b3279c1c8"
sha256: ba2aa5d8cc609d96bbb2899c28934f9e1af5cddbd60a827822ea467161eb54e7
url: "https://pub.dev"
source: hosted
version: "2.1.1"
version: "2.1.2"
string_scanner:
dependency: transitive
description:
Expand Down Expand Up @@ -426,26 +450,26 @@ packages:
dependency: "direct dev"
description:
name: test
sha256: "13b41f318e2a5751c3169137103b60c584297353d4b1761b66029bae6411fe46"
sha256: a1f7595805820fcc05e5c52e3a231aedd0b72972cb333e8c738a8b1239448b6f
url: "https://pub.dev"
source: hosted
version: "1.24.3"
version: "1.24.9"
test_api:
dependency: transitive
description:
name: test_api
sha256: "75760ffd7786fffdfb9597c35c5b27eaeec82be8edfb6d71d32651128ed7aab8"
sha256: "5c2f730018264d276c20e4f1503fd1308dfbbae39ec8ee63c5236311ac06954b"
url: "https://pub.dev"
source: hosted
version: "0.6.0"
version: "0.6.1"
test_core:
dependency: transitive
description:
name: test_core
sha256: "99806e9e6d95c7b059b7a0fc08f07fc53fabe54a829497f0d9676299f1e8637e"
sha256: a757b14fc47507060a162cc2530d9a4a2f92f5100a952c7443b5cad5ef5b106a
url: "https://pub.dev"
source: hosted
version: "0.5.3"
version: "0.5.9"
typed_data:
dependency: transitive
description:
Expand All @@ -466,10 +490,10 @@ packages:
dependency: transitive
description:
name: vm_service
sha256: c620a6f783fa22436da68e42db7ebbf18b8c44b9a46ab911f666ff09ffd9153f
sha256: b3d56ff4341b8f182b96aceb2fa20e3dcb336b9f867bc0eafc0de10f1048e957
url: "https://pub.dev"
source: hosted
version: "11.7.1"
version: "13.0.0"
watcher:
dependency: transitive
description:
Expand All @@ -482,10 +506,10 @@ packages:
dependency: transitive
description:
name: web
sha256: dc8ccd225a2005c1be616fe02951e2e342092edf968cf0844220383757ef8f10
sha256: afe077240a270dcfd2aafe77602b4113645af95d0ad31128cc02bce5ac5d5152
url: "https://pub.dev"
source: hosted
version: "0.1.4-beta"
version: "0.3.0"
web_socket_channel:
dependency: transitive
description:
Expand All @@ -498,10 +522,10 @@ packages:
dependency: transitive
description:
name: webdriver
sha256: "3c923e918918feeb90c4c9fdf1fe39220fa4c0e8e2c0fffaded174498ef86c49"
sha256: "003d7da9519e1e5f329422b36c4dcdf18d7d2978d1ba099ea4e45ba490ed845e"
url: "https://pub.dev"
source: hosted
version: "3.0.2"
version: "3.0.3"
webkit_inspection_protocol:
dependency: transitive
description:
Expand All @@ -519,5 +543,5 @@ packages:
source: hosted
version: "3.1.2"
sdks:
dart: ">=3.1.0 <4.0.0"
dart: ">=3.2.0-194.0.dev <4.0.0"
flutter: ">=2.11.0"
39 changes: 21 additions & 18 deletions pkgs/jni/test/jobject_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// BSD-style license that can be found in the LICENSE file.

import 'dart:io';
import 'dart:ffi';
import 'dart:isolate';

import 'package:test/test.dart';
Expand Down Expand Up @@ -244,8 +243,27 @@ void run({required TestRunnerCallback testRunner}) {
});
});

testRunner("Isolate", () {
Isolate.spawn(doSomeWorkInIsolate, null);
testRunner("Isolate", () async {
final receivePort = ReceivePort();
await Isolate.spawn((sendPort) {
// On standalone target, make sure to call [setDylibDir] before accessing
// any JNI function in a new isolate.
//
// otherwise subsequent JNI calls will throw a "library not found" exception.
Jni.setDylibDir(dylibDir: "build/jni_libs");
final random = Jni.newInstance("java/util/Random", "()V", []);
final result = random.callMethodByName<int>(
"nextInt", "(I)I", [256], JniCallType.intType);
random.release();
// A workaround for `--pause-isolates-on-exit`. Otherwise getting test
// with coverage pauses indefinitely here.
// https://github.com/dart-lang/coverage/issues/472
sendPort.send(result);
Isolate.current.kill();
}, receivePort.sendPort);
final random = await receivePort.first as int;
expect(random, greaterThanOrEqualTo(0));
expect(random, lessThan(256));
});

testRunner("Methods rethrow exceptions in Java as JniException", () {
Expand Down Expand Up @@ -276,18 +294,3 @@ void run({required TestRunnerCallback testRunner}) {
expect(maxLong, equals(maxLongInJava));
});
}

void doSomeWorkInIsolate(Void? _) {
// On standalone target, make sure to call [setDylibDir] before accessing
// any JNI function in a new isolate.
//
// otherwise subsequent JNI calls will throw a "library not found" exception.
Jni.setDylibDir(dylibDir: "build/jni_libs");
final random = Jni.newInstance("java/util/Random", "()V", []);
// final r = random.callMethodByName<int>("nextInt", "(I)I", [256]);
// expect(r, lessThan(256));
// Expect throws an [OutsideTestException]
// but you can uncomment below print and see it works
// print("\n$r");
random.release();
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ void main() async {
final config = getConfig(generateFullVersion: true);
config.outputConfig.bindingsType = BindingsType.dartOnly;
await generateAndAnalyzeBindings(config);
}, timeout: const Timeout.factor(2), tags: largeTestTag);
}, timeout: const Timeout(Duration(minutes: 2)), tags: largeTestTag);
}
4 changes: 2 additions & 2 deletions pkgs/jnigen/test/test_util/test_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,10 @@ void generateAndCompareBothModes(
) {
test('$description (cBased)', () async {
await generateAndCompareBindings(cBasedConfig);
});
}, timeout: const Timeout(Duration(minutes: 2)));
test('$description (dartOnly)', () async {
await generateAndCompareBindings(dartOnlyConfig);
});
}, timeout: const Timeout(Duration(minutes: 2)));
}

List<String> findFilesWithSuffix(Directory dir, String suffix) {
Expand Down

0 comments on commit 3153a35

Please sign in to comment.