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 import static junit.framework.Assert.assertTrue; |
10 | 10 |
(...skipping 29 matching lines...) Expand all Loading... |
40 } catch (NoSuchElementException e) { | 40 } catch (NoSuchElementException e) { |
41 throw new RuntimeException("Task was removed during iteration",
e); | 41 throw new RuntimeException("Task was removed during iteration",
e); |
42 } | 42 } |
43 } | 43 } |
44 } | 44 } |
45 | 45 |
46 /** | 46 /** |
47 * RequestFinishedInfo.Listener for testing, which saves the RequestFinished
Info | 47 * RequestFinishedInfo.Listener for testing, which saves the RequestFinished
Info |
48 */ | 48 */ |
49 public static class TestRequestFinishedListener extends RequestFinishedInfo.
Listener { | 49 public static class TestRequestFinishedListener extends RequestFinishedInfo.
Listener { |
| 50 private final ConditionVariable mBlock; |
50 private RequestFinishedInfo mRequestInfo; | 51 private RequestFinishedInfo mRequestInfo; |
51 private ConditionVariable mBlock; | |
52 private int mNumExpectedRequests = -1; | |
53 | 52 |
54 // TODO(mgersh): it's weird that you can use either this constructor or
blockUntilDone() but | 53 // TODO(mgersh): it's weird that you can use either this constructor or
blockUntilDone() but |
55 // not both. Either clean it up or document why it has to work this way. | 54 // not both. Either clean it up or document why it has to work this way. |
56 public TestRequestFinishedListener(Executor executor) { | 55 public TestRequestFinishedListener(Executor executor) { |
57 super(executor); | 56 super(executor); |
58 } | |
59 | |
60 public TestRequestFinishedListener(int numExpectedRequests) { | |
61 super(Executors.newSingleThreadExecutor()); | |
62 mNumExpectedRequests = numExpectedRequests; | |
63 mBlock = new ConditionVariable(); | 57 mBlock = new ConditionVariable(); |
64 } | 58 } |
65 | 59 |
66 public TestRequestFinishedListener() { | 60 public TestRequestFinishedListener() { |
67 super(Executors.newSingleThreadExecutor()); | 61 super(Executors.newSingleThreadExecutor()); |
68 mNumExpectedRequests = 1; | |
69 mBlock = new ConditionVariable(); | 62 mBlock = new ConditionVariable(); |
70 } | 63 } |
71 | 64 |
72 public RequestFinishedInfo getRequestInfo() { | 65 public RequestFinishedInfo getRequestInfo() { |
73 return mRequestInfo; | 66 return mRequestInfo; |
74 } | 67 } |
75 | 68 |
76 @Override | 69 @Override |
77 public void onRequestFinished(RequestFinishedInfo requestInfo) { | 70 public void onRequestFinished(RequestFinishedInfo requestInfo) { |
78 assertNull("onRequestFinished called repeatedly", mRequestInfo); | 71 assertNull("onRequestFinished called repeatedly", mRequestInfo); |
79 assertNotNull(requestInfo); | 72 assertNotNull(requestInfo); |
80 mRequestInfo = requestInfo; | 73 mRequestInfo = requestInfo; |
81 mNumExpectedRequests--; | 74 mBlock.open(); |
82 if (mNumExpectedRequests == 0) { | |
83 mBlock.open(); | |
84 } | |
85 } | 75 } |
86 | 76 |
87 public void blockUntilDone() { | 77 public void blockUntilDone() { |
88 mBlock.block(); | 78 mBlock.block(); |
89 } | 79 } |
90 | 80 |
91 public void reset() { | 81 public void reset() { |
92 mBlock.close(); | 82 mBlock.close(); |
93 mNumExpectedRequests = 1; | |
94 mRequestInfo = null; | 83 mRequestInfo = null; |
95 } | 84 } |
96 } | 85 } |
97 | 86 |
| 87 // Helper method to assert date2 is equals to or after date1. |
| 88 // Some implementation of java.util.Date broke the symmetric property, so |
| 89 // check both directions. |
| 90 public static void assertAfter(Date date1, Date date2) { |
| 91 assertTrue("date1: " + date1.getTime() + ", date2: " + date2.getTime(), |
| 92 date1.after(date2) || date1.equals(date2) || date2.equals(date1)
); |
| 93 } |
| 94 |
98 /** | 95 /** |
99 * Check existence of all the timing metrics that apply to most test request
s, | 96 * Check existence of all the timing metrics that apply to most test request
s, |
100 * except those that come from net::LoadTimingInfo::ConnectTiming. | 97 * except those that come from net::LoadTimingInfo::ConnectTiming. |
101 * Also check some timing differences, focusing on things we can't check wit
h asserts in the | 98 * Also check some timing differences, focusing on things we can't check wit
h asserts in the |
102 * CronetMetrics constructor. | 99 * CronetMetrics constructor. |
103 * Don't check push times here. | 100 * Don't check push times here. |
104 */ | 101 */ |
105 public static void checkTimingMetrics( | 102 public static void checkTimingMetrics( |
106 RequestFinishedInfo.Metrics metrics, Date startTime, Date endTime) { | 103 RequestFinishedInfo.Metrics metrics, Date startTime, Date endTime) { |
107 assertNotNull(metrics.getRequestStart()); | 104 assertNotNull(metrics.getRequestStart()); |
108 assertTrue(metrics.getRequestStart().after(startTime) | 105 assertAfter(metrics.getRequestStart(), startTime); |
109 || metrics.getRequestStart().equals(startTime)); | |
110 assertNotNull(metrics.getSendingStart()); | 106 assertNotNull(metrics.getSendingStart()); |
111 assertTrue(metrics.getSendingStart().after(startTime) | 107 assertAfter(metrics.getSendingStart(), startTime); |
112 || metrics.getSendingStart().equals(startTime)); | |
113 assertNotNull(metrics.getSendingEnd()); | 108 assertNotNull(metrics.getSendingEnd()); |
114 assertTrue(metrics.getSendingEnd().before(endTime)); | 109 assertTrue(metrics.getSendingEnd().before(endTime)); |
115 assertNotNull(metrics.getResponseStart()); | 110 assertNotNull(metrics.getResponseStart()); |
116 assertTrue(metrics.getResponseStart().after(startTime)); | 111 assertTrue(metrics.getResponseStart().after(startTime)); |
117 assertNotNull(metrics.getResponseEnd()); | 112 assertNotNull(metrics.getResponseEnd()); |
118 assertTrue(metrics.getResponseEnd().before(endTime) | 113 assertAfter(endTime, metrics.getResponseEnd()); |
119 || metrics.getResponseEnd().equals(endTime)); | |
120 // Entire request should take more than 0 ms | 114 // Entire request should take more than 0 ms |
121 assertTrue(metrics.getResponseEnd().getTime() - metrics.getRequestStart(
).getTime() > 0); | 115 assertTrue(metrics.getResponseEnd().getTime() - metrics.getRequestStart(
).getTime() > 0); |
122 } | 116 } |
123 | 117 |
124 /** | 118 /** |
125 * Check that the timing metrics which come from net::LoadTimingInfo::Connec
tTiming exist, | 119 * Check that the timing metrics which come from net::LoadTimingInfo::Connec
tTiming exist, |
126 * except SSL times in the case of non-https requests. | 120 * except SSL times in the case of non-https requests. |
127 */ | 121 */ |
128 public static void checkHasConnectTiming( | 122 public static void checkHasConnectTiming( |
129 RequestFinishedInfo.Metrics metrics, Date startTime, Date endTime, b
oolean isSsl) { | 123 RequestFinishedInfo.Metrics metrics, Date startTime, Date endTime, b
oolean isSsl) { |
130 assertNotNull(metrics.getDnsStart()); | 124 assertNotNull(metrics.getDnsStart()); |
131 assertTrue( | 125 assertAfter(metrics.getDnsStart(), startTime); |
132 metrics.getDnsStart().after(startTime) || metrics.getDnsStart().
equals(startTime)); | |
133 assertNotNull(metrics.getDnsEnd()); | 126 assertNotNull(metrics.getDnsEnd()); |
134 assertTrue(metrics.getDnsEnd().before(endTime)); | 127 assertTrue(metrics.getDnsEnd().before(endTime)); |
135 assertNotNull(metrics.getConnectStart()); | 128 assertNotNull(metrics.getConnectStart()); |
136 assertTrue(metrics.getConnectStart().after(startTime) | 129 assertAfter(metrics.getConnectStart(), startTime); |
137 || metrics.getConnectStart().equals(startTime)); | |
138 assertNotNull(metrics.getConnectEnd()); | 130 assertNotNull(metrics.getConnectEnd()); |
139 assertTrue(metrics.getConnectEnd().before(endTime)); | 131 assertTrue(metrics.getConnectEnd().before(endTime)); |
140 if (isSsl) { | 132 if (isSsl) { |
141 assertNotNull(metrics.getSslStart()); | 133 assertNotNull(metrics.getSslStart()); |
142 assertTrue(metrics.getSslStart().after(startTime) | 134 assertAfter(metrics.getSslStart(), startTime); |
143 || metrics.getSslStart().equals(startTime)); | |
144 assertNotNull(metrics.getSslEnd()); | 135 assertNotNull(metrics.getSslEnd()); |
145 assertTrue(metrics.getSslEnd().before(endTime)); | 136 assertTrue(metrics.getSslEnd().before(endTime)); |
146 } else { | 137 } else { |
147 assertNull(metrics.getSslStart()); | 138 assertNull(metrics.getSslStart()); |
148 assertNull(metrics.getSslEnd()); | 139 assertNull(metrics.getSslEnd()); |
149 } | 140 } |
150 } | 141 } |
151 | 142 |
152 /** | 143 /** |
153 * Check that the timing metrics from net::LoadTimingInfo::ConnectTiming don
't exist. | 144 * Check that the timing metrics from net::LoadTimingInfo::ConnectTiming don
't exist. |
154 */ | 145 */ |
155 public static void checkNoConnectTiming(RequestFinishedInfo.Metrics metrics)
{ | 146 public static void checkNoConnectTiming(RequestFinishedInfo.Metrics metrics)
{ |
156 assertNull(metrics.getDnsStart()); | 147 assertNull(metrics.getDnsStart()); |
157 assertNull(metrics.getDnsEnd()); | 148 assertNull(metrics.getDnsEnd()); |
158 assertNull(metrics.getSslStart()); | 149 assertNull(metrics.getSslStart()); |
159 assertNull(metrics.getSslEnd()); | 150 assertNull(metrics.getSslEnd()); |
160 assertNull(metrics.getConnectStart()); | 151 assertNull(metrics.getConnectStart()); |
161 assertNull(metrics.getConnectEnd()); | 152 assertNull(metrics.getConnectEnd()); |
162 } | 153 } |
163 } | 154 } |
OLD | NEW |