use of com.google.firebase.perf.transport.TransportManager in project firebase-android-sdk by firebase.
the class InstrumentOkHttpEnqueueCallbackTest method testOnResponse.
@Test
public void testOnResponse() throws IOException {
String urlStr = "www.google.com";
String scheme = "https";
String fullUrlStr = "https://www.google.com/";
String method = "POST";
long requestContentLength = 240;
long responseContentLength = 353;
int responseCode = 300;
String message = "";
long startTimeMicros = 1;
HttpUrl url = new HttpUrl.Builder().scheme(scheme).host(urlStr).build();
RequestBody requestBody = mock(RequestBody.class);
when(requestBody.contentLength()).thenReturn(requestContentLength);
Request request = new Request.Builder().url(url).method(method, requestBody).build();
ResponseBody responseBody = mock(ResponseBody.class);
when(responseBody.contentLength()).thenReturn(responseContentLength);
Response response = new Response.Builder().code(responseCode).message(message).body(responseBody).request(request).protocol(Protocol.HTTP_2).build();
TransportManager transportManager = mock(TransportManager.class);
Callback callback = mock(Callback.class);
Call call = mock(Call.class);
InstrumentOkHttpEnqueueCallback enqueueCallback = new InstrumentOkHttpEnqueueCallback(callback, transportManager, mockTimer(), startTimeMicros);
enqueueCallback.onResponse(call, response);
ArgumentCaptor<NetworkRequestMetric> argument = ArgumentCaptor.forClass(NetworkRequestMetric.class);
verify(transportManager).log(argument.capture(), ArgumentMatchers.any(ApplicationProcessState.class));
verify(callback).onResponse(call, response);
NetworkRequestMetric metric = argument.getValue();
assertEquals(fullUrlStr, metric.getUrl());
assertEquals(HttpMethod.POST, metric.getHttpMethod());
assertEquals(requestContentLength, metric.getRequestPayloadBytes());
assertEquals(responseContentLength, metric.getResponsePayloadBytes());
assertEquals(responseCode, metric.getHttpResponseCode());
assertEquals(startTimeMicros, metric.getClientStartTimeUs());
assertEquals(2000, metric.getTimeToResponseCompletedUs());
}
use of com.google.firebase.perf.transport.TransportManager in project firebase-android-sdk by firebase.
the class InstrumentOkHttpEnqueueCallbackTest method testOnFailure.
@Test
public void testOnFailure() throws IOException {
String urlStr = "www.google.com";
String scheme = "https";
String fullUrlStr = "https://www.google.com/";
String method = "POST";
HttpUrl url = new HttpUrl.Builder().scheme(scheme).host(urlStr).build();
RequestBody requestBody = mock(RequestBody.class);
TransportManager transportManager = mock(TransportManager.class);
Callback callback = mock(Callback.class);
Call call = mock(Call.class);
Request request = new Request.Builder().url(url).method(method, requestBody).build();
when(call.request()).thenReturn(request);
IOException e = new IOException();
long startTime = 1;
InstrumentOkHttpEnqueueCallback enqueueCallback = new InstrumentOkHttpEnqueueCallback(callback, transportManager, mockTimer(), startTime);
enqueueCallback.onFailure(call, e);
ArgumentCaptor<NetworkRequestMetric> argument = ArgumentCaptor.forClass(NetworkRequestMetric.class);
verify(transportManager).log(argument.capture(), ArgumentMatchers.any(ApplicationProcessState.class));
verify(callback).onFailure(call, e);
NetworkRequestMetric metric = argument.getValue();
assertEquals(fullUrlStr, metric.getUrl());
assertEquals(HttpMethod.POST, metric.getHttpMethod());
assertEquals(NetworkClientErrorReason.GENERIC_CLIENT_ERROR, metric.getNetworkClientErrorReason());
assertEquals(startTime, metric.getClientStartTimeUs());
assertEquals(2000, metric.getTimeToResponseCompletedUs());
}
use of com.google.firebase.perf.transport.TransportManager in project firebase-android-sdk by firebase.
the class AppStartTraceTest method setUp.
@Before
public void setUp() {
initMocks(this);
doAnswer(new Answer<Timer>() {
@Override
public Timer answer(InvocationOnMock invocationOnMock) throws Throwable {
return new Timer(currentTime);
}
}).when(clock).getTime();
transportManager = mock(TransportManager.class);
traceArgumentCaptor = ArgumentCaptor.forClass(TraceMetric.class);
appStartTime = FirebasePerfProvider.getAppStartTime().getMicros();
appStartHRT = FirebasePerfProvider.getAppStartTime().getHighResTime();
}
use of com.google.firebase.perf.transport.TransportManager in project firebase-android-sdk by firebase.
the class AppStateMonitorTest method testAppStateCallbackWithNetworkRequestMetricBuilder.
@Test
public void testAppStateCallbackWithNetworkRequestMetricBuilder() {
AppStateMonitor monitor = new AppStateMonitor(transportManager, clock);
// registerForAppState() is called by NetworkRequestMetricBuilder's constructor.
NetworkRequestMetricBuilder builder = new NetworkRequestMetricBuilder(mock(TransportManager.class), monitor, mock(GaugeManager.class));
Assert.assertEquals(ApplicationProcessState.BACKGROUND, builder.getAppState());
// activity1 comes to foreground.
currentTime = 1;
monitor.onActivityResumed(activity1);
Assert.assertTrue(monitor.isForeground());
// builder is updated through AppStateCallback.
Assert.assertEquals(FOREGROUND_BACKGROUND, builder.getAppState());
verify(transportManager, times(0)).log(argTraceMetric.capture(), nullable(ApplicationProcessState.class));
// activity1 goes to background.
currentTime = 2;
monitor.onActivityStopped(activity1);
Assert.assertFalse(monitor.isForeground());
// Foreground session trace.
verify(transportManager, times(1)).log(argTraceMetric.capture(), eq(FOREGROUND_BACKGROUND));
// builder is updated again.
Assert.assertEquals(FOREGROUND_BACKGROUND, builder.getAppState());
// unregisterForAppState() is called by NetworkRequestMetricBuilder.build().
builder.build();
Assert.assertEquals(FOREGROUND_BACKGROUND, builder.getAppState());
}
use of com.google.firebase.perf.transport.TransportManager in project firebase-android-sdk by firebase.
the class FirebasePerfTraceValidatorTest method createValidTraceMetric.
private TraceMetric.Builder createValidTraceMetric() {
String traceName = "trace_1";
long expectedClientStartTime = 1;
long expectedTraceDuration = 50;
TransportManager transportManager = mock(TransportManager.class);
AppStateMonitor appStateMonitor = mock(AppStateMonitor.class);
ArgumentCaptor<TraceMetric> argMetric = ArgumentCaptor.forClass(TraceMetric.class);
Trace trace = new Trace(traceName, transportManager, clock, appStateMonitor);
currentTime = expectedClientStartTime;
trace.start();
currentTime += expectedTraceDuration;
trace.stop();
verify(transportManager).log(argMetric.capture(), ArgumentMatchers.nullable(ApplicationProcessState.class));
TraceMetric traceMetric = argMetric.getValue();
assertThat(traceMetric.getName()).isEqualTo(traceName);
assertThat(traceMetric.getClientStartTimeUs()).isEqualTo(expectedClientStartTime);
assertThat(traceMetric.getDurationUs()).isEqualTo(expectedTraceDuration);
return traceMetric.toBuilder();
}
Aggregations