diff --git a/dev/devicelab/lib/framework/cocoon.dart b/dev/devicelab/lib/framework/cocoon.dart index f13a61ea00..707b9a3b9b 100644 --- a/dev/devicelab/lib/framework/cocoon.dart +++ b/dev/devicelab/lib/framework/cocoon.dart @@ -36,6 +36,7 @@ class Cocoon { @visibleForTesting Client httpClient, @visibleForTesting this.fs = const LocalFileSystem(), @visibleForTesting this.processRunSync = Process.runSync, + @visibleForTesting this.requestRetryLimit = 5, }) : _httpClient = AuthenticatedCocoonClient(serviceAccountTokenPath, httpClient: httpClient, filesystem: fs); /// Client to make http requests to Cocoon. @@ -51,6 +52,9 @@ class Cocoon { static final Logger logger = Logger('CocoonClient'); + @visibleForTesting + final int requestRetryLimit; + String get commitSha => _commitSha ?? _readCommitSha(); String _commitSha; @@ -122,6 +126,7 @@ class Cocoon { if (resultFile.existsSync()) { resultFile.deleteSync(); } + logger.fine('Writing results: ' + json.encode(updateRequest)); resultFile.createSync(); resultFile.writeAsStringSync(json.encode(updateRequest)); } @@ -137,6 +142,7 @@ class Cocoon { 'BuilderName': builderName, 'NewStatus': result.succeeded ? 'Succeeded' : 'Failed', }; + logger.fine('Update request: $updateRequest'); // Make a copy of result data because we may alter it for validation below. updateRequest['ResultData'] = result.data; @@ -177,7 +183,7 @@ class Cocoon { final Response response = await retry( () => _httpClient.post(url, body: json.encode(jsonData)), retryIf: (Exception e) => e is SocketException || e is TimeoutException || e is ClientException, - maxAttempts: 5, + maxAttempts: requestRetryLimit, ); return json.decode(response.body) as Map; } diff --git a/dev/devicelab/test/cocoon_test.dart b/dev/devicelab/test/cocoon_test.dart index a1f9ccd977..cfdb30219e 100644 --- a/dev/devicelab/test/cocoon_test.dart +++ b/dev/devicelab/test/cocoon_test.dart @@ -104,6 +104,36 @@ void main() { expect(resultJson, expectedJson); }); + test('uploads metrics sends expected post body', () async { + _processResult = ProcessResult(1, 0, commitSha, ''); + const String uploadMetricsRequestWithSpaces = '{"CommitBranch":"master","CommitSha":"a4952838bf288a81d8ea11edfd4b4cd649fa94cc","BuilderName":"builder a b c","NewStatus":"Succeeded","ResultData":{},"BenchmarkScoreKeys":[]}'; + final MockClient client = MockClient((Request request) async { + if (request.body == uploadMetricsRequestWithSpaces) { + return Response('{}', 200); + } + + return Response('Expected: $uploadMetricsRequestWithSpaces\nReceived: ${request.body}', 500); + }); + cocoon = Cocoon( + fs: fs, + httpClient: client, + processRunSync: runSyncStub, + serviceAccountTokenPath: serviceAccountTokenPath, + requestRetryLimit: 0, + ); + + const String resultsPath = 'results.json'; + const String updateTaskJson = '{' + '"CommitBranch":"master",' + '"CommitSha":"$commitSha",' + '"BuilderName":"builder a b c",' + '"NewStatus":"Succeeded",' + '"ResultData":{},' + '"BenchmarkScoreKeys":[]}'; + fs.file(resultsPath).writeAsStringSync(updateTaskJson); + await cocoon.sendResultsPath(resultsPath); + }); + test('uploads expected update task payload from results file', () async { _processResult = ProcessResult(1, 0, commitSha, ''); cocoon = Cocoon( @@ -111,6 +141,7 @@ void main() { httpClient: mockClient, processRunSync: runSyncStub, serviceAccountTokenPath: serviceAccountTokenPath, + requestRetryLimit: 0, ); const String resultsPath = 'results.json'; @@ -132,6 +163,7 @@ void main() { serviceAccountTokenPath: serviceAccountTokenPath, fs: fs, httpClient: mockClient, + requestRetryLimit: 0, ); final TaskResult result = TaskResult.success({}); @@ -146,6 +178,7 @@ void main() { serviceAccountTokenPath: serviceAccountTokenPath, fs: fs, httpClient: mockClient, + requestRetryLimit: 0, ); final TaskResult result = TaskResult.success({}); @@ -160,6 +193,7 @@ void main() { serviceAccountTokenPath: serviceAccountTokenPath, fs: fs, httpClient: mockClient, + requestRetryLimit: 0, ); final TaskResult result = TaskResult.success({});