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(core): Clean up and expand state machine tests #3885

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ abstract class StateMachineManager<
await for (final completer in _eventController.stream) {
try {
await dispatch(completer.event, completer).completed;
} on Object {
} on Object catch (e) {
logger.verbose('Failed hard on event: ${completer.event}', e);
continue;
}
}
Expand Down
27 changes: 24 additions & 3 deletions packages/amplify_core/test/state_machine/my_state_machine.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import 'dart:async';

import 'package:amplify_core/amplify_core.dart';
import 'package:test/test.dart';

final _builders = <StateMachineToken,
StateMachineBuilder<StateMachineEvent, StateMachineState,
Expand All @@ -12,10 +13,22 @@ final _builders = <StateMachineToken,
WorkerMachine.type: WorkerMachine.new,
};

enum MyType { initial, doWork, tryWork, delegateWork, failHard, success, error }
enum MyType {
initial,
doWork,
tryWork,
delegateWork,
failPrecondition,
failHard,
success,
error
}

class MyPreconditionException implements PreconditionException {
const MyPreconditionException(this.precondition);
const MyPreconditionException(
this.precondition, {
this.shouldEmit = false,
});

@override
final String precondition;
Expand All @@ -24,7 +37,7 @@ class MyPreconditionException implements PreconditionException {
bool get shouldLog => true;

@override
bool get shouldEmit => false;
final bool shouldEmit;
}

final class MyEvent extends StateMachineEvent<MyType, MyType> {
Expand All @@ -38,6 +51,12 @@ final class MyEvent extends StateMachineEvent<MyType, MyType> {

@override
MyPreconditionException? checkPrecondition(MyState currentState) {
if (type == MyType.failPrecondition) {
return const MyPreconditionException(
'Failed precondition',
shouldEmit: true,
);
}
if (currentState.type == type) {
return const MyPreconditionException('Cannot process event of same type');
}
Expand Down Expand Up @@ -98,6 +117,8 @@ class MyStateMachine extends StateMachine<MyEvent, MyState, StateMachineEvent,
case MyType.success:
case MyType.error:
break;
case MyType.failPrecondition:
fail('Should never reach here');
case MyType.doWork:
await doWork(fail: false);
case MyType.tryWork:
Expand Down
Loading
Loading