Search in sources :

Example 1 with TransportManager

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());
}
Also used : Call(okhttp3.Call) ApplicationProcessState(com.google.firebase.perf.v1.ApplicationProcessState) NetworkRequestMetric(com.google.firebase.perf.v1.NetworkRequestMetric) Request(okhttp3.Request) HttpUrl(okhttp3.HttpUrl) ResponseBody(okhttp3.ResponseBody) Response(okhttp3.Response) Callback(okhttp3.Callback) TransportManager(com.google.firebase.perf.transport.TransportManager) RequestBody(okhttp3.RequestBody) Test(org.junit.Test)

Example 2 with TransportManager

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());
}
Also used : Call(okhttp3.Call) ApplicationProcessState(com.google.firebase.perf.v1.ApplicationProcessState) NetworkRequestMetric(com.google.firebase.perf.v1.NetworkRequestMetric) Request(okhttp3.Request) IOException(java.io.IOException) HttpUrl(okhttp3.HttpUrl) Callback(okhttp3.Callback) TransportManager(com.google.firebase.perf.transport.TransportManager) RequestBody(okhttp3.RequestBody) Test(org.junit.Test)

Example 3 with TransportManager

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();
}
Also used : Timer(com.google.firebase.perf.util.Timer) TraceMetric(com.google.firebase.perf.v1.TraceMetric) InvocationOnMock(org.mockito.invocation.InvocationOnMock) TransportManager(com.google.firebase.perf.transport.TransportManager) Before(org.junit.Before)

Example 4 with TransportManager

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());
}
Also used : ApplicationProcessState(com.google.firebase.perf.v1.ApplicationProcessState) GaugeManager(com.google.firebase.perf.session.gauges.GaugeManager) TransportManager(com.google.firebase.perf.transport.TransportManager) NetworkRequestMetricBuilder(com.google.firebase.perf.metrics.NetworkRequestMetricBuilder) Test(org.junit.Test)

Example 5 with TransportManager

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();
}
Also used : Trace(com.google.firebase.perf.metrics.Trace) TraceMetric(com.google.firebase.perf.v1.TraceMetric) ApplicationProcessState(com.google.firebase.perf.v1.ApplicationProcessState) AppStateMonitor(com.google.firebase.perf.application.AppStateMonitor) TransportManager(com.google.firebase.perf.transport.TransportManager)

Aggregations

TransportManager (com.google.firebase.perf.transport.TransportManager)6 ApplicationProcessState (com.google.firebase.perf.v1.ApplicationProcessState)5 Test (org.junit.Test)4 NetworkRequestMetric (com.google.firebase.perf.v1.NetworkRequestMetric)3 NetworkRequestMetricBuilder (com.google.firebase.perf.metrics.NetworkRequestMetricBuilder)2 TraceMetric (com.google.firebase.perf.v1.TraceMetric)2 Call (okhttp3.Call)2 Callback (okhttp3.Callback)2 HttpUrl (okhttp3.HttpUrl)2 Request (okhttp3.Request)2 RequestBody (okhttp3.RequestBody)2 AppStateMonitor (com.google.firebase.perf.application.AppStateMonitor)1 Trace (com.google.firebase.perf.metrics.Trace)1 GaugeManager (com.google.firebase.perf.session.gauges.GaugeManager)1 Timer (com.google.firebase.perf.util.Timer)1 IOException (java.io.IOException)1 Response (okhttp3.Response)1 ResponseBody (okhttp3.ResponseBody)1 HttpResponse (org.apache.http.HttpResponse)1 ResponseHandler (org.apache.http.client.ResponseHandler)1