fix(app): harden socket lifecycle and expand transport regressions

This commit is contained in:
2026-10-21 13:28:59 +01:00
parent c262f77501
commit 1e4ecea823
7 changed files with 401 additions and 79 deletions
@@ -0,0 +1,144 @@
import 'dart:async';
import 'dart:convert';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/transport/pipelines_client.dart';
import 'pipelines_client_test.dart' show FakeChannel;
Future<void> flush() => Future<void>.delayed(Duration.zero);
Map<String, dynamic> sent(FakeChannel channel, [int index = 0]) =>
jsonDecode(channel.outgoing[index] as String) as Map<String, dynamic>;
void reply(FakeChannel channel, {int index = 0, String type = 'pong'}) =>
channel.incoming.add(
jsonEncode({'id': sent(channel, index)['id'], 'type': type}),
);
void main() {
test(
'requests serialize through replies, errors do not poison queue',
() async {
final channel = FakeChannel()..readyCompleter.complete();
final client = PipelinesClient(channelFactory: (_) => channel);
await client.connect(Uri.parse('ws://example.test'));
final first = client.call('ping');
final firstError = expectLater(first, throwsA(isA<ProtocolError>()));
final second = client.call('list_runs');
await flush();
expect(channel.outgoing, hasLength(1));
channel.incoming.add(
jsonEncode({
'id': sent(channel)['id'],
'type': 'error',
'error': 'refused',
}),
);
await firstError;
await flush();
expect(channel.outgoing, hasLength(2));
reply(channel, index: 1, type: 'runs');
expect((await second)['type'], 'runs');
await client.dispose();
},
);
test(
'timeout releases queue and late response is an unsolicited frame',
() async {
final channel = FakeChannel()..readyCompleter.complete();
final client = PipelinesClient(
channelFactory: (_) => channel,
requestTimeout: const Duration(milliseconds: 10),
);
await client.connect(Uri.parse('ws://example.test'));
await expectLater(client.call('ping'), throwsA(isA<TimeoutException>()));
final late = client.frames.first;
reply(channel);
expect((await late)['type'], 'pong');
await client.dispose();
},
);
test(
'replacement fails queued calls rather than sending on new socket',
() async {
final channels = [
FakeChannel()..readyCompleter.complete(),
FakeChannel()..readyCompleter.complete(),
];
var index = 0;
final client = PipelinesClient(channelFactory: (_) => channels[index++]);
await client.connect(Uri.parse('ws://old.test'));
final firstError = expectLater(client.call('ping'), throwsStateError);
final queuedError = expectLater(
client.call('list_runs'),
throwsStateError,
);
await flush();
await client.connect(Uri.parse('ws://new.test'));
await Future.wait([firstError, queuedError]);
expect(channels.last.outgoing, isEmpty);
expect(client.state.kind, ConnectionKind.connected);
await client.dispose();
},
);
test('sendOnly never blocks correlated requests', () async {
final channel = FakeChannel()..readyCompleter.complete();
final client = PipelinesClient(channelFactory: (_) => channel);
await client.connect(Uri.parse('ws://example.test'));
client.sendOnly('subscribe', {'workspace': ''});
final ping = client.call('ping');
await flush();
expect(sent(channel)['type'], 'subscribe');
reply(channel, index: 1);
expect((await ping)['type'], 'pong');
await client.dispose();
});
test('overlapping connect cannot create a superseded socket', () async {
var factories = 0;
final channel = FakeChannel()..readyCompleter.complete();
final client = PipelinesClient(
channelFactory: (_) {
factories++;
return channel;
},
);
await Future.wait([
client.connect(Uri.parse('ws://old.test')),
client.connect(Uri.parse('ws://new.test')),
]);
expect(factories, 1);
expect(client.state.kind, ConnectionKind.connected);
await client.dispose();
await expectLater(
client.connect(Uri.parse('ws://new.test')),
throwsStateError,
);
});
test('socket failure reconnects, disconnect cancels retry', () async {
final first = FakeChannel()..readyCompleter.complete();
final next = FakeChannel()..readyCompleter.complete();
var factories = 0;
final client = PipelinesClient(
channelFactory: (_) => factories++ == 0 ? first : next,
backoffBase: const Duration(milliseconds: 5),
);
await client.connect(Uri.parse('ws://example.test'));
final connectedAgain = client.states.firstWhere(
(s) => s.kind == ConnectionKind.connected,
);
first.incoming.addError(StateError('network drop'));
await connectedAgain.timeout(const Duration(seconds: 2));
expect(factories, 2);
await client.disconnect();
expect(client.state.retrying, isFalse);
expect(
() => client.sendOnly('subscribe'),
throwsA(isA<NotConnectedError>()),
);
await client.dispose();
});
}
@@ -34,9 +34,17 @@ class FakeSink implements WebSocketSink {
@override
void addError(Object error, [StackTrace? stackTrace]) => throw error;
@override
Future<void> addStream(Stream<dynamic> stream) async { await for (final item in stream) { add(item); } }
Future<void> addStream(Stream<dynamic> stream) async {
await for (final item in stream) {
add(item);
}
}
@override
Future<void> close([int? closeCode, String? closeReason]) async { await incoming.close(); }
Future<void> close([int? closeCode, String? closeReason]) async {
await incoming.close();
}
@override
Future<void> get done async {}
@override
@@ -54,7 +62,8 @@ void main() {
await connecting;
final request = client.call('ping', {'value': 1});
await Future<void>.delayed(Duration.zero);
final sent = jsonDecode(channel.outgoing.single as String) as Map<String, dynamic>;
final sent =
jsonDecode(channel.outgoing.single as String) as Map<String, dynamic>;
channel.incoming.add(jsonEncode({'id': sent['id'], 'type': 'pong'}));
expect((await request)['type'], 'pong');
channel.incoming.add(jsonEncode({'type': 'run_event'}));
+33 -5
View File
@@ -2,6 +2,20 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/transport/server_url.dart';
void main() {
test('IPv6 authorities and encoded artifact queries stay intact', () {
expect(
ServerUrl.validate('ws://[::1]:8081').wsUrl,
'ws://[::1]:8081/connect',
);
expect(
ServerUrl.resolveArtifactUrl(
'ws://[::1]:8081/connect',
'/artifacts/a%20b?download=1',
).toString(),
'http://[::1]:8081/artifacts/a%20b?download=1',
);
expect(() => ServerUrl.validate('ws://host:70000'), throwsFormatException);
});
test('validates and normalizes endpoint with path and query', () {
final value = ServerUrl.validate(' wss://example.test:8443/?token=x ');
expect(value.wsUrl, 'wss://example.test:8443/connect?token=x');
@@ -10,14 +24,28 @@ void main() {
});
test('rejects unsupported schemes and credentials', () {
expect(() => ServerUrl.validate('https://example.test'), throwsFormatException);
expect(() => ServerUrl.validate('ws://user:pass@example.test'), throwsFormatException);
expect(
() => ServerUrl.validate('https://example.test'),
throwsFormatException,
);
expect(
() => ServerUrl.validate('ws://user:pass@example.test'),
throwsFormatException,
);
});
test('resolves relative artifacts at the server origin', () {
expect(ServerUrl.resolveArtifactUrl('ws://example.test:8081/connect', '/artifacts/a')!.toString(),
'http://example.test:8081/artifacts/a');
expect(
ServerUrl.resolveArtifactUrl(
'ws://example.test:8081/connect',
'/artifacts/a',
)!.toString(),
'http://example.test:8081/artifacts/a',
);
expect(ServerUrl.resolveArtifactUrl('', '/artifacts/a'), isNull);
expect(ServerUrl.resolveArtifactUrl('', 'https://cdn.test/a')!.toString(), 'https://cdn.test/a');
expect(
ServerUrl.resolveArtifactUrl('', 'https://cdn.test/a')!.toString(),
'https://cdn.test/a',
);
});
}