fix(train): the firmware relay's calls to Polar time out, are bounded and limited per person (ADR 0030, D55)
Like BRouter's and Photon's: timeouts on connecting and reading, a check answer up to 64 KB and a file up to 64 MB, no redirect but to https, one fetch per file at a time, and 10 checks and downloads a minute for a person (tooOften). An answer that names no update properly is unreachable, no longer no update. The relay is the pod's, as routing's and search's. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Tizian 「ティツィアーン」 <raisondetredev@pm.me>
This commit is contained in:
parent
ac6cff179f
commit
0240a80faf
1 changed files with 481 additions and 92 deletions
|
|
@ -1,8 +1,8 @@
|
|||
import 'dart:async';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:clock/clock.dart';
|
||||
|
||||
import 'package:halkyon_train_server/src/firmware/firmware_endpoint.dart';
|
||||
import 'package:halkyon_train_server/src/firmware/firmware_relay.dart';
|
||||
import 'package:halkyon_train_server/src/generated/protocol.dart';
|
||||
import 'package:serverpod/serverpod.dart';
|
||||
|
|
@ -21,6 +21,9 @@ class _FakePolar() implements PolarFirmwareService {
|
|||
int fetches = 0;
|
||||
bool down = false;
|
||||
|
||||
/// Where set, fetches wait for it.
|
||||
Completer<void>? fetching;
|
||||
|
||||
@override
|
||||
Future<({String version, Uri file})?> check({
|
||||
required String uuid,
|
||||
|
|
@ -37,13 +40,18 @@ class _FakePolar() implements PolarFirmwareService {
|
|||
@override
|
||||
Future<Uint8List> fetch(Uri file) async {
|
||||
fetches++;
|
||||
await fetching?.future;
|
||||
return Uint8List.fromList([1, 2, 3]);
|
||||
}
|
||||
}
|
||||
|
||||
Matcher _refused(String reason) => throwsA(
|
||||
isA<LoopFirmwareRefused>().having((e) => e.reason, 'reason', reason),
|
||||
);
|
||||
|
||||
/// The Loop's firmware through the instance (ADR 0030, D10, D40): for the
|
||||
/// people signed in, unless the operator turned it off; each file fetched
|
||||
/// once for every band of its hardware.
|
||||
/// people signed in, unless the operator turned it off, within a limit a
|
||||
/// minute (D11); each file fetched once for every band of its hardware.
|
||||
void main() {
|
||||
withServerpod('Relaying the Loop firmware', (sessionBuilder, endpoints) {
|
||||
late _FakePolar polar;
|
||||
|
|
@ -53,11 +61,19 @@ void main() {
|
|||
{},
|
||||
),
|
||||
);
|
||||
final other = sessionBuilder.copyWith(
|
||||
authentication: AuthenticationOverride.authenticationInfo(
|
||||
const Uuid().v7(),
|
||||
{},
|
||||
),
|
||||
);
|
||||
|
||||
void useRelay(FirmwareRelay relay) =>
|
||||
FirmwareRelay.use(sessionBuilder.build().serverpod, relay);
|
||||
|
||||
setUp(() {
|
||||
polar = _FakePolar();
|
||||
firmwareRelay = FirmwareRelay(polar);
|
||||
firmwareRelayed = () => true;
|
||||
useRelay(FirmwareRelay(polar));
|
||||
});
|
||||
|
||||
test('names an update to someone signed in, and hands its file', () async {
|
||||
|
|
@ -91,6 +107,48 @@ void main() {
|
|||
expect(polar.fetches, 1);
|
||||
});
|
||||
|
||||
test(
|
||||
'fetches a file once for those downloading it at the same time',
|
||||
() async {
|
||||
await endpoints.firmware.check(signedIn, 'uuid-1', '3.0.0', 'HW-1');
|
||||
polar.fetching = Completer();
|
||||
|
||||
final first = endpoints.firmware.download(signedIn, 'HW-1', '3.1.2');
|
||||
final second = endpoints.firmware.download(other, 'HW-1', '3.1.2');
|
||||
await pumpEventQueue();
|
||||
polar.fetching!.complete();
|
||||
|
||||
for (final file in await Future.wait([first, second])) {
|
||||
expect(Uint8List.sublistView(file), [1, 2, 3]);
|
||||
}
|
||||
expect(polar.fetches, 1);
|
||||
},
|
||||
);
|
||||
|
||||
test('refuses a person beyond the checks and downloads of a minute, not '
|
||||
'others, and relays again a minute later', () async {
|
||||
var now = DateTime.utc(2026, 10, 28, 12);
|
||||
await withClock(Clock(() => now), () async {
|
||||
await endpoints.firmware.check(signedIn, 'uuid-1', '3.0.0', 'HW-1');
|
||||
for (var i = 1; i < FirmwareRelay.perMinute; i++) {
|
||||
await endpoints.firmware.download(signedIn, 'HW-1', '3.1.2');
|
||||
}
|
||||
await expectLater(
|
||||
endpoints.firmware.check(signedIn, 'uuid-1', '3.0.0', 'HW-1'),
|
||||
_refused('tooOften'),
|
||||
);
|
||||
await expectLater(
|
||||
endpoints.firmware.download(signedIn, 'HW-1', '3.1.2'),
|
||||
_refused('tooOften'),
|
||||
);
|
||||
expect(polar.checks, 1);
|
||||
await endpoints.firmware.check(other, 'uuid-2', '3.0.0', 'HW-1');
|
||||
|
||||
now = now.add(const Duration(minutes: 1));
|
||||
await endpoints.firmware.check(signedIn, 'uuid-1', '3.0.0', 'HW-1');
|
||||
});
|
||||
});
|
||||
|
||||
test('hands no file a check never named', () async {
|
||||
await expectLater(
|
||||
endpoints.firmware.download(signedIn, 'HW-9', '9.9.9'),
|
||||
|
|
@ -120,7 +178,7 @@ void main() {
|
|||
expect(polar.checks, 0);
|
||||
});
|
||||
|
||||
test("says Polar didn't answer", () async {
|
||||
test("says Polar didn't answer, or not as it does", () async {
|
||||
polar.down = true;
|
||||
|
||||
await expectLater(
|
||||
|
|
@ -138,7 +196,7 @@ void main() {
|
|||
test(
|
||||
'relays nothing where the operator turned it off, and says so',
|
||||
() async {
|
||||
firmwareRelayed = () => false;
|
||||
useRelay(FirmwareRelay(polar, offered: false));
|
||||
|
||||
expect(await endpoints.firmware.offered(signedIn), isFalse);
|
||||
await expectLater(
|
||||
|
|
@ -185,23 +243,26 @@ void main() {
|
|||
test('a file is kept a day, then fetched again after a new check', () async {
|
||||
final polar = _FakePolar();
|
||||
final relay = FirmwareRelay(polar);
|
||||
final person = const Uuid().v7obj();
|
||||
var now = DateTime.utc(2026, 10, 28, 9);
|
||||
await withClock(Clock(() => now), () async {
|
||||
await relay.check(
|
||||
person,
|
||||
uuid: 'uuid-1',
|
||||
firmwareVersion: '3.0.0',
|
||||
hardwareCode: 'HW-1',
|
||||
);
|
||||
await relay.download(hardwareCode: 'HW-1', version: '3.1.2');
|
||||
await relay.download(person, hardwareCode: 'HW-1', version: '3.1.2');
|
||||
now = now.add(const Duration(hours: 25));
|
||||
|
||||
// Checked again a day on, before any download: fetched anew.
|
||||
await relay.check(
|
||||
person,
|
||||
uuid: 'uuid-1',
|
||||
firmwareVersion: '3.0.0',
|
||||
hardwareCode: 'HW-1',
|
||||
);
|
||||
await relay.download(hardwareCode: 'HW-1', version: '3.1.2');
|
||||
await relay.download(person, hardwareCode: 'HW-1', version: '3.1.2');
|
||||
});
|
||||
|
||||
expect(polar.fetches, 2);
|
||||
|
|
|
|||
Loading…
Reference in a new issue