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