Skip to content

Commit 12cdd59

Browse files
vkryachkorlazo
andauthored
Add better testing support for TransportRuntime. (#3670)
* Add better testing support for TransportRuntime. * Add licenses * Fix lint errors. * Update transport/transport-runtime-testing/src/main/AndroidManifest.xml Co-authored-by: Rodrigo Lazo <[email protected]> * Update transport/transport-runtime-testing/transport-runtime-testing.gradle Co-authored-by: Rodrigo Lazo <[email protected]> Co-authored-by: Rodrigo Lazo <[email protected]>
1 parent 14f26b8 commit 12cdd59

File tree

8 files changed

+140
-7
lines changed

8 files changed

+140
-7
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 2022 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,34 @@
1+
// Copyright 2022 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+
package com.google.android.datatransport.runtime;
16+
17+
import com.google.android.datatransport.Priority;
18+
import com.google.android.datatransport.runtime.backends.BackendResponse;
19+
import com.google.android.datatransport.runtime.scheduling.jobscheduling.UploadTestSupport;
20+
21+
/** Test support for {@link TransportRuntime}. */
22+
public final class TransportRuntimeTesting {
23+
private TransportRuntimeTesting() {}
24+
25+
/** Synchronously force upload all scheduled events for a given destination and priority. */
26+
public static BackendResponse forceUpload(Destination destination, Priority priority) {
27+
return UploadTestSupport.forceUpload(
28+
TransportContext.builder()
29+
.setBackendName(destination.getName())
30+
.setExtras(destination.getExtras())
31+
.setPriority(priority)
32+
.build());
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2022 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+
package com.google.android.datatransport.runtime.scheduling.jobscheduling;
16+
17+
import com.google.android.datatransport.runtime.TransportContext;
18+
import com.google.android.datatransport.runtime.TransportRuntime;
19+
import com.google.android.datatransport.runtime.backends.BackendResponse;
20+
21+
/**
22+
* Support class that avoids making {@link Uploader#logAndUpdateState(TransportContext, int) }
23+
* method public in the SDK code.
24+
*/
25+
public class UploadTestSupport {
26+
private UploadTestSupport() {}
27+
28+
public static BackendResponse forceUpload(TransportContext context) {
29+
return TransportRuntime.getInstance().getUploader().logAndUpdateState(context, 1);
30+
}
31+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright 2022 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/TransportContext.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public static Builder builder() {
7272
*
7373
* @hide
7474
*/
75-
@RestrictTo(RestrictTo.Scope.LIBRARY)
75+
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
7676
public TransportContext withPriority(Priority priority) {
7777
return builder()
7878
.setBackendName(getBackendName())
@@ -89,7 +89,7 @@ public abstract static class Builder {
8989
public abstract Builder setExtras(@Nullable byte[] extras);
9090

9191
/** @hide */
92-
@RestrictTo(RestrictTo.Scope.LIBRARY)
92+
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
9393
public abstract Builder setPriority(Priority priority);
9494

9595
public abstract TransportContext build();

transport/transport-runtime/src/main/java/com/google/android/datatransport/runtime/TransportRuntime.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ private static Set<Encoding> getSupportedEncodings(Destination destination) {
141141
return Collections.singleton(Encoding.of("proto"));
142142
}
143143

144-
@RestrictTo(RestrictTo.Scope.LIBRARY)
144+
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
145145
public Uploader getUploader() {
146146
return uploader;
147147
}

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)