Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Add PeriodicFuture automated tests. #17

Merged
merged 1 commit into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .github/workflows/flutter-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Flutter Test

on:
push:
branches:
- '*'

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout Repository
uses: actions/checkout@v4

- name: Set up Flutter
uses: subosito/flutter-action@v2
with:
channel: "stable"
flutter-version: '3.22.2'

- name: Install dependencies
run: flutter pub get

- name: Run tests
run: flutter test
47 changes: 26 additions & 21 deletions lib/src/util/periodic_future.dart
Original file line number Diff line number Diff line change
@@ -1,34 +1,39 @@
import 'dart:async';

import 'package:archethic_dapp_framework_flutter/src/domain/models/failures.dart';

extension _DurationTimeoutFuture on Duration {
Future<void> get toTimeoutFuture => Future.delayed(
this,
() {
throw const Timeout();
},
);
}

class PeriodicFuture {
static Future<T> periodic<T>(
FutureOr<T> Function() action, {
required Duration sleepDuration,
required bool Function(T value) until,
Completer<void>? cancelCompleter,
Duration? timeout,
}) async {
final timeoutFuture = timeout?.toTimeoutFuture;

late T result;
try {
await Future.doWhile(() async {
result = await action();
if (until(result)) {
return false;
}
await Future.any(
[
Future.delayed(sleepDuration),
if (cancelCompleter != null) cancelCompleter.future,
],
);
return cancelCompleter == null || !cancelCompleter.isCompleted;
});
} catch (e) {
if (cancelCompleter != null && cancelCompleter.isCompleted) {
// If the cancelCompleter was completed, it means we should stop the loop.
return result;
await Future.doWhile(() async {
result = await action();
if (until(result)) {
return false;
}
rethrow;
}
await Future.any(
[
Future.delayed(sleepDuration),
if (timeoutFuture != null) timeoutFuture,
],
);
return true;
});
return result;
}
}
98 changes: 88 additions & 10 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,16 @@ packages:
archethic_lib_dart:
dependency: "direct main"
description:
name: archethic_lib_dart
sha256: "421f3ffe4b2f3816424fbb81ef572344a46a67590e4a237cc6251f8101582335"
url: "https://pub.dev"
source: hosted
path: "../libdart"
relative: true
source: path
version: "3.4.1"
archethic_wallet_client:
dependency: "direct main"
description:
name: archethic_wallet_client
sha256: b144c2db4988823b3c08f9e6ea73e3348abd8fb58a45cd29bd285acded0a98d6
url: "https://pub.dev"
source: hosted
path: "../archethic-wallet-client-dart"
relative: true
source: path
version: "2.0.3"
archive:
dependency: "direct main"
Expand Down Expand Up @@ -201,6 +199,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "3.1.1"
coverage:
dependency: transitive
description:
name: coverage
sha256: "3945034e86ea203af7a056d98e98e42a5518fff200d6e8e6647e1886b07e936e"
url: "https://pub.dev"
source: hosted
version: "1.8.0"
crypto:
dependency: transitive
description:
Expand Down Expand Up @@ -592,6 +598,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.0.0"
node_preamble:
dependency: transitive
description:
name: node_preamble
sha256: "6e7eac89047ab8a8d26cf16127b5ed26de65209847630400f9aefd7cd5c730db"
url: "https://pub.dev"
source: hosted
version: "2.0.2"
normalize:
dependency: transitive
description:
Expand Down Expand Up @@ -808,14 +822,30 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.4.1"
shelf_packages_handler:
dependency: transitive
description:
name: shelf_packages_handler
sha256: "89f967eca29607c933ba9571d838be31d67f53f6e4ee15147d5dc2934fee1b1e"
url: "https://pub.dev"
source: hosted
version: "3.0.2"
shelf_static:
dependency: transitive
description:
name: shelf_static
sha256: a41d3f53c4adf0f57480578c1d61d90342cd617de7fc8077b1304643c2d85c1e
url: "https://pub.dev"
source: hosted
version: "1.1.2"
shelf_web_socket:
dependency: transitive
description:
name: shelf_web_socket
sha256: "073c147238594ecd0d193f3456a5fe91c4b0abbcc68bf5cd95b36c4e194ac611"
sha256: "9ca081be41c60190ebcb4766b2486a7d50261db7bd0f5d9615f2d653637a84c1"
url: "https://pub.dev"
source: hosted
version: "2.0.0"
version: "1.0.4"
sky_engine:
dependency: transitive
description: flutter
Expand All @@ -837,6 +867,22 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.3.4"
source_map_stack_trace:
dependency: transitive
description:
name: source_map_stack_trace
sha256: "84cf769ad83aa6bb61e0aa5a18e53aea683395f196a6f39c4c881fb90ed4f7ae"
url: "https://pub.dev"
source: hosted
version: "2.1.1"
source_maps:
dependency: transitive
description:
name: source_maps
sha256: "708b3f6b97248e5781f493b765c3337db11c5d2c81c3094f10904bfa8004c703"
url: "https://pub.dev"
source: hosted
version: "0.10.12"
source_span:
dependency: transitive
description:
Expand Down Expand Up @@ -909,6 +955,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.2.1"
test:
dependency: "direct dev"
description:
name: test
sha256: "7ee446762c2c50b3bd4ea96fe13ffac69919352bd3b4b17bac3f3465edc58073"
url: "https://pub.dev"
source: hosted
version: "1.25.2"
test_api:
dependency: transitive
description:
Expand All @@ -917,6 +971,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.7.0"
test_core:
dependency: transitive
description:
name: test_core
sha256: "2bc4b4ecddd75309300d8096f781c0e3280ca1ef85beda558d33fcbedc2eead4"
url: "https://pub.dev"
source: hosted
version: "0.6.0"
timing:
dependency: transitive
description:
Expand Down Expand Up @@ -1013,6 +1075,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.1.4"
vm_service:
dependency: transitive
description:
name: vm_service
sha256: f652077d0bdf60abe4c1f6377448e8655008eef28f128bc023f7b5e8dfeb48fc
url: "https://pub.dev"
source: hosted
version: "14.2.4"
watcher:
dependency: transitive
description:
Expand Down Expand Up @@ -1045,6 +1115,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.4.5"
webkit_inspection_protocol:
dependency: transitive
description:
name: webkit_inspection_protocol
sha256: "87d3f2333bb240704cd3f1c6b5b7acd8a10e7f0bc28c28dcf14e782014f4a572"
url: "https://pub.dev"
source: hosted
version: "1.2.1"
win32:
dependency: transitive
description:
Expand Down
2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ dev_dependencies:
json_serializable: ^6.7.1
# A code generator for Riverpod. This both simplifies the syntax empowers it, such as allowing stateful hot-reload.
riverpod_generator: ^2.4.0
# Automated tests
test: ^1.25.2

flutter:
generate: true
Expand Down
113 changes: 113 additions & 0 deletions test/periodic_future_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import 'dart:async';

import 'package:archethic_dapp_framework_flutter/archethic_dapp_framework_flutter.dart'
as aedappfm;
import 'package:test/test.dart';

const kTimerAccuracy = Duration(milliseconds: 500);

void main() {
test('Should complete when `until` verified', () async {
var i = 0;
await aedappfm.PeriodicFuture.periodic<int>(
() async {
return i++;
},
sleepDuration: const Duration(milliseconds: 100),
until: (value) => value >= 4,
);

expect(i, 5);
});

test('Should rethrow when `action` fails', () async {
expect(
aedappfm.PeriodicFuture.periodic<int>(
() async {
throw Exception('broken');
},
sleepDuration: const Duration(milliseconds: 100),
until: (value) => true,
),
throwsA(isA<Exception>()),
);
});

test(
'Should wait expected duration between `action` executions',
() async {
const durationBetweenActions = Duration(milliseconds: 10);
const iterations = 30;

final stopWatch = Stopwatch()..start();
var i = 0;
await aedappfm.PeriodicFuture.periodic<int>(
() async {
return i++;
},
sleepDuration: durationBetweenActions,
until: (value) => value >= iterations,
);
stopWatch.stop();

final actualAverageSleepDuration =
stopWatch.elapsedMilliseconds / iterations;
final expectedAverageSleepDuration =
durationBetweenActions.inMilliseconds;

final sleepDurationGap =
(actualAverageSleepDuration - expectedAverageSleepDuration).abs();
expect(sleepDurationGap, lessThan(kTimerAccuracy.inMilliseconds));
},
);

test(
'Should stop when timeout reached',
() async {
const timeout = Duration(seconds: 1);

final stopWatch = Stopwatch();

Object? exception;
var runCount = 0;
try {
stopWatch.start();
await aedappfm.PeriodicFuture.periodic(
() {
return runCount++;
},
sleepDuration: const Duration(milliseconds: 100),
until: (value) => false,
timeout: timeout,
);
} catch (e) {
exception = e;
}
stopWatch.stop();

expect(
exception,
isA<aedappfm.Timeout>(),
reason: 'Should throw a Timeout exception when timeout reached',
);

final elapsedTime = stopWatch.elapsedMilliseconds;
final expectedElapsedTime = timeout.inMilliseconds;
final elapsedTimeGap = (elapsedTime - expectedElapsedTime).abs();
expect(
elapsedTimeGap,
lessThan(kTimerAccuracy.inMilliseconds),
reason: 'Timeout duration not respected.',
);

final runCountSnapshot = runCount;
await Future.delayed(const Duration(milliseconds: 100));
final runsAfterTimeoutCount = runCount - runCountSnapshot;
expect(
runsAfterTimeoutCount,
lessThanOrEqualTo(1),
reason: '`action` should not be run after timeout.',
);
},
);
}