feat(app): reconnect on foreground and keep the protocol warning
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/application/pipelines_app.dart';
|
||||
import 'package:pipelines_app/core/transport/pipelines_client.dart';
|
||||
|
||||
import '../core/transport/pipelines_client_test.dart' show FakeChannel;
|
||||
import '../features/pipelines/fixtures.dart';
|
||||
import 'pipelines_app_test.dart' show webServer;
|
||||
import 'pipelines_replies.dart';
|
||||
|
||||
Future<void> pumpApp(
|
||||
WidgetTester tester, {
|
||||
required PlatformRouteInformationProvider provider,
|
||||
required PipelinesClient client,
|
||||
}) async {
|
||||
await tester.pumpWidget(
|
||||
PipelinesApp(
|
||||
server: webServer(),
|
||||
client: client,
|
||||
cupertino: false,
|
||||
routeInformationProvider: provider,
|
||||
),
|
||||
);
|
||||
await tester.pump();
|
||||
}
|
||||
|
||||
PlatformRouteInformationProvider routeProvider() {
|
||||
final provider = PlatformRouteInformationProvider(
|
||||
initialRouteInformation: RouteInformation(uri: Uri.parse('/pipelines')),
|
||||
);
|
||||
addTearDown(provider.dispose);
|
||||
return provider;
|
||||
}
|
||||
|
||||
/// The app receives a real return-to-foreground as the engine's lifecycle
|
||||
/// transition does: backgrounded, then resumed.
|
||||
Future<void> resume(WidgetTester tester) async {
|
||||
tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.inactive);
|
||||
tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed);
|
||||
await tester.pump();
|
||||
await tester.pump();
|
||||
}
|
||||
|
||||
List<String> types(FakeChannel channel) => <String>[
|
||||
for (final frame in channel.outgoing)
|
||||
(jsonDecode(frame as String) as Map<String, dynamic>)['type'] as String,
|
||||
];
|
||||
|
||||
void main() {
|
||||
tearDown(() {
|
||||
WidgetsBinding.instance.handleAppLifecycleStateChanged(
|
||||
AppLifecycleState.resumed,
|
||||
);
|
||||
});
|
||||
|
||||
test('only a dropped socket is reopened on foreground', () {
|
||||
expect(
|
||||
shouldReconnectOnForeground(ConnectionKind.disconnected),
|
||||
isTrue,
|
||||
);
|
||||
// A deliberate Disconnect reaches idle and must stay disconnected.
|
||||
expect(shouldReconnectOnForeground(ConnectionKind.idle), isFalse);
|
||||
// These states already have their own answer; foregrounding does not churn
|
||||
// a live socket or restart an attempt that is already in flight.
|
||||
expect(shouldReconnectOnForeground(ConnectionKind.connected), isFalse);
|
||||
expect(shouldReconnectOnForeground(ConnectionKind.connecting), isFalse);
|
||||
});
|
||||
|
||||
testWidgets(
|
||||
'foreground resume reopens a dropped socket and re-watches it once',
|
||||
(tester) async {
|
||||
final first = FakeChannel()..readyCompleter.complete();
|
||||
final second = FakeChannel()..readyCompleter.complete();
|
||||
var sockets = 0;
|
||||
final client = PipelinesClient(
|
||||
channelFactory: (_) => sockets++ == 0 ? first : second,
|
||||
// Keep the client's own backoff retry far away, so the new socket can
|
||||
// only come from the foreground transition under test.
|
||||
backoffBase: const Duration(minutes: 1),
|
||||
);
|
||||
await pumpApp(tester, provider: routeProvider(), client: client);
|
||||
await replyPipelines(tester, first, PipelineFixture());
|
||||
expect(sockets, 1);
|
||||
expect(types(first), contains('subscribe'));
|
||||
expect(client.state.kind, ConnectionKind.connected);
|
||||
|
||||
first.incoming.addError(StateError('network drop'));
|
||||
await tester.pump();
|
||||
await tester.pump();
|
||||
expect(client.state.kind, ConnectionKind.disconnected);
|
||||
expect(sockets, 1, reason: 'the backoff retry is still pending');
|
||||
|
||||
await resume(tester);
|
||||
|
||||
expect(sockets, 2);
|
||||
expect(types(second), contains('subscribe'));
|
||||
expect(client.state.kind, ConnectionKind.connected);
|
||||
|
||||
await tester.pumpWidget(const SizedBox.shrink());
|
||||
await tester.pump();
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets('foreground resume does not churn a still-connected socket', (
|
||||
tester,
|
||||
) async {
|
||||
final channel = FakeChannel()..readyCompleter.complete();
|
||||
final client = PipelinesClient(channelFactory: (_) => channel);
|
||||
await pumpApp(tester, provider: routeProvider(), client: client);
|
||||
await replyPipelines(tester, channel, PipelineFixture());
|
||||
expect(types(channel).where((type) => type == 'subscribe'), hasLength(1));
|
||||
|
||||
await resume(tester);
|
||||
|
||||
expect(types(channel).where((type) => type == 'subscribe'), hasLength(1));
|
||||
expect(client.state.kind, ConnectionKind.connected);
|
||||
|
||||
await tester.pumpWidget(const SizedBox.shrink());
|
||||
await tester.pump();
|
||||
});
|
||||
}
|
||||
@@ -76,11 +76,20 @@ void main() {
|
||||
'type': 'pong',
|
||||
'build': {'version': 'settings-fixture'},
|
||||
'time': 'fixture time',
|
||||
'protocol': 2,
|
||||
}),
|
||||
);
|
||||
await tester.pump();
|
||||
await tester.pump();
|
||||
expect(find.textContaining('settings-fixture'), findsOneWidget);
|
||||
expect(
|
||||
find.text(
|
||||
'Protocol mismatch: this page speaks protocol 1, '
|
||||
'the server reports 2.',
|
||||
),
|
||||
findsOneWidget,
|
||||
reason: 'a mismatched server must stay visibly flagged in Settings',
|
||||
);
|
||||
expect(tester.takeException(), isNull);
|
||||
await tester.pumpWidget(const SizedBox.shrink());
|
||||
await tester.pump();
|
||||
|
||||
Reference in New Issue
Block a user