refactor(shorebird_cli): rename gcp upload methods for clarity (#2554)

This commit is contained in:
Felix Angelov
2024-10-18 12:36:28 -05:00
committed by GitHub
parent 5a540e4f69
commit 207cf4ffbf
8 changed files with 36 additions and 29 deletions
@@ -810,7 +810,9 @@ aar artifact already exists, continuing...''',
}
/// Returns a GCP upload link for measuring upload speed.
Future<Uri> getGCPSpeedTestUrl() => codePushClient.getGCPSpeedTestUrl();
Future<Uri> getGCPUploadSpeedTestUrl() {
return codePushClient.getGCPUploadSpeedTestUrl();
}
/// Prints an appropriate error message for the given error and exits with
/// code 70. If [progress] is provided, it will be failed with the given
@@ -104,12 +104,14 @@ Android Toolchain
final progress = logger.progress('Performing GCP speed test');
try {
final speed = await networkChecker.performGCPSpeedTest();
progress.complete('GCP Upload Speed: ${speed.toStringAsFixed(2)} MB/s');
final uploadSpeed = await networkChecker.performGCPUploadSpeedTest();
progress.complete(
'GCP Upload Speed: ${uploadSpeed.toStringAsFixed(2)} MB/s',
);
} on NetworkCheckerException catch (error) {
progress.fail('GCP speed test failed: ${error.message}');
progress.fail('GCP upload speed test failed: ${error.message}');
} catch (error) {
progress.fail('GCP speed test failed: $error');
progress.fail('GCP upload speed test failed: $error');
}
}
@@ -57,7 +57,7 @@ class NetworkChecker {
/// Uploads a file to GCP to measure upload speed. Returns the upload rate
/// in MB/s.
Future<double> performGCPSpeedTest({
Future<double> performGCPUploadSpeedTest({
// If they can't upload the file in two minutes, we can just say it's slow.
Duration uploadTimeout = const Duration(minutes: 2),
}) async {
@@ -69,7 +69,7 @@ class NetworkChecker {
final testFile = File(p.join(tempDir.path, 'speed_test_file'))
..writeAsBytesSync(ByteData(fileSize).buffer.asUint8List());
try {
final uri = await codePushClientWrapper.getGCPSpeedTestUrl();
final uri = await codePushClientWrapper.getGCPUploadSpeedTestUrl();
final start = clock.now();
final file = await http.MultipartFile.fromPath('file', testFile.path);
final uploadRequest = http.MultipartRequest('POST', uri)..files.add(file);
@@ -2361,11 +2361,11 @@ You can manage this release in the ${link(uri: uri, message: 'Shorebird Console'
});
});
group('getGCPSpeedTestUrl', () {
group('getGCPUploadSpeedTestUrl', () {
final gcpSpeedTestUrl = Uri.parse('https://speedtest.gcp.com');
setUp(() {
when(() => codePushClient.getGCPSpeedTestUrl()).thenAnswer(
when(() => codePushClient.getGCPUploadSpeedTestUrl()).thenAnswer(
(_) async => gcpSpeedTestUrl,
);
});
@@ -2373,12 +2373,12 @@ You can manage this release in the ${link(uri: uri, message: 'Shorebird Console'
test('calls codePushClient method', () async {
await expectLater(
runWithOverrides(
() => codePushClientWrapper.getGCPSpeedTestUrl(),
() => codePushClientWrapper.getGCPUploadSpeedTestUrl(),
),
completion(gcpSpeedTestUrl),
);
verify(() => codePushClient.getGCPSpeedTestUrl()).called(1);
verify(() => codePushClient.getGCPUploadSpeedTestUrl()).called(1);
});
});
});
@@ -83,7 +83,7 @@ void main() {
() => networkChecker.checkReachability(),
).thenAnswer((_) async => {});
when(
() => networkChecker.performGCPSpeedTest(),
() => networkChecker.performGCPUploadSpeedTest(),
).thenAnswer((_) async => 1.0);
when(
() => shorebirdEnv.shorebirdEngineRevision,
@@ -134,14 +134,14 @@ Engine • revision $shorebirdEngineRevision
'''),
).called(1);
verify(() => networkChecker.checkReachability()).called(1);
verifyNever(() => networkChecker.performGCPSpeedTest());
verifyNever(() => networkChecker.performGCPUploadSpeedTest());
});
group('--verbose', () {
setUp(() {
when(() => argResults['verbose']).thenReturn(true);
when(
() => networkChecker.performGCPSpeedTest(),
() => networkChecker.performGCPUploadSpeedTest(),
).thenAnswer((_) async => 1.23456789);
});
@@ -219,7 +219,7 @@ Android Toolchain
);
verify(() => networkChecker.checkReachability()).called(1);
verify(() => networkChecker.performGCPSpeedTest()).called(1);
verify(() => networkChecker.performGCPUploadSpeedTest()).called(1);
verify(
() => progress.complete('GCP Upload Speed: 1.23 MB/s'),
).called(1);
@@ -289,7 +289,7 @@ Android Toolchain
group('with NetworkCheckerException', () {
setUp(() {
when(
() => networkChecker.performGCPSpeedTest(),
() => networkChecker.performGCPUploadSpeedTest(),
).thenThrow(const NetworkCheckerException('oops'));
});
@@ -300,7 +300,7 @@ Android Toolchain
);
verify(
() => progress.fail('GCP speed test failed: oops'),
() => progress.fail('GCP upload speed test failed: oops'),
).called(1);
});
});
@@ -308,7 +308,7 @@ Android Toolchain
group('with generic Exception', () {
setUp(() {
when(
() => networkChecker.performGCPSpeedTest(),
() => networkChecker.performGCPUploadSpeedTest(),
).thenThrow(Exception('oops'));
});
@@ -319,7 +319,9 @@ Android Toolchain
);
verify(
() => progress.fail('GCP speed test failed: Exception: oops'),
() => progress.fail(
'GCP upload speed test failed: Exception: oops',
),
).called(1);
});
});
@@ -82,11 +82,11 @@ void main() {
});
});
group('performGCPSpeedTest', () {
group('performGCPUploadSpeedTest', () {
final gcpUri = Uri.parse('http://localhost');
setUp(() {
when(() => codePushClientWrapper.getGCPSpeedTestUrl()).thenAnswer(
when(() => codePushClientWrapper.getGCPUploadSpeedTestUrl()).thenAnswer(
(_) async => gcpUri,
);
});
@@ -103,7 +103,7 @@ void main() {
test('throws a NetworkCheckerException', () async {
await expectLater(
runWithOverrides(networkChecker.performGCPSpeedTest),
runWithOverrides(networkChecker.performGCPUploadSpeedTest),
throwsA(
isA<NetworkCheckerException>().having(
(e) => e.message,
@@ -135,7 +135,7 @@ void main() {
test('throws a NetworkCheckerException', () async {
await expectLater(
() => runWithOverrides(
() => networkChecker.performGCPSpeedTest(
() => networkChecker.performGCPUploadSpeedTest(
uploadTimeout: uploadTimeout,
),
),
@@ -177,8 +177,9 @@ void main() {
}
});
await withClock(clock, () async {
final speed =
await runWithOverrides(networkChecker.performGCPSpeedTest);
final speed = await runWithOverrides(
networkChecker.performGCPUploadSpeedTest,
);
// Our 5MB file took 1 second to upload, so our speed is 5 MB/s.
expect(speed, equals(5.0));
@@ -502,7 +502,7 @@ class CodePushClient {
}
/// Returns a GCP upload link for measuring upload speed.
Future<Uri> getGCPSpeedTestUrl() async {
Future<Uri> getGCPUploadSpeedTestUrl() async {
final response = await _httpClient.get(
Uri.parse('$_v1/diagnostics/gcp_upload'),
);
@@ -1978,7 +1978,7 @@ void main() {
});
});
group('getGCPSpeedTestUrl', () {
group('getGCPUploadSpeedTestUrl', () {
group('when request fails', () {
setUp(() {
when(() => httpClient.send(any())).thenAnswer(
@@ -1991,7 +1991,7 @@ void main() {
test('throws exception', () async {
expect(
() async => codePushClient.getGCPSpeedTestUrl(),
() async => codePushClient.getGCPUploadSpeedTestUrl(),
throwsA(
isA<CodePushException>().having(
(e) => e.message,
@@ -2016,7 +2016,7 @@ void main() {
});
test('returns upload_url as parsed Uri', () async {
final url = await codePushClient.getGCPSpeedTestUrl();
final url = await codePushClient.getGCPUploadSpeedTestUrl();
expect(url, equals(Uri.parse('https://example.com')));
});
});