Skip to content

Commit 797035d

Browse files
committed
Add better testing support for TransportRuntime.
1 parent 20da1e7 commit 797035d

File tree

6 files changed

+109
-4
lines changed

6 files changed

+109
-4
lines changed

subprojects.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,4 @@ transport
7575
transport:transport-api
7676
transport:transport-backend-cct
7777
transport:transport-runtime
78+
transport:transport-runtime-testing
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!-- Copyright 2018 Google LLC -->
2+
<!-- -->
3+
<!-- Licensed under the Apache License, Version 2.0 (the "License"); -->
4+
<!-- you may not use this file except in compliance with the License. -->
5+
<!-- You may obtain a copy of the License at -->
6+
<!-- -->
7+
<!-- http://www.apache.org/licenses/LICENSE-2.0 -->
8+
<!-- -->
9+
<!-- Unless required by applicable law or agreed to in writing, software -->
10+
<!-- distributed under the License is distributed on an "AS IS" BASIS, -->
11+
<!-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -->
12+
<!-- See the License for the specific language governing permissions and -->
13+
<!-- limitations under the License. -->
14+
15+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
16+
package="com.google.android.datatransport.runtime.testing">
17+
<!--Although the *SdkVersion is captured in gradle build files, this is required for non gradle builds-->
18+
<!--<uses-sdk android:minSdkVersion="14"/>-->
19+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.google.android.datatransport.runtime;
2+
3+
import com.google.android.datatransport.Priority;
4+
import com.google.android.datatransport.runtime.backends.BackendResponse;
5+
import com.google.android.datatransport.runtime.scheduling.jobscheduling.UploadTestSupport;
6+
7+
/** Test support for {@link TransportRuntime}. */
8+
public final class TransportRuntimeTesting {
9+
private TransportRuntimeTesting() {}
10+
11+
/** Synchronously force upload all scheduled events for a given destination and priority. */
12+
public static BackendResponse forceUpload(Destination destination, Priority priority) {
13+
return UploadTestSupport.forceUpload(
14+
TransportContext.builder()
15+
.setBackendName(destination.getName())
16+
.setExtras(destination.getExtras())
17+
.setPriority(priority)
18+
.build());
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.google.android.datatransport.runtime.scheduling.jobscheduling;
2+
3+
import com.google.android.datatransport.runtime.TransportContext;
4+
import com.google.android.datatransport.runtime.TransportRuntime;
5+
import com.google.android.datatransport.runtime.backends.BackendResponse;
6+
7+
/**
8+
* Support class that avoids making {@link Uploader#logAndUpdateState(TransportContext, int) }
9+
* method public in the SDK code.
10+
*/
11+
public class UploadTestSupport {
12+
private UploadTestSupport() {}
13+
14+
public static BackendResponse forceUpload(TransportContext context) {
15+
return TransportRuntime.getInstance().getUploader().logAndUpdateState(context, 1);
16+
}
17+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright 2019 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
plugins {
16+
// not a firebase-library, since we don't intend to release this artifact.
17+
id 'com.android.library'
18+
}
19+
20+
android {
21+
compileSdkVersion project.targetSdkVersion
22+
defaultConfig {
23+
minSdkVersion project.minSdkVersion
24+
targetSdkVersion project.targetSdkVersion
25+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
26+
}
27+
compileOptions {
28+
sourceCompatibility JavaVersion.VERSION_1_8
29+
targetCompatibility JavaVersion.VERSION_1_8
30+
}
31+
testOptions.unitTests {
32+
includeAndroidResources = true
33+
}
34+
}
35+
36+
dependencies {
37+
implementation project(':transport:transport-api')
38+
implementation project(':transport:transport-runtime')
39+
implementation 'androidx.annotation:annotation:1.3.0'
40+
41+
implementation project(':transport:transport-backend-cct')
42+
androidTestImplementation 'junit:junit:4.13.2'
43+
androidTestImplementation "com.google.truth:truth:$googleTruthVersion"
44+
androidTestImplementation 'androidx.test:runner:1.4.0'
45+
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
46+
androidTestImplementation 'androidx.test:rules:1.4.0'
47+
}

transport/transport-runtime/src/main/java/com/google/android/datatransport/runtime/scheduling/jobscheduling/Uploader.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,20 +111,20 @@ public void upload(TransportContext transportContext, int attemptNumber, Runnabl
111111
});
112112
}
113113

114-
void logAndUpdateState(TransportContext transportContext, int attemptNumber) {
114+
BackendResponse logAndUpdateState(TransportContext transportContext, int attemptNumber) {
115115
TransportBackend backend = backendRegistry.get(transportContext.getBackendName());
116116
long maxNextRequestWaitMillis = 0;
117117

118+
BackendResponse response = BackendResponse.ok(maxNextRequestWaitMillis);
118119
while (guard.runCriticalSection(() -> eventStore.hasPendingEventsFor(transportContext))) {
119120
Iterable<PersistedEvent> persistedEvents =
120121
guard.runCriticalSection(() -> eventStore.loadBatch(transportContext));
121122

122123
// Do not make a call to the backend if the list is empty.
123124
if (!persistedEvents.iterator().hasNext()) {
124-
return;
125+
return response;
125126
}
126127

127-
BackendResponse response;
128128
if (backend == null) {
129129
Logging.d(
130130
LOG_TAG, "Unknown backend for %s, deleting event batch for it...", transportContext);
@@ -157,7 +157,7 @@ void logAndUpdateState(TransportContext transportContext, int attemptNumber) {
157157
return null;
158158
});
159159
workScheduler.schedule(transportContext, attemptNumber + 1, true);
160-
return;
160+
return response;
161161
} else {
162162
guard.runCriticalSection(
163163
() -> {
@@ -202,6 +202,7 @@ void logAndUpdateState(TransportContext transportContext, int attemptNumber) {
202202
transportContext, clock.getTime() + finalMaxNextRequestWaitMillis);
203203
return null;
204204
});
205+
return response;
205206
}
206207

207208
@VisibleForTesting

0 commit comments

Comments
 (0)