• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 package org.chromium.net;
6 
7 import static com.google.common.truth.Truth.assertThat;
8 import static com.google.common.truth.Truth.assertWithMessage;
9 
10 import java.util.Date;
11 import java.util.LinkedList;
12 import java.util.NoSuchElementException;
13 import java.util.concurrent.Executor;
14 
15 /**
16  * Classes which are useful for testing Cronet's metrics implementation and are needed in more than
17  * one test file.
18  */
19 public class MetricsTestUtil {
20     /** Executor which runs tasks only when told to with runAllTasks(). */
21     public static class TestExecutor implements Executor {
22         private final LinkedList<Runnable> mTaskQueue = new LinkedList<Runnable>();
23 
24         @Override
execute(Runnable task)25         public void execute(Runnable task) {
26             mTaskQueue.add(task);
27         }
28 
runAllTasks()29         public void runAllTasks() {
30             try {
31                 while (mTaskQueue.size() > 0) {
32                     mTaskQueue.remove().run();
33                 }
34             } catch (NoSuchElementException e) {
35                 throw new RuntimeException("Task was removed during iteration", e);
36             }
37         }
38     }
39 
40     // Helper method to assert date1 is equals to or after date2.
41     // Some implementation of java.util.Date broke the symmetric property, so
42     // check both directions.
assertAfter(Date date1, Date date2)43     public static void assertAfter(Date date1, Date date2) {
44         assertWithMessage("date1: " + date1.getTime() + ", date2: " + date2.getTime())
45                 .that(date1.after(date2) || date1.equals(date2) || date2.equals(date1))
46                 .isTrue();
47     }
48 
49     /**
50      * Check existence of all the timing metrics that apply to most test requests,
51      * except those that come from net::LoadTimingInfo::ConnectTiming.
52      * Also check some timing differences, focusing on things we can't check with asserts in the
53      * CronetMetrics constructor.
54      * Don't check push times here.
55      */
checkTimingMetrics( RequestFinishedInfo.Metrics metrics, Date startTime, Date endTime)56     public static void checkTimingMetrics(
57             RequestFinishedInfo.Metrics metrics, Date startTime, Date endTime) {
58         assertThat(metrics.getRequestStart()).isNotNull();
59         assertAfter(metrics.getRequestStart(), startTime);
60         assertThat(metrics.getSendingStart()).isNotNull();
61         assertAfter(metrics.getSendingStart(), startTime);
62         assertThat(metrics.getSendingEnd()).isNotNull();
63         assertAfter(endTime, metrics.getSendingEnd());
64         assertThat(metrics.getResponseStart()).isNotNull();
65         assertAfter(metrics.getResponseStart(), startTime);
66         assertThat(metrics.getRequestEnd()).isNotNull();
67         assertAfter(endTime, metrics.getRequestEnd());
68         assertAfter(metrics.getRequestEnd(), metrics.getRequestStart());
69     }
70 
71     /**
72      * Check that the timing metrics which come from net::LoadTimingInfo::ConnectTiming exist,
73      * except SSL times in the case of non-https requests.
74      */
checkHasConnectTiming( RequestFinishedInfo.Metrics metrics, Date startTime, Date endTime, boolean isSsl)75     public static void checkHasConnectTiming(
76             RequestFinishedInfo.Metrics metrics, Date startTime, Date endTime, boolean isSsl) {
77         assertThat(metrics.getDnsStart()).isNotNull();
78         assertAfter(metrics.getDnsStart(), startTime);
79         assertThat(metrics.getDnsEnd()).isNotNull();
80         assertAfter(endTime, metrics.getDnsEnd());
81         assertThat(metrics.getConnectStart()).isNotNull();
82         assertAfter(metrics.getConnectStart(), startTime);
83         assertThat(metrics.getConnectEnd()).isNotNull();
84         assertAfter(endTime, metrics.getConnectEnd());
85         if (isSsl) {
86             assertThat(metrics.getSslStart()).isNotNull();
87             assertAfter(metrics.getSslStart(), startTime);
88             assertThat(metrics.getSslEnd()).isNotNull();
89             assertAfter(endTime, metrics.getSslEnd());
90         } else {
91             assertThat(metrics.getSslStart()).isNull();
92             assertThat(metrics.getSslEnd()).isNull();
93         }
94     }
95 
96     /** Check that the timing metrics from net::LoadTimingInfo::ConnectTiming don't exist. */
checkNoConnectTiming(RequestFinishedInfo.Metrics metrics)97     public static void checkNoConnectTiming(RequestFinishedInfo.Metrics metrics) {
98         assertThat(metrics.getDnsStart()).isNull();
99         assertThat(metrics.getDnsEnd()).isNull();
100         assertThat(metrics.getSslStart()).isNull();
101         assertThat(metrics.getSslEnd()).isNull();
102         assertThat(metrics.getConnectStart()).isNull();
103         assertThat(metrics.getConnectEnd()).isNull();
104     }
105 
106     /** Check that RequestFinishedInfo looks the way it should look for a normal successful request. */
checkRequestFinishedInfo( RequestFinishedInfo info, String url, Date startTime, Date endTime)107     public static void checkRequestFinishedInfo(
108             RequestFinishedInfo info, String url, Date startTime, Date endTime) {
109         assertWithMessage("RequestFinishedInfo.Listener must be called").that(info).isNotNull();
110         assertThat(info.getUrl()).isEqualTo(url);
111         assertThat(info.getResponseInfo()).isNotNull();
112         assertThat(info.getException()).isNull();
113         RequestFinishedInfo.Metrics metrics = info.getMetrics();
114         assertWithMessage("RequestFinishedInfo.getMetrics() must not be null")
115                 .that(metrics)
116                 .isNotNull();
117         // Check old (deprecated) timing metrics
118         assertThat(metrics.getTotalTimeMs()).isAtLeast(0L);
119         assertThat(metrics.getTotalTimeMs()).isAtLeast(metrics.getTtfbMs());
120         // Check new timing metrics
121         checkTimingMetrics(metrics, startTime, endTime);
122         assertThat(metrics.getPushStart()).isNull();
123         assertThat(metrics.getPushEnd()).isNull();
124         // Check data use metrics
125         assertThat(metrics.getSentByteCount()).isGreaterThan(0L);
126         assertThat(metrics.getReceivedByteCount()).isGreaterThan(0L);
127     }
128 }
129