Skip to content

Commit f358825

Browse files
committed
Migrate away from fake_time
1 parent 4dce8b3 commit f358825

File tree

12 files changed

+121
-928
lines changed

12 files changed

+121
-928
lines changed

app/lib/package/api_export/api_exporter.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import 'package:pub_dev/frontend/handlers/atom_feed.dart';
1313
import 'package:pub_dev/service/security_advisories/backend.dart';
1414
import 'package:pub_dev/shared/exceptions.dart';
1515
import 'package:pub_dev/shared/parallel_foreach.dart';
16+
import 'package:pub_dev/task/clock_control.dart';
1617

1718
import '../../search/backend.dart';
1819
import '../../shared/datastore.dart';
@@ -308,7 +309,8 @@ final class ApiExporter {
308309
seen.removeWhere((_, updated) => updated.isBefore(since));
309310

310311
// Wait until aborted or 10 minutes before scanning again!
311-
await abort.future.timeout(Duration(minutes: 10), onTimeout: () => null);
312+
await abort.future
313+
.timeoutWithClock(Duration(minutes: 10), onTimeout: () => null);
312314
}
313315
}
314316

app/lib/service/services.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ Future<R> _withPubServices<R>(FutureOr<R> Function() fn) async {
300300

301301
// Create a zone-local flag to indicate that services setup has been completed.
302302
return await fork(
303-
() => Zone.current.fork(zoneValues: {
303+
() async => Zone.current.fork(zoneValues: {
304304
_pubDevServicesInitializedKey: true,
305305
}).run(
306306
() async {

app/lib/task/backend.dart

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import 'package:pub_dev/shared/versions.dart'
3636
acceptedRuntimeVersions;
3737
import 'package:pub_dev/shared/versions.dart' as shared_versions
3838
show runtimeVersion;
39+
import 'package:pub_dev/task/clock_control.dart';
3940
import 'package:pub_dev/task/cloudcompute/cloudcompute.dart';
4041
import 'package:pub_dev/task/global_lock.dart';
4142
import 'package:pub_dev/task/handlers.dart';
@@ -138,7 +139,7 @@ class TaskBackend {
138139
st,
139140
);
140141
// Sleep 5 minutes to reduce risk of degenerate behavior
141-
await Future.delayed(Duration(minutes: 5));
142+
await clock.delayed(Duration(minutes: 5));
142143
}
143144
}
144145
} catch (e, st) {
@@ -176,7 +177,7 @@ class TaskBackend {
176177
st,
177178
);
178179
// Sleep 5 minutes to reduce risk of degenerate behavior
179-
await Future.delayed(Duration(minutes: 5));
180+
await clock.delayed(Duration(minutes: 5));
180181
}
181182
}
182183
} catch (e, st) {
@@ -349,7 +350,8 @@ class TaskBackend {
349350
seen.removeWhere((_, updated) => updated.isBefore(since));
350351

351352
// Wait until aborted or 10 minutes before scanning again!
352-
await abort.future.timeout(Duration(minutes: 10), onTimeout: () => null);
353+
await abort.future
354+
.timeoutWithClock(Duration(minutes: 10), onTimeout: () => null);
353355
}
354356
}
355357

app/lib/task/cloudcompute/fakecloudcompute.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import 'package:collection/collection.dart';
1010
import 'package:logging/logging.dart';
1111
import 'package:path/path.dart' as p;
1212
import 'package:pub_dev/frontend/static_files.dart';
13+
import 'package:pub_dev/task/clock_control.dart';
1314

1415
import 'cloudcompute.dart';
1516

@@ -75,7 +76,8 @@ final class FakeCloudCompute extends CloudCompute {
7576
}
7677

7778
@override
78-
Stream<FakeCloudInstance> listInstances() => Stream.fromIterable(_instances);
79+
Stream<FakeCloudInstance> listInstances() =>
80+
Stream.fromIterable([..._instances]);
7981

8082
@override
8183
Future<void> delete(String zone, String instanceName) async {
@@ -90,7 +92,7 @@ final class FakeCloudCompute extends CloudCompute {
9092

9193
// Let's make the operation take a second, and then remove the instance!
9294
_log.info('Deleting instance "$instanceName"');
93-
await Future.delayed(Duration(seconds: 1));
95+
await clock.delayed(Duration(seconds: 1));
9496
final removed = _instances
9597
.where((i) => i.instanceName == instanceName && i.zone == zone)
9698
.toList();

app/lib/task/global_lock.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'dart:async';
77
import 'package:clock/clock.dart';
88
import 'package:logging/logging.dart' show Logger;
99
import 'package:pub_dev/shared/datastore.dart';
10+
import 'package:pub_dev/task/clock_control.dart';
1011
import 'package:pub_dev/task/global_lock_models.dart';
1112
import 'package:ulid/ulid.dart' show Ulid;
1213

@@ -58,7 +59,7 @@ class GlobalLock {
5859
// refresh, before it's truly expired.
5960

6061
// Wait for done or delay
61-
await done.future.timeout(delay, onTimeout: () => null);
62+
await done.future.timeoutWithClock(delay, onTimeout: () => null);
6263

6364
// Try to refresh, if claim is still valid and we're not done.
6465
if (c.valid && !done.isCompleted) {
@@ -158,7 +159,7 @@ class GlobalLock {
158159
delay = _expiration * 0.1;
159160
}
160161
// Wait for delay or abort
161-
await abort.future.timeout(delay, onTimeout: () => null);
162+
await abort.future.timeoutWithClock(delay, onTimeout: () => null);
162163
}
163164
e = await _tryClaimOrGet(claimId);
164165
}

app/lib/task/scheduler.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import 'package:pub_dev/shared/configuration.dart';
1111
import 'package:pub_dev/shared/datastore.dart';
1212
import 'package:pub_dev/shared/utils.dart';
1313
import 'package:pub_dev/shared/versions.dart' show runtimeVersion;
14+
import 'package:pub_dev/task/clock_control.dart';
1415
import 'package:pub_dev/task/cloudcompute/cloudcompute.dart';
1516
import 'package:pub_dev/task/global_lock.dart';
1617
import 'package:pub_dev/task/models.dart';
@@ -39,7 +40,7 @@ Future<void> schedule(
3940
// Await a micro task to ensure consistent behavior
4041
await Future.microtask(() {});
4142
} else {
42-
await abort.future.timeout(delay, onTimeout: () => null);
43+
await abort.future.timeoutWithClock(delay, onTimeout: () => null);
4344
}
4445
}
4546

app/test/package/api_export/api_exporter_test.dart

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import 'package:test/test.dart';
2323

2424
import '../../shared/test_models.dart';
2525
import '../../shared/test_services.dart';
26-
import '../../task/fake_time.dart';
2726

2827
final _log = Logger('api_export.test');
2928

@@ -43,12 +42,12 @@ final _testProfile = TestProfile(
4342
);
4443

4544
void main() {
46-
testWithFakeTime('synchronizeExportedApi()',
45+
testWithProfile('synchronizeExportedApi()',
4746
testProfile: _testProfile,
4847
expectedLogMessages: [
4948
'SHOUT Deleting object from public bucket: "packages/bar-2.0.0.tar.gz".',
5049
'SHOUT Deleting object from public bucket: "packages/bar-3.0.0.tar.gz".',
51-
], (fakeTime) async {
50+
], fn: () async {
5251
// Since we want to verify post-upload tasks triggering API exporter,
5352
// we cannot use an isolated instance, we need to use the same setup.
5453
// However, for better control and consistency, we can remove all the
@@ -59,20 +58,19 @@ void main() {
5958
await _deleteAll(bucket);
6059

6160
await _testExportedApiSynchronization(
62-
fakeTime,
6361
bucket,
6462
apiExporter!.synchronizeExportedApi,
6563
);
6664
});
6765

68-
testWithFakeTime(
66+
testWithProfile(
6967
'apiExporter.start()',
7068
expectedLogMessages: [
7169
'SHOUT Deleting object from public bucket: "packages/bar-2.0.0.tar.gz".',
7270
'SHOUT Deleting object from public bucket: "packages/bar-3.0.0.tar.gz".',
7371
],
7472
testProfile: _testProfile,
75-
(fakeTime) async {
73+
fn: () async {
7674
// Since we want to verify post-upload tasks triggering API exporter,
7775
// we cannot use an isolated instance, we need to use the same setup.
7876
// However, for better control and consistency, we can remove all the
@@ -87,9 +85,8 @@ void main() {
8785
await apiExporter!.start();
8886

8987
await _testExportedApiSynchronization(
90-
fakeTime,
9188
bucket,
92-
() async => await fakeTime.elapse(minutes: 15),
89+
() async => await clockControl.elapse(minutes: 15),
9390
);
9491

9592
await apiExporter!.stop();
@@ -106,7 +103,6 @@ Future<void> _deleteAll(Bucket bucket) async {
106103
}
107104

108105
Future<void> _testExportedApiSynchronization(
109-
FakeTime fakeTime,
110106
Bucket bucket,
111107
Future<void> Function() synchronize,
112108
) async {
@@ -341,7 +337,7 @@ Future<void> _testExportedApiSynchronization(
341337
{
342338
// Elapse time before moderating package, because exported-api won't delete
343339
// recently created files as a guard against race conditions.
344-
fakeTime.elapseSync(days: 1);
340+
clockControl.elapseSync(days: 1);
345341

346342
await withRetryPubApiClient(
347343
authToken: createFakeServiceAccountToken(email: '[email protected]'),
@@ -422,7 +418,7 @@ Future<void> _testExportedApiSynchronization(
422418
{
423419
// Elapse time before moderating package, because exported-api won't delete
424420
// recently created files as a guard against race conditions.
425-
fakeTime.elapseSync(days: 1);
421+
clockControl.elapseSync(days: 1);
426422

427423
await withRetryPubApiClient(
428424
authToken: createFakeServiceAccountToken(email: '[email protected]'),

app/test/package/api_export/exported_api_test.dart

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ void main() {
189189
);
190190
});
191191

192-
testWithFakeTime('ExportedApi.garbageCollect()', (fakeTime) async {
192+
testWithProfile('ExportedApi.garbageCollect()', fn: () async {
193193
await storageService.createBucket('exported-api');
194194
final bucket = storageService.bucket('exported-api');
195195
final exportedApi = ExportedApi(storageService, bucket);
@@ -208,15 +208,15 @@ void main() {
208208
);
209209

210210
// Check that GC after 10 mins won't delete a package we don't recognize
211-
fakeTime.elapseSync(minutes: 10);
211+
clockControl.elapseSync(minutes: 10);
212212
await exportedApi.garbageCollect({});
213213
expect(
214214
await bucket.readGzippedJson('latest/api/packages/retry'),
215215
isNotNull,
216216
);
217217

218218
// Check that GC after 2 days won't delete a package we know
219-
fakeTime.elapseSync(days: 2);
219+
clockControl.elapseSync(days: 2);
220220
await exportedApi.garbageCollect({'retry'});
221221
expect(
222222
await bucket.readGzippedJson('latest/api/packages/retry'),
@@ -256,8 +256,7 @@ void main() {
256256
}
257257
});
258258

259-
testWithFakeTime('ExportedApi.package().synchronizeTarballs()',
260-
(fakeTime) async {
259+
testWithProfile('ExportedApi.package().synchronizeTarballs()', fn: () async {
261260
await storageService.createBucket('exported-api');
262261
final bucket = storageService.bucket('exported-api');
263262
final exportedApi = ExportedApi(storageService, bucket);
@@ -313,7 +312,7 @@ void main() {
313312
[3, 0, 0],
314313
);
315314

316-
fakeTime.elapseSync(days: 2);
315+
clockControl.elapseSync(days: 2);
317316

318317
await exportedApi.package('retry').synchronizeTarballs({
319318
'1.0.0': src1,
@@ -338,7 +337,7 @@ void main() {
338337
);
339338
});
340339

341-
testWithFakeTime('ExportedApi.package().garbageCollect()', (fakeTime) async {
340+
testWithProfile('ExportedApi.package().garbageCollect()', fn: () async {
342341
await storageService.createBucket('exported-api');
343342
final bucket = storageService.bucket('exported-api');
344343
final exportedApi = ExportedApi(storageService, bucket);
@@ -363,7 +362,7 @@ void main() {
363362
);
364363

365364
// Nothing is GC'ed after 10 mins
366-
fakeTime.elapseSync(minutes: 10);
365+
clockControl.elapseSync(minutes: 10);
367366
await exportedApi.package('retry').garbageCollect({'1.2.3'});
368367
expect(
369368
await bucket.readBytes('latest/api/archives/retry-1.2.3.tar.gz'),
@@ -375,7 +374,7 @@ void main() {
375374
);
376375

377376
// Something is GC'ed after 2 days
378-
fakeTime.elapseSync(days: 2);
377+
clockControl.elapseSync(days: 2);
379378
await exportedApi.package('retry').garbageCollect({'1.2.3'});
380379
expect(
381380
await bucket.readBytes('latest/api/archives/retry-1.2.3.tar.gz'),

0 commit comments

Comments
 (0)