Add ordered websocket address fallback
This commit is contained in:
@@ -0,0 +1,262 @@
|
||||
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;
|
||||
|
||||
final primary = Uri.parse('ws://tailscale.test/connect');
|
||||
final backup = Uri.parse('ws://lan.test/connect');
|
||||
const retry = Duration(milliseconds: 5);
|
||||
|
||||
Future<void> connected(PipelinesClient client) => client.states
|
||||
.firstWhere((state) => state.kind == ConnectionKind.connected)
|
||||
.timeout(const Duration(seconds: 2));
|
||||
Future<void> flush() => Future<void>.delayed(Duration.zero);
|
||||
|
||||
void main() {
|
||||
test('failed primary uses only configured backup and stays there', () async {
|
||||
final tried = <Uri>[];
|
||||
final channel = FakeChannel()..readyCompleter.complete();
|
||||
final client = PipelinesClient(
|
||||
channelFactory: (uri) {
|
||||
tried.add(uri);
|
||||
if (uri == primary) throw StateError('unreachable');
|
||||
return channel;
|
||||
},
|
||||
backoffBase: retry,
|
||||
);
|
||||
final ready = connected(client);
|
||||
await client.connectAny([primary, backup]);
|
||||
expect(client.endpoint, primary);
|
||||
expect(client.state.retrying, isTrue);
|
||||
await ready;
|
||||
expect(tried, [primary, backup]);
|
||||
expect(client.endpoint, backup);
|
||||
await Future<void>.delayed(const Duration(milliseconds: 40));
|
||||
expect(tried, [primary, backup]);
|
||||
await client.dispose();
|
||||
});
|
||||
|
||||
test(
|
||||
'stalled readiness times out; late success cannot replace backup',
|
||||
() async {
|
||||
final first = FakeChannel();
|
||||
final second = FakeChannel()..readyCompleter.complete();
|
||||
final tried = <Uri>[];
|
||||
final client = PipelinesClient(
|
||||
channelFactory: (uri) {
|
||||
tried.add(uri);
|
||||
return uri == primary ? first : second;
|
||||
},
|
||||
connectionTimeout: const Duration(milliseconds: 10),
|
||||
backoffBase: retry,
|
||||
);
|
||||
final ready = connected(client);
|
||||
await client.connectAny([primary, backup]);
|
||||
expect(client.state.reason, contains('TimeoutException'));
|
||||
await ready;
|
||||
first.readyCompleter.complete();
|
||||
await flush();
|
||||
expect(client.endpoint, backup);
|
||||
expect(client.state.kind, ConnectionKind.connected);
|
||||
expect(tried, [primary, backup]);
|
||||
await client.dispose();
|
||||
},
|
||||
);
|
||||
|
||||
test('failed readiness advances to backup', () async {
|
||||
final first = FakeChannel();
|
||||
final second = FakeChannel()..readyCompleter.complete();
|
||||
final client = PipelinesClient(
|
||||
channelFactory: (uri) => uri == primary ? first : second,
|
||||
backoffBase: retry,
|
||||
);
|
||||
final ready = connected(client);
|
||||
final connecting = client.connectAny([primary, backup]);
|
||||
await flush();
|
||||
first.readyCompleter.completeError(StateError('handshake refused'));
|
||||
await connecting;
|
||||
await ready;
|
||||
expect(client.endpoint, backup);
|
||||
await client.dispose();
|
||||
});
|
||||
|
||||
test('closed primary advances; explicit connect restarts primary', () async {
|
||||
final channels = List.generate(
|
||||
3,
|
||||
(_) => FakeChannel()..readyCompleter.complete(),
|
||||
);
|
||||
final tried = <Uri>[];
|
||||
final client = PipelinesClient(
|
||||
channelFactory: (uri) {
|
||||
tried.add(uri);
|
||||
return channels[tried.length - 1];
|
||||
},
|
||||
backoffBase: retry,
|
||||
);
|
||||
await client.connectAny([primary, backup]);
|
||||
final ready = connected(client);
|
||||
await channels.first.incoming.close();
|
||||
await ready;
|
||||
expect(client.endpoint, backup);
|
||||
await client.connectAny(client.endpoints);
|
||||
expect(tried, [primary, backup, primary]);
|
||||
expect(client.endpoint, primary);
|
||||
await client.dispose();
|
||||
});
|
||||
|
||||
testWidgets(
|
||||
'all down wraps with exponential capped backoff; manual stop cancels',
|
||||
(tester) async {
|
||||
final tried = <Uri>[];
|
||||
final client = PipelinesClient(
|
||||
channelFactory: (uri) {
|
||||
tried.add(uri);
|
||||
throw StateError('offline');
|
||||
},
|
||||
backoffBase: const Duration(milliseconds: 20),
|
||||
backoffMax: const Duration(milliseconds: 50),
|
||||
);
|
||||
await client.connectAny([primary, backup]);
|
||||
await tester.pump(const Duration(milliseconds: 19));
|
||||
expect(tried, [primary]);
|
||||
await tester.pump(const Duration(milliseconds: 1));
|
||||
expect(tried, [primary, backup]);
|
||||
await tester.pump(const Duration(milliseconds: 39));
|
||||
expect(tried, hasLength(2));
|
||||
await tester.pump(const Duration(milliseconds: 1));
|
||||
expect(tried, [primary, backup, primary]);
|
||||
await tester.pump(const Duration(milliseconds: 49));
|
||||
expect(tried, hasLength(3));
|
||||
await tester.pump(const Duration(milliseconds: 1));
|
||||
expect(tried, [primary, backup, primary, backup]);
|
||||
expect(client.state.attempt, 4);
|
||||
await client.disconnect();
|
||||
await tester.pump(const Duration(seconds: 10));
|
||||
expect(tried, hasLength(4));
|
||||
expect(client.state.kind, ConnectionKind.idle);
|
||||
await tester.runAsync(client.dispose);
|
||||
},
|
||||
);
|
||||
|
||||
for (final dispose in [false, true]) {
|
||||
test(
|
||||
'${dispose ? 'dispose' : 'disconnect'} cancels stalled open',
|
||||
() async {
|
||||
final channel = FakeChannel();
|
||||
var factories = 0;
|
||||
final client = PipelinesClient(
|
||||
channelFactory: (_) {
|
||||
factories++;
|
||||
return channel;
|
||||
},
|
||||
connectionTimeout: const Duration(milliseconds: 10),
|
||||
backoffBase: retry,
|
||||
);
|
||||
final connecting = client.connectAny([primary, backup]);
|
||||
await flush();
|
||||
if (dispose) {
|
||||
await client.dispose();
|
||||
} else {
|
||||
await client.disconnect();
|
||||
}
|
||||
await connecting;
|
||||
channel.readyCompleter.completeError(StateError('late failure'));
|
||||
await Future<void>.delayed(const Duration(milliseconds: 40));
|
||||
expect(factories, 1);
|
||||
expect(client.state.kind, ConnectionKind.idle);
|
||||
await client.dispose();
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
test(
|
||||
'replacement candidates cancel stale callbacks and retry target',
|
||||
() async {
|
||||
final stale = FakeChannel();
|
||||
final current = FakeChannel()..readyCompleter.complete();
|
||||
final next = FakeChannel()..readyCompleter.complete();
|
||||
final replacement = Uri.parse('wss://replacement.test/connect');
|
||||
final replacementBackup = Uri.parse('wss://replacement-lan.test/connect');
|
||||
final channels = [stale, current, next];
|
||||
final tried = <Uri>[];
|
||||
final client = PipelinesClient(
|
||||
channelFactory: (uri) {
|
||||
tried.add(uri);
|
||||
return channels[tried.length - 1];
|
||||
},
|
||||
backoffBase: retry,
|
||||
);
|
||||
final oldConnect = client.connectAny([primary, backup]);
|
||||
await flush();
|
||||
stale.incoming.add(jsonEncode({'type': 'stale'}));
|
||||
final frames = <Map<String, dynamic>>[];
|
||||
client.frames.listen(frames.add);
|
||||
await client.connectAny([replacement, replacementBackup]);
|
||||
await oldConnect;
|
||||
stale.readyCompleter.completeError(StateError('late ready error'));
|
||||
await Future<void>.delayed(const Duration(milliseconds: 30));
|
||||
expect(frames, isEmpty);
|
||||
expect(client.endpoint, replacement);
|
||||
expect(client.state.kind, ConnectionKind.connected);
|
||||
final ready = connected(client);
|
||||
current.incoming.addError(StateError('lost'));
|
||||
await ready;
|
||||
expect(tried, [primary, replacement, replacementBackup]);
|
||||
expect(client.endpoints, [replacement, replacementBackup]);
|
||||
await client.dispose();
|
||||
},
|
||||
);
|
||||
|
||||
test('failure never replays outstanding or queued mutations', () async {
|
||||
final first = FakeChannel()..readyCompleter.complete();
|
||||
final second = FakeChannel()..readyCompleter.complete();
|
||||
final client = PipelinesClient(
|
||||
channelFactory: (uri) => uri == primary ? first : second,
|
||||
backoffBase: retry,
|
||||
);
|
||||
await client.connectAny([primary, backup]);
|
||||
final outstanding = expectLater(client.call('start_run'), throwsStateError);
|
||||
final queued = expectLater(client.call('delete_run'), throwsStateError);
|
||||
await flush();
|
||||
expect(first.outgoing, hasLength(1));
|
||||
final ready = connected(client);
|
||||
first.incoming.addError(StateError('network drop'));
|
||||
await Future.wait([outstanding, queued, ready]);
|
||||
expect(second.outgoing, isEmpty);
|
||||
expect(first.outgoing, hasLength(1));
|
||||
await client.dispose();
|
||||
});
|
||||
|
||||
test(
|
||||
'validation preserves live socket; normalized candidates are immutable',
|
||||
() async {
|
||||
final channel = FakeChannel()..readyCompleter.complete();
|
||||
var factories = 0;
|
||||
final client = PipelinesClient(
|
||||
channelFactory: (_) {
|
||||
factories++;
|
||||
return channel;
|
||||
},
|
||||
);
|
||||
final supplied = [Uri.parse('ws://TAILSCALE.test/'), primary, backup];
|
||||
await client.connectAny(supplied);
|
||||
supplied.clear();
|
||||
expect(client.endpoints, [primary, backup]);
|
||||
expect(() => client.endpoints.add(primary), throwsUnsupportedError);
|
||||
await expectLater(client.connectAny([]), throwsArgumentError);
|
||||
await expectLater(
|
||||
client.connectAny([backup, Uri.parse('https://invalid.test')]),
|
||||
throwsFormatException,
|
||||
);
|
||||
expect(client.endpoints, [primary, backup]);
|
||||
expect(client.endpoint, primary);
|
||||
expect(client.state.kind, ConnectionKind.connected);
|
||||
expect(factories, 1);
|
||||
client.sendOnly('ping');
|
||||
expect(channel.outgoing, hasLength(1));
|
||||
await client.dispose();
|
||||
},
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user