test: Add a DST and tests reproducing found failures (#110)

Co-authored-by: Marcelo Soares <marcelo.me.soares@gmail.com>
This commit is contained in:
Kasper Overgård Nielsen 2026-09-04 18:09:20 +02:00 • committed by GitHub
commit c78af247fe
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,18 +1,17 @@
import 'package:clock/clock.dart';
import 'package:serverpod_offline_sync/serverpod_offline_sync.dart';
import 'package:test/test.dart';
import 'package:uuid/uuid.dart';
final _dateTime = DateTime.parse('2001-09-09T01:46:40.000Z');
final _nodeId = const Uuid().v4obj();
import 'hlc_fixtures.dart';
/// [Hlc.increment] derives its result from `clock.now()`, so every increment
/// here runs with the wall clock pinned to an explicit instant.
void main() {
group('Given an HLC with lower canonical time than wall time', () {
final hlc = Hlc(_dateTime, 17, _nodeId);
final wallTime = _dateTime.advance();
final hlc = Hlc(hlcTime, 17, hlcNodeId);
final wallTime = hlcTime.advance();
test('when incrementing then dateTime becomes wall time and counter resets.', () {
withClock(Clock.fixed(wallTime), () {
atWallTime(wallTime, () {
final sendHlc = hlc.increment();
expect(sendHlc, isNot(hlc));
@ -24,11 +23,11 @@ void main() {
});
group('Given an HLC with equal canonical time and wall time', () {
final hlc = Hlc(_dateTime, 17, _nodeId);
final wallTime = _dateTime;
final hlc = Hlc(hlcTime, 17, hlcNodeId);
final wallTime = hlcTime;
test('when incrementing then counter increments and dateTime is unchanged.', () {
withClock(Clock.fixed(wallTime), () {
atWallTime(wallTime, () {
final sendHlc = hlc.increment();
expect(sendHlc, isNot(hlc));
@ -40,11 +39,11 @@ void main() {
});
group('Given an HLC with higher canonical time than wall time', () {
final hlc = Hlc(_dateTime, 17, _nodeId);
final wallTime = _dateTime.retreat();
final hlc = Hlc(hlcTime, 17, hlcNodeId);
final wallTime = hlcTime.retreat();
test('when incrementing then counter increments and dateTime is unchanged.', () {
withClock(Clock.fixed(wallTime), () {
atWallTime(wallTime, () {
final sendHlc = hlc.increment();
expect(sendHlc, isNot(hlc));
@ -56,29 +55,24 @@ void main() {
});
group('Given an HLC with canonical time more than one minute ahead of wall time', () {
final hlc = Hlc(_dateTime.add(const Duration(minutes: 1, seconds: 5)), 0, _nodeId);
final wallTime = _dateTime;
final hlc = Hlc(hlcTime.add(const Duration(minutes: 1, seconds: 5)), 0, hlcNodeId);
final wallTime = hlcTime;
test('when incrementing then ClockDriftException is thrown.', () {
withClock(Clock.fixed(wallTime), () {
atWallTime(wallTime, () {
expect(hlc.increment, throwsA(isA<ClockDriftException>()));
});
});
});
group('Given an HLC with counter at maximum and canonical time at wall time', () {
final hlc = Hlc(_dateTime, 0xFFFF, _nodeId);
final wallTime = _dateTime;
final hlc = Hlc(hlcTime, 0xFFFF, hlcNodeId);
final wallTime = hlcTime;
test('when incrementing at same time then OverflowException is thrown.', () {
withClock(Clock.fixed(wallTime), () {
atWallTime(wallTime, () {
expect(hlc.increment, throwsA(isA<OverflowException>()));
});
});
});
}
extension on DateTime {
DateTime advance() => add(const Duration(milliseconds: 1));
DateTime retreat() => subtract(const Duration(milliseconds: 1));
}