OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.net; | 5 package org.chromium.net; |
6 | 6 |
7 import static junit.framework.Assert.assertNotNull; | 7 import static junit.framework.Assert.assertNotNull; |
8 import static junit.framework.Assert.assertNull; | 8 import static junit.framework.Assert.assertNull; |
9 import static junit.framework.Assert.assertTrue; | |
9 | 10 |
10 import android.os.ConditionVariable; | 11 import android.os.ConditionVariable; |
11 | 12 |
13 import java.util.Date; | |
12 import java.util.LinkedList; | 14 import java.util.LinkedList; |
13 import java.util.NoSuchElementException; | 15 import java.util.NoSuchElementException; |
14 import java.util.concurrent.Executor; | 16 import java.util.concurrent.Executor; |
15 import java.util.concurrent.Executors; | 17 import java.util.concurrent.Executors; |
16 | 18 |
17 /** | 19 /** |
18 * Classes which are useful for testing Cronet's metrics implementation and are needed in more than | 20 * Classes which are useful for testing Cronet's metrics implementation and are needed in more than |
19 * one test file. | 21 * one test file. |
20 */ | 22 */ |
21 public class MetricsTestUtil { | 23 public class MetricsTestUtil { |
22 /** | 24 /** |
23 * Executor which runs tasks only when told to with runAllTasks(). | 25 * Executor which runs tasks only when told to with runAllTasks(). |
xunjieli
2016/10/04 22:29:23
minor style nit: I didn't notice it in the other C
mgersh
2016/10/05 16:55:18
Yeah, I was wondering how/why that happened. Done.
| |
24 */ | 26 */ |
25 public static class TestExecutor implements Executor { | 27 public static class TestExecutor implements Executor { |
26 private final LinkedList<Runnable> mTaskQueue = new LinkedList<Runnable> (); | 28 private final LinkedList<Runnable> mTaskQueue = new LinkedList<Runnable> (); |
27 | 29 |
28 @Override | 30 @Override |
29 public void execute(Runnable task) { | 31 public void execute(Runnable task) { |
30 mTaskQueue.add(task); | 32 mTaskQueue.add(task); |
31 } | 33 } |
32 | 34 |
33 public void runAllTasks() { | 35 public void runAllTasks() { |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
76 mRequestInfo = requestInfo; | 78 mRequestInfo = requestInfo; |
77 mNumExpectedRequests--; | 79 mNumExpectedRequests--; |
78 if (mNumExpectedRequests == 0) { | 80 if (mNumExpectedRequests == 0) { |
79 mBlock.open(); | 81 mBlock.open(); |
80 } | 82 } |
81 } | 83 } |
82 | 84 |
83 public void blockUntilDone() { | 85 public void blockUntilDone() { |
84 mBlock.block(); | 86 mBlock.block(); |
85 } | 87 } |
88 | |
89 public void reset() { | |
90 mBlock.close(); | |
91 mNumExpectedRequests = 1; | |
92 mRequestInfo = null; | |
93 } | |
94 } | |
95 | |
96 /** | |
97 * Check existence of all the timing metrics that apply to most test request s, | |
98 * except those that come from net::LoadTimingInfo::ConnectTiming. | |
99 * Also check some timing differences, focusing on things we can't check wit h asserts in the | |
100 * CronetMetrics constructor. | |
101 * Don't check push times here. | |
102 */ | |
103 public static void checkTimingMetrics( | |
104 RequestFinishedInfo.Metrics metrics, Date startTime, Date endTime) { | |
105 assertNotNull(metrics.getRequestStart()); | |
106 assertTrue(metrics.getRequestStart().after(startTime)); | |
107 assertNotNull(metrics.getSendingStart()); | |
108 assertTrue(metrics.getSendingStart().after(startTime)); | |
109 assertNotNull(metrics.getSendingEnd()); | |
110 assertTrue(metrics.getSendingEnd().before(endTime)); | |
111 assertNotNull(metrics.getResponseStart()); | |
112 assertTrue(metrics.getResponseStart().after(startTime)); | |
113 assertNotNull(metrics.getResponseEnd()); | |
114 assertTrue(metrics.getResponseEnd().before(endTime) | |
115 || metrics.getResponseEnd().equals(endTime)); | |
116 // Entire request should take more than 0 ms | |
117 assertTrue(metrics.getResponseEnd().getTime() - metrics.getRequestStart( ).getTime() > 0); | |
118 } | |
119 | |
120 /** | |
121 * Check that the timing metrics which come from net::LoadTimingInfo::Connec tTiming exist, | |
122 * except SSL times in the case of non-https requests. | |
123 */ | |
124 public static void checkHasConnectTiming( | |
125 RequestFinishedInfo.Metrics metrics, Date startTime, Date endTime, b oolean isSsl) { | |
126 assertNotNull(metrics.getDnsStart()); | |
127 assertTrue(metrics.getDnsStart().after(startTime)); | |
128 assertNotNull(metrics.getDnsEnd()); | |
129 assertTrue(metrics.getDnsEnd().before(endTime)); | |
130 assertNotNull(metrics.getConnectStart()); | |
131 assertTrue(metrics.getConnectStart().after(startTime)); | |
132 assertNotNull(metrics.getConnectEnd()); | |
133 assertTrue(metrics.getConnectEnd().before(endTime)); | |
134 if (isSsl) { | |
135 assertNotNull(metrics.getSslStart()); | |
136 assertTrue(metrics.getSslStart().after(startTime)); | |
137 assertNotNull(metrics.getSslEnd()); | |
138 assertTrue(metrics.getSslEnd().before(endTime)); | |
139 } else { | |
140 assertNull(metrics.getSslStart()); | |
141 assertNull(metrics.getSslEnd()); | |
142 } | |
143 } | |
144 | |
145 /** | |
146 * Check that the timing metrics from net::LoadTimingInfo::ConnectTiming don 't exist. | |
147 */ | |
148 public static void checkNoConnectTiming(RequestFinishedInfo.Metrics metrics) { | |
149 assertNull(metrics.getDnsStart()); | |
150 assertNull(metrics.getDnsEnd()); | |
151 assertNull(metrics.getSslStart()); | |
152 assertNull(metrics.getSslEnd()); | |
153 assertNull(metrics.getConnectStart()); | |
154 assertNull(metrics.getConnectEnd()); | |
86 } | 155 } |
87 } | 156 } |
OLD | NEW |