Skip to content

Commit 5ddf8fc

Browse files
authored
Speeding up the Project Management Unit Tests (#256)
* Speeding up project mgt unit tests * Moved FirebaseAppScheduler out of the interface file * Removed extra whitespace
1 parent 13eaaa0 commit 5ddf8fc

File tree

3 files changed

+67
-12
lines changed

3 files changed

+67
-12
lines changed

src/main/java/com/google/firebase/projectmanagement/FirebaseProjectManagementServiceImpl.java

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616
package com.google.firebase.projectmanagement;
1717

1818
import static com.google.common.base.Preconditions.checkArgument;
19+
import static com.google.common.base.Preconditions.checkNotNull;
1920

2021
import com.google.api.client.http.HttpResponseInterceptor;
2122
import com.google.api.client.util.Base64;
2223
import com.google.api.client.util.Key;
24+
import com.google.api.client.util.Sleeper;
2325
import com.google.api.core.ApiAsyncFunction;
2426
import com.google.api.core.ApiFunction;
2527
import com.google.api.core.ApiFuture;
@@ -51,6 +53,8 @@ class FirebaseProjectManagementServiceImpl implements AndroidAppService, IosAppS
5153
private static final String IOS_NAMESPACE_PROPERTY = "bundle_id";
5254

5355
private final FirebaseApp app;
56+
private final Sleeper sleeper;
57+
private final Scheduler scheduler;
5458
private final HttpHelper httpHelper;
5559

5660
private final CreateAndroidAppFromAppIdFunction createAndroidAppFromAppIdFunction =
@@ -59,7 +63,13 @@ class FirebaseProjectManagementServiceImpl implements AndroidAppService, IosAppS
5963
new CreateIosAppFromAppIdFunction();
6064

6165
FirebaseProjectManagementServiceImpl(FirebaseApp app) {
62-
this.app = app;
66+
this(app, Sleeper.DEFAULT, new FirebaseAppScheduler(app));
67+
}
68+
69+
FirebaseProjectManagementServiceImpl(FirebaseApp app, Sleeper sleeper, Scheduler scheduler) {
70+
this.app = checkNotNull(app);
71+
this.sleeper = checkNotNull(sleeper);
72+
this.scheduler = checkNotNull(scheduler);
6373
this.httpHelper = new HttpHelper(
6474
app.getOptions().getJsonFactory(),
6575
app.getOptions().getHttpTransport().createRequestFactory(
@@ -187,7 +197,7 @@ protected List<T> execute() throws FirebaseProjectManagementException {
187197
projectId,
188198
platformResourceName,
189199
MAXIMUM_LIST_APPS_PAGE_SIZE);
190-
ImmutableList.Builder<T> builder = ImmutableList.<T>builder();
200+
ImmutableList.Builder<T> builder = ImmutableList.builder();
191201
ListAppsResponse parsedResponse;
192202
do {
193203
parsedResponse = new ListAppsResponse();
@@ -361,10 +371,9 @@ private WaitOperationFunction(String projectId) {
361371
* or an exception if an error occurred during polling.
362372
*/
363373
@Override
364-
public ApiFuture<String> apply(String operationName) throws FirebaseProjectManagementException {
365-
SettableApiFuture<String> settableFuture = SettableApiFuture.<String>create();
366-
ImplFirebaseTrampolines.schedule(
367-
app,
374+
public ApiFuture<String> apply(String operationName) {
375+
SettableApiFuture<String> settableFuture = SettableApiFuture.create();
376+
scheduler.schedule(
368377
new WaitOperationRunnable(
369378
/* numberOfPreviousPolls= */ 0,
370379
operationName,
@@ -417,8 +426,7 @@ public void run() {
417426
long delayMillis = (long) (
418427
POLL_BASE_WAIT_TIME_MILLIS
419428
* Math.pow(POLL_EXPONENTIAL_BACKOFF_FACTOR, numberOfPreviousPolls + 1));
420-
ImplFirebaseTrampolines.schedule(
421-
app,
429+
scheduler.schedule(
422430
new WaitOperationRunnable(
423431
numberOfPreviousPolls + 1,
424432
operationName,
@@ -725,12 +733,26 @@ public static class ShaCertificateResponse {
725733
private String certType;
726734
}
727735

736+
private static class FirebaseAppScheduler implements Scheduler {
737+
738+
private final FirebaseApp app;
739+
740+
FirebaseAppScheduler(FirebaseApp app) {
741+
this.app = checkNotNull(app);
742+
}
743+
744+
@Override
745+
public void schedule(Runnable runnable, long delayMillis) {
746+
ImplFirebaseTrampolines.schedule(app, runnable, delayMillis);
747+
}
748+
}
749+
728750
/* Helper methods. */
729751

730752
private void sleepOrThrow(String projectId, long delayMillis)
731753
throws FirebaseProjectManagementException {
732754
try {
733-
Thread.sleep(delayMillis);
755+
sleeper.sleep(delayMillis);
734756
} catch (InterruptedException e) {
735757
throw HttpHelper.createFirebaseProjectManagementException(
736758
projectId,
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2019 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.firebase.projectmanagement;
18+
19+
/**
20+
* Schedules a task to be executed after a specified delay.
21+
*/
22+
interface Scheduler {
23+
24+
void schedule(Runnable runnable, long delayMillis);
25+
}

src/test/java/com/google/firebase/projectmanagement/FirebaseProjectManagementServiceImplTest.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import com.google.api.client.json.JsonParser;
3535
import com.google.api.client.testing.http.MockHttpTransport;
3636
import com.google.api.client.testing.http.MockLowLevelHttpResponse;
37+
import com.google.api.client.testing.util.MockSleeper;
3738
import com.google.api.client.util.Base64;
3839
import com.google.common.base.Charsets;
3940
import com.google.common.collect.ImmutableList;
@@ -341,7 +342,7 @@ public void listIosAppsMultiplePages() throws Exception {
341342
MockLowLevelHttpResponse secondRpcResponse = new MockLowLevelHttpResponse();
342343
secondRpcResponse.setContent(LIST_IOS_APPS_PAGE_2_RESPONSE);
343344
serviceImpl = initServiceImpl(
344-
ImmutableList.<MockLowLevelHttpResponse>of(firstRpcResponse, secondRpcResponse),
345+
ImmutableList.of(firstRpcResponse, secondRpcResponse),
345346
interceptor);
346347

347348
List<IosApp> iosAppList = serviceImpl.listIosApps(PROJECT_ID);
@@ -931,7 +932,7 @@ private static FirebaseProjectManagementServiceImpl initServiceImpl(
931932
.build();
932933
FirebaseApp app = FirebaseApp.initializeApp(options);
933934
FirebaseProjectManagementServiceImpl serviceImpl =
934-
new FirebaseProjectManagementServiceImpl(app);
935+
new FirebaseProjectManagementServiceImpl(app, new MockSleeper(), new MockScheduler());
935936
serviceImpl.setInterceptor(interceptor);
936937
return serviceImpl;
937938
}
@@ -985,7 +986,7 @@ private enum HttpMethod {
985986
/**
986987
* Can be used to intercept multiple HTTP requests and responses made by the SDK during tests.
987988
*/
988-
private class MultiRequestTestResponseInterceptor implements HttpResponseInterceptor {
989+
private static class MultiRequestTestResponseInterceptor implements HttpResponseInterceptor {
989990
private final List<HttpResponse> responsesList = new ArrayList<>();
990991

991992
@Override
@@ -1001,4 +1002,11 @@ public HttpResponse getResponse(int index) {
10011002
return responsesList.get(index);
10021003
}
10031004
}
1005+
1006+
private static class MockScheduler implements Scheduler {
1007+
@Override
1008+
public void schedule(Runnable runnable, long delayMillis) {
1009+
runnable.run();
1010+
}
1011+
}
10041012
}

0 commit comments

Comments
 (0)