fix(plex): stabilize Linux endpoint failover

This commit is contained in:
edde746
2026-05-06 00:51:00 +02:00
parent da7aef4548
commit fb084d3ea1
7 changed files with 166 additions and 12 deletions
+33
View File
@@ -61,6 +61,39 @@ void main() {
expect(httpClient.requests.map((r) => r.url.path), everyElement('/hubs'));
expect(httpClient.requests.map((r) => r.url.queryParameters['count']), everyElement('12'));
});
test('fetchGlobalHubs retries transient failures without switching Plex endpoints', () async {
final db = AppDatabase.forTesting(NativeDatabase.memory());
PlexApiCache.initialize(db);
addTearDown(db.close);
const primary = 'http://primary:32400';
const fallback = 'http://fallback:32400';
final httpClient = _SequenceClient([
(_) async => throw TimeoutException('queued behind cold handshakes'),
(_) async => _jsonResponse(_globalHubsPayload()),
]);
final client = PlexClient.forTesting(
config: PlexConfig(
baseUrl: primary,
token: 'token',
clientIdentifier: 'client-id',
product: 'Plezy',
version: 'test',
),
serverId: 'server-id',
serverName: 'Server',
httpClient: httpClient,
prioritizedEndpoints: const [primary, fallback],
);
addTearDown(client.close);
final hubs = await client.fetchGlobalHubs(limit: 12);
expect(hubs, hasLength(1));
expect(client.config.baseUrl, primary);
expect(httpClient.requests.map((r) => r.url.origin), everyElement(primary));
});
});
}
@@ -1,12 +1,14 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:plezy/services/plex_auth_service.dart';
Map<String, dynamic> _serverJson(Map<String, dynamic> connection) => {
Map<String, dynamic> _serverJson(Map<String, dynamic> connection) => _serverJsonWithConnections([connection]);
Map<String, dynamic> _serverJsonWithConnections(List<Map<String, dynamic>> connections) => {
'name': 'Home Server',
'clientIdentifier': 'srv-1',
'accessToken': 'token-1',
'owned': true,
'connections': [connection],
'connections': connections,
};
Map<String, dynamic> _connectionJson({
@@ -108,5 +110,41 @@ void main() {
expect(urls, contains('https://plex.example.com'));
expect(urls, isNot(contains('http://plex.example.com:32400')));
});
test('treats custom public HTTPS preferred endpoint as remote for failover filtering', () {
const preferred = 'https://plex.example.com';
const localPlexDirect = 'https://192-168-1-50.abc.plex.direct:32400';
const remotePlexDirect = 'https://203-0-113-10.abc.plex.direct:32400';
final server = PlexServer.fromJson(
_serverJsonWithConnections([
_connectionJson(protocol: 'https', address: '192.168.1.50', port: 32400, uri: localPlexDirect, local: true),
_connectionJson(protocol: 'https', address: '203.0.113.10', port: 32400, uri: remotePlexDirect),
]),
);
final urls = server.prioritizedEndpointUrls(preferredFirst: preferred);
expect(server.networkClassForUrl(preferred), PlexNetworkClass.remote);
expect(urls.first, preferred);
expect(urls, contains(remotePlexDirect));
expect(urls, isNot(contains(localPlexDirect)));
expect(urls, isNot(contains('http://192.168.1.50:32400')));
});
test('does not treat custom local-looking preferred endpoint as remote', () {
const preferred = 'https://plex.lan';
const localPlexDirect = 'https://192-168-1-50.abc.plex.direct:32400';
final server = PlexServer.fromJson(
_serverJsonWithConnections([
_connectionJson(protocol: 'https', address: '192.168.1.50', port: 32400, uri: localPlexDirect, local: true),
]),
);
final urls = server.prioritizedEndpointUrls(preferredFirst: preferred);
expect(server.networkClassForUrl(preferred), PlexNetworkClass.unknown);
expect(urls.first, preferred);
expect(urls, contains(localPlexDirect));
});
});
}