Skip to content

Future-proof TransportBackend. #318

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.android.datatransport.cct.proto.LogRequest;
import com.google.android.datatransport.cct.proto.LogResponse;
import com.google.android.datatransport.cct.proto.QosTierConfiguration;
import com.google.android.datatransport.runtime.BackendRequest;
import com.google.android.datatransport.runtime.BackendResponse;
import com.google.android.datatransport.runtime.BackendResponse.Status;
import com.google.android.datatransport.runtime.EventInternal;
Expand Down Expand Up @@ -105,9 +106,9 @@ public EventInternal decorate(EventInternal eventInternal) {
.build();
}

private BatchedLogRequest getRequestBody(Iterable<EventInternal> eventInternals) {
private BatchedLogRequest getRequestBody(BackendRequest backendRequest) {
HashMap<String, List<EventInternal>> eventInternalMap = new HashMap<>();
for (EventInternal eventInternal : eventInternals) {
for (EventInternal eventInternal : backendRequest.getEvents()) {
String key = eventInternal.getTransportName();
if (!eventInternalMap.containsKey(key)) {
List<EventInternal> eventInternalList = new ArrayList<EventInternal>();
Expand Down Expand Up @@ -192,8 +193,8 @@ private BackendResponse doSend(BatchedLogRequest requestBody) throws IOException
}

@Override
public BackendResponse send(Iterable<EventInternal> events) {
BatchedLogRequest requestBody = getRequestBody(events);
public BackendResponse send(BackendRequest request) {
BatchedLogRequest requestBody = getRequestBody(request);
try {
return doSend(requestBody);
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import com.google.android.datatransport.Priority;
import com.google.android.datatransport.cct.proto.LogResponse;
import com.google.android.datatransport.runtime.BackendRequest;
import com.google.android.datatransport.runtime.BackendResponse;
import com.google.android.datatransport.runtime.BackendResponse.Status;
import com.google.android.datatransport.runtime.EventInternal;
Expand All @@ -45,16 +46,17 @@ public class GoogleTransportBackendTest {

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

private Iterable<EventInternal> getEventInternalIterable() {
return Collections.singleton(
BACKEND.decorate(
EventInternal.builder()
.setEventMillis(3)
.setUptimeMillis(1)
.setTransportName(TRANSPORT_NAME)
.setPriority(Priority.DEFAULT)
.setPayload("TelemetryData".getBytes())
.build()));
private BackendRequest getBackendRequest() {
return BackendRequest.create(
Collections.singleton(
BACKEND.decorate(
EventInternal.builder()
.setEventMillis(3)
.setUptimeMillis(1)
.setTransportName(TRANSPORT_NAME)
.setPriority(Priority.DEFAULT)
.setPayload("TelemetryData".getBytes())
.build())));
}

@Test
Expand All @@ -70,7 +72,7 @@ public void testSuccessLoggingRequest() {
.setNextRequestWaitMillis(3)
.build()
.toByteArray())));
BackendResponse response = BACKEND.send(getEventInternalIterable());
BackendResponse response = BACKEND.send(getBackendRequest());
verify(
postRequestedFor(urlEqualTo("/api"))
.withHeader("Content-Type", equalTo("application/x-protobuf")));
Expand All @@ -81,7 +83,7 @@ public void testSuccessLoggingRequest() {
@Test
public void testUnsuccessfulLoggingRequest() {
stubFor(post(urlEqualTo("/api")).willReturn(aResponse().withStatus(404)));
BackendResponse response = BACKEND.send(getEventInternalIterable());
BackendResponse response = BACKEND.send(getBackendRequest());
verify(
postRequestedFor(urlEqualTo("/api"))
.withHeader("Content-Type", equalTo("application/x-protobuf")));
Expand All @@ -92,7 +94,7 @@ public void testUnsuccessfulLoggingRequest() {
@Test
public void testServerErrorLoggingRequest() {
stubFor(post(urlEqualTo("/api")).willReturn(aResponse().withStatus(500)));
BackendResponse response = BACKEND.send(getEventInternalIterable());
BackendResponse response = BACKEND.send(getBackendRequest());
verify(
postRequestedFor(urlEqualTo("/api"))
.withHeader("Content-Type", equalTo("application/x-protobuf")));
Expand All @@ -109,7 +111,7 @@ public void testGarbageFromServer() {
.withStatus(200)
.withHeader("Content-Type", "application/x-protobuf;charset=UTF8;hello=world")
.withBody("{\"status\":\"Error\",\"message\":\"Endpoint not found\"}")));
BackendResponse response = BACKEND.send(getEventInternalIterable());
BackendResponse response = BACKEND.send(getBackendRequest());
verify(
postRequestedFor(urlEqualTo("/api"))
.withHeader("Content-Type", equalTo("application/x-protobuf")));
Expand All @@ -120,7 +122,7 @@ public void testGarbageFromServer() {
@Test
public void testNonHandledResponseCode() {
stubFor(post(urlEqualTo("/api")).willReturn(aResponse().withStatus(300)));
BackendResponse response = BACKEND.send(getEventInternalIterable());
BackendResponse response = BACKEND.send(getBackendRequest());
verify(
postRequestedFor(urlEqualTo("/api"))
.withHeader("Content-Type", equalTo("application/x-protobuf")));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.android.datatransport.runtime;

import com.google.auto.value.AutoValue;

/** Encapsulates a send request made to an individual {@link TransportBackend}. */
@AutoValue
public abstract class BackendRequest {
/** Events to be sent to the backend. */
public abstract Iterable<EventInternal> getEvents();

/** Creates a new instance of the request. */
public static BackendRequest create(Iterable<EventInternal> events) {
return new AutoValue_BackendRequest(events);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
public interface TransportBackend {
EventInternal decorate(EventInternal event);

BackendResponse send(Iterable<EventInternal> event);
BackendResponse send(BackendRequest backendRequest);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
import com.google.android.datatransport.Transport;
import com.google.android.datatransport.TransportFactory;

public final class TransportFactoryImpl implements TransportFactory {
private String backendName;
final class TransportFactoryImpl implements TransportFactory {
private final String backendName;
private final TransportInternal transportInternal;

TransportFactoryImpl(String backendName, TransportInternal transportInternal) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
import com.google.android.datatransport.Transport;

class TransportImpl<T> implements Transport<T> {
private String backendName;
private String name;
private Transformer<T, byte[]> transformer;
private final String backendName;
private final String name;
private final Transformer<T, byte[]> transformer;
private final TransportInternal transportInternal;

TransportImpl(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package com.google.android.datatransport.runtime.scheduling;

import com.google.android.datatransport.runtime.BackendRegistry;
import com.google.android.datatransport.runtime.BackendRequest;
import com.google.android.datatransport.runtime.EventInternal;
import com.google.android.datatransport.runtime.TransportBackend;
import com.google.android.datatransport.runtime.TransportRuntime;
Expand Down Expand Up @@ -49,7 +50,7 @@ public void schedule(String backendName, EventInternal event) {
LOGGER.warning(String.format("Transport backend '%s' is not registered", backendName));
return;
}
backend.send(Collections.singleton(backend.decorate(event)));
backend.send(BackendRequest.create(Collections.singleton(backend.decorate(event))));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ public <T> T runCriticalSection(
verify(mockBackend, times(1))
.send(
eq(
Collections.singleton(
expectedEvent.toBuilder().addMetadata(TEST_KEY, TEST_VALUE).build())));
BackendRequest.create(
Collections.singleton(
expectedEvent.toBuilder().addMetadata(TEST_KEY, TEST_VALUE).build()))));
}
}