Skip to content

Commit c6481e8

Browse files
authored
Future-proof TransportBackend. (#318)
The PR changes the signature of `send()` to take `BackendRequest` as parameter. This allows us to evolve the API in a backwards compatible way in the furure.
1 parent 1dea553 commit c6481e8

File tree

8 files changed

+62
-28
lines changed

8 files changed

+62
-28
lines changed

transport/transport-backend-cct/src/main/java/com/google/android/datatransport/cct/GoogleTransportBackend.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.google.android.datatransport.cct.proto.LogRequest;
2424
import com.google.android.datatransport.cct.proto.LogResponse;
2525
import com.google.android.datatransport.cct.proto.QosTierConfiguration;
26+
import com.google.android.datatransport.runtime.BackendRequest;
2627
import com.google.android.datatransport.runtime.BackendResponse;
2728
import com.google.android.datatransport.runtime.BackendResponse.Status;
2829
import com.google.android.datatransport.runtime.EventInternal;
@@ -105,9 +106,9 @@ public EventInternal decorate(EventInternal eventInternal) {
105106
.build();
106107
}
107108

108-
private BatchedLogRequest getRequestBody(Iterable<EventInternal> eventInternals) {
109+
private BatchedLogRequest getRequestBody(BackendRequest backendRequest) {
109110
HashMap<String, List<EventInternal>> eventInternalMap = new HashMap<>();
110-
for (EventInternal eventInternal : eventInternals) {
111+
for (EventInternal eventInternal : backendRequest.getEvents()) {
111112
String key = eventInternal.getTransportName();
112113
if (!eventInternalMap.containsKey(key)) {
113114
List<EventInternal> eventInternalList = new ArrayList<EventInternal>();
@@ -192,8 +193,8 @@ private BackendResponse doSend(BatchedLogRequest requestBody) throws IOException
192193
}
193194

194195
@Override
195-
public BackendResponse send(Iterable<EventInternal> events) {
196-
BatchedLogRequest requestBody = getRequestBody(events);
196+
public BackendResponse send(BackendRequest request) {
197+
BatchedLogRequest requestBody = getRequestBody(request);
197198
try {
198199
return doSend(requestBody);
199200
} catch (IOException e) {

transport/transport-backend-cct/src/test/java/com/google/android/datatransport/cct/GoogleTransportBackendTest.java

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.github.tomakehurst.wiremock.junit.WireMockRule;
2727
import com.google.android.datatransport.Priority;
2828
import com.google.android.datatransport.cct.proto.LogResponse;
29+
import com.google.android.datatransport.runtime.BackendRequest;
2930
import com.google.android.datatransport.runtime.BackendResponse;
3031
import com.google.android.datatransport.runtime.BackendResponse.Status;
3132
import com.google.android.datatransport.runtime.EventInternal;
@@ -45,16 +46,17 @@ public class GoogleTransportBackendTest {
4546

4647
@Rule public WireMockRule wireMockRule = new WireMockRule(8999);
4748

48-
private Iterable<EventInternal> getEventInternalIterable() {
49-
return Collections.singleton(
50-
BACKEND.decorate(
51-
EventInternal.builder()
52-
.setEventMillis(3)
53-
.setUptimeMillis(1)
54-
.setTransportName(TRANSPORT_NAME)
55-
.setPriority(Priority.DEFAULT)
56-
.setPayload("TelemetryData".getBytes())
57-
.build()));
49+
private BackendRequest getBackendRequest() {
50+
return BackendRequest.create(
51+
Collections.singleton(
52+
BACKEND.decorate(
53+
EventInternal.builder()
54+
.setEventMillis(3)
55+
.setUptimeMillis(1)
56+
.setTransportName(TRANSPORT_NAME)
57+
.setPriority(Priority.DEFAULT)
58+
.setPayload("TelemetryData".getBytes())
59+
.build())));
5860
}
5961

6062
@Test
@@ -70,7 +72,7 @@ public void testSuccessLoggingRequest() {
7072
.setNextRequestWaitMillis(3)
7173
.build()
7274
.toByteArray())));
73-
BackendResponse response = BACKEND.send(getEventInternalIterable());
75+
BackendResponse response = BACKEND.send(getBackendRequest());
7476
verify(
7577
postRequestedFor(urlEqualTo("/api"))
7678
.withHeader("Content-Type", equalTo("application/x-protobuf")));
@@ -81,7 +83,7 @@ public void testSuccessLoggingRequest() {
8183
@Test
8284
public void testUnsuccessfulLoggingRequest() {
8385
stubFor(post(urlEqualTo("/api")).willReturn(aResponse().withStatus(404)));
84-
BackendResponse response = BACKEND.send(getEventInternalIterable());
86+
BackendResponse response = BACKEND.send(getBackendRequest());
8587
verify(
8688
postRequestedFor(urlEqualTo("/api"))
8789
.withHeader("Content-Type", equalTo("application/x-protobuf")));
@@ -92,7 +94,7 @@ public void testUnsuccessfulLoggingRequest() {
9294
@Test
9395
public void testServerErrorLoggingRequest() {
9496
stubFor(post(urlEqualTo("/api")).willReturn(aResponse().withStatus(500)));
95-
BackendResponse response = BACKEND.send(getEventInternalIterable());
97+
BackendResponse response = BACKEND.send(getBackendRequest());
9698
verify(
9799
postRequestedFor(urlEqualTo("/api"))
98100
.withHeader("Content-Type", equalTo("application/x-protobuf")));
@@ -109,7 +111,7 @@ public void testGarbageFromServer() {
109111
.withStatus(200)
110112
.withHeader("Content-Type", "application/x-protobuf;charset=UTF8;hello=world")
111113
.withBody("{\"status\":\"Error\",\"message\":\"Endpoint not found\"}")));
112-
BackendResponse response = BACKEND.send(getEventInternalIterable());
114+
BackendResponse response = BACKEND.send(getBackendRequest());
113115
verify(
114116
postRequestedFor(urlEqualTo("/api"))
115117
.withHeader("Content-Type", equalTo("application/x-protobuf")));
@@ -120,7 +122,7 @@ public void testGarbageFromServer() {
120122
@Test
121123
public void testNonHandledResponseCode() {
122124
stubFor(post(urlEqualTo("/api")).willReturn(aResponse().withStatus(300)));
123-
BackendResponse response = BACKEND.send(getEventInternalIterable());
125+
BackendResponse response = BACKEND.send(getBackendRequest());
124126
verify(
125127
postRequestedFor(urlEqualTo("/api"))
126128
.withHeader("Content-Type", equalTo("application/x-protobuf")));
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
package com.google.android.datatransport.runtime;
16+
17+
import com.google.auto.value.AutoValue;
18+
19+
/** Encapsulates a send request made to an individual {@link TransportBackend}. */
20+
@AutoValue
21+
public abstract class BackendRequest {
22+
/** Events to be sent to the backend. */
23+
public abstract Iterable<EventInternal> getEvents();
24+
25+
/** Creates a new instance of the request. */
26+
public static BackendRequest create(Iterable<EventInternal> events) {
27+
return new AutoValue_BackendRequest(events);
28+
}
29+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@
1717
public interface TransportBackend {
1818
EventInternal decorate(EventInternal event);
1919

20-
BackendResponse send(Iterable<EventInternal> event);
20+
BackendResponse send(BackendRequest backendRequest);
2121
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
import com.google.android.datatransport.Transport;
1919
import com.google.android.datatransport.TransportFactory;
2020

21-
public final class TransportFactoryImpl implements TransportFactory {
22-
private String backendName;
21+
final class TransportFactoryImpl implements TransportFactory {
22+
private final String backendName;
2323
private final TransportInternal transportInternal;
2424

2525
TransportFactoryImpl(String backendName, TransportInternal transportInternal) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
import com.google.android.datatransport.Transport;
2020

2121
class TransportImpl<T> implements Transport<T> {
22-
private String backendName;
23-
private String name;
24-
private Transformer<T, byte[]> transformer;
22+
private final String backendName;
23+
private final String name;
24+
private final Transformer<T, byte[]> transformer;
2525
private final TransportInternal transportInternal;
2626

2727
TransportImpl(

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package com.google.android.datatransport.runtime.scheduling;
1616

1717
import com.google.android.datatransport.runtime.BackendRegistry;
18+
import com.google.android.datatransport.runtime.BackendRequest;
1819
import com.google.android.datatransport.runtime.EventInternal;
1920
import com.google.android.datatransport.runtime.TransportBackend;
2021
import com.google.android.datatransport.runtime.TransportRuntime;
@@ -49,7 +50,7 @@ public void schedule(String backendName, EventInternal event) {
4950
LOGGER.warning(String.format("Transport backend '%s' is not registered", backendName));
5051
return;
5152
}
52-
backend.send(Collections.singleton(backend.decorate(event)));
53+
backend.send(BackendRequest.create(Collections.singleton(backend.decorate(event))));
5354
});
5455
}
5556
}

transport/transport-runtime/src/test/java/com/google/android/datatransport/runtime/TransportRuntimeTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ public <T> T runCriticalSection(
109109
verify(mockBackend, times(1))
110110
.send(
111111
eq(
112-
Collections.singleton(
113-
expectedEvent.toBuilder().addMetadata(TEST_KEY, TEST_VALUE).build())));
112+
BackendRequest.create(
113+
Collections.singleton(
114+
expectedEvent.toBuilder().addMetadata(TEST_KEY, TEST_VALUE).build()))));
114115
}
115116
}

0 commit comments

Comments
 (0)