Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(146)

Side by Side Diff: components/cronet/android/test/src/org/chromium/net/CronetTestUtil.java

Issue 2406273002: [Cronet] Test the libcronet that's shipped, not libcronet_test (Closed)
Patch Set: fix perf test Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 android.os.ConditionVariable; 7 import android.os.ConditionVariable;
8 import android.os.Handler;
9 import android.os.Looper;
8 10
11 import org.json.JSONException;
12 import org.json.JSONObject;
13
14 import org.chromium.base.annotations.CalledByNative;
9 import org.chromium.base.annotations.JNINamespace; 15 import org.chromium.base.annotations.JNINamespace;
10 import org.chromium.net.impl.CronetUrlRequest; 16 import org.chromium.net.impl.CronetUrlRequest;
11 import org.json.JSONException; 17
12 import org.json.JSONObject; 18 import java.nio.ByteBuffer;
19 import java.util.concurrent.Executor;
13 20
14 /** 21 /**
15 * Utilities for Cronet testing 22 * Utilities for Cronet testing
16 */ 23 */
17 @JNINamespace("cronet") 24 @JNINamespace("cronet")
18 public class CronetTestUtil { 25 public class CronetTestUtil {
19 private static final ConditionVariable sHostResolverBlock = new ConditionVar iable(); 26 private static final ConditionVariable sHostResolverBlock = new ConditionVar iable();
27 private static CronetEngine sCronetEngineWithPreparedNetworkThread;
kapishnikov 2016/10/20 17:25:46 It would be great if we can avoid 'static' here.
pauljensen 2016/10/24 18:52:53 Done.
20 28
21 static final String SDCH_FAKE_HOST = "fake.sdch.domain"; 29 static final String SDCH_FAKE_HOST = "fake.sdch.domain";
22 // QUIC test domain must match the certificate used 30 // QUIC test domain must match the certificate used
23 // (quic_test.example.com.crt and quic_test.example.com.key.pkcs8), and 31 // (quic_test.example.com.crt and quic_test.example.com.key.pkcs8), and
24 // the file served ( 32 // the file served (
25 // components/cronet/android/test/assets/test/quic_data/simple.txt). 33 // components/cronet/android/test/assets/test/quic_data/simple.txt).
26 static final String QUIC_FAKE_HOST = "test.example.com"; 34 static final String QUIC_FAKE_HOST = "test.example.com";
27 private static final String[] TEST_DOMAINS = {SDCH_FAKE_HOST, QUIC_FAKE_HOST }; 35 private static final String[] TEST_DOMAINS = {SDCH_FAKE_HOST, QUIC_FAKE_HOST };
28 private static final String LOOPBACK_ADDRESS = "127.0.0.1"; 36 private static final String LOOPBACK_ADDRESS = "127.0.0.1";
29 37
(...skipping 21 matching lines...) Expand all
51 */ 59 */
52 public static JSONObject generateHostResolverRules(String destination) throw s JSONException { 60 public static JSONObject generateHostResolverRules(String destination) throw s JSONException {
53 StringBuilder rules = new StringBuilder(); 61 StringBuilder rules = new StringBuilder();
54 for (String domain : TEST_DOMAINS) { 62 for (String domain : TEST_DOMAINS) {
55 rules.append("MAP " + domain + " " + destination + ","); 63 rules.append("MAP " + domain + " " + destination + ",");
56 } 64 }
57 return new JSONObject().put("host_resolver_rules", rules); 65 return new JSONObject().put("host_resolver_rules", rules);
58 } 66 }
59 67
60 /** 68 /**
69 * Run {@code r} on {@code engine}'s network thread.
70 */
71 public static void postToNetworkThread(final CronetEngine engine, final Runn able r) {
72 // This method works by creating a UrlRequest with {@code engine} and us ing a
73 // direct-executor to execute on {@code engine}'s network thread. The U rlRequest
74 // goes to a bogus URL which immediately triggers {@link UrlRequest#Call back#onFailed}.
75
76 // UrlRequests cannot be created on the network thread (see DCHECK in
77 // CronetURLRequestAdapter()), so this method must first post over to th e main thread,
78 // in case it was called on the network thread.
79 new Handler(Looper.getMainLooper()).post(new Runnable() {
80 @Override
81 public void run() {
82 Executor directExecutor = new Executor() {
83 @Override
84 public void execute(Runnable runable) {
85 runable.run();
86 }
87 };
88 UrlRequest.Callback callback = new UrlRequest.Callback() {
89 @Override
90 public void onRedirectReceived(UrlRequest request, UrlRespon seInfo responseInfo,
91 String newLocationUrl) {}
92
93 @Override
94 public void onResponseStarted(
95 UrlRequest request, UrlResponseInfo responseInfo) {}
96
97 @Override
98 public void onReadCompleted(UrlRequest request, UrlResponseI nfo responseInfo,
99 ByteBuffer byteBuffer) {}
100
101 @Override
102 public void onSucceeded(UrlRequest request, UrlResponseInfo responseInfo) {}
103
104 @Override
105 public void onFailed(UrlRequest request, UrlResponseInfo res ponseInfo,
106 UrlRequestException error) {
107 r.run();
108 }
109 };
110 // Bogus empty URL will trigger onFailed() immediately.
111 new UrlRequest.Builder("", callback, directExecutor, engine).bui ld().start();
112 }
113 });
114 }
115
116 /**
117 * Prepare {@code cronetEngine}'s network thread so libcronet_test code can run on it.
118 */
119 public static void prepareNetworkThreadForTesting(final CronetEngine cronetE ngine) {
120 sCronetEngineWithPreparedNetworkThread = cronetEngine;
121 final ConditionVariable initDone = new ConditionVariable();
122 postToNetworkThread(cronetEngine, new Runnable() {
123 @Override
124 public void run() {
125 nativePrepareNetworkThreadForTesting();
126 initDone.open();
127 }
128 });
129 // Don't return until network thread is prepared. Some test call
130 // {@code cronetEngine.shutdown} immediatly after calling this function, which will fail
131 // if postToNetworkThread() isn't finished running a UrlRequest with {@c ode cronetEngine}.
132 initDone.block();
133 }
134
135 /**
136 * Get pointer to SingleThreadTaskRunner created by {@link #prepareNetworkTh readForTesting}.
137 */
138 public static long getNativeSingleThreadTaskRunner() {
139 return nativeGetNativeSingleThreadTaskRunner();
140 }
141
142 /**
143 * Clean up preparation performed by {@link #prepareNetworkThreadForTesting} .
144 */
145 public static void cleanupNetorkThreadForTesting() {
146 final ConditionVariable cleanupDone = new ConditionVariable();
147 postToNetworkThread(sCronetEngineWithPreparedNetworkThread, new Runnable () {
148 @Override
149 public void run() {
150 nativeCleanupNetorkThreadForTesting();
151 cleanupDone.open();
152 }
153 });
154 // Don't return until network thread is cleaned up. Some test call
155 // {@code cronetEngine.shutdown} immediatly after calling this function, which will fail
156 // if postToNetworkThread() isn't finished running a UrlRequest with {@c ode cronetEngine}.
157 cleanupDone.block();
158 }
159
160 /**
161 * Execute base::Closure pointed to by {@code closure} on network thread.
162 */
163 @CalledByNative
164 private static void postClosureToNetworkThread(final long closure) {
165 CronetTestUtil.postToNetworkThread(sCronetEngineWithPreparedNetworkThrea d, new Runnable() {
166 @Override
167 public void run() {
168 nativeRunClosure(closure);
169 }
170 });
171 }
172
173 /**
61 * Returns the value of load flags in |urlRequest|. 174 * Returns the value of load flags in |urlRequest|.
62 * @param urlRequest is the UrlRequest object of interest. 175 * @param urlRequest is the UrlRequest object of interest.
63 */ 176 */
64 public static int getLoadFlags(UrlRequest urlRequest) { 177 public static int getLoadFlags(UrlRequest urlRequest) {
65 return nativeGetLoadFlags(((CronetUrlRequest) urlRequest).getUrlRequestA dapterForTesting()); 178 return nativeGetLoadFlags(((CronetUrlRequest) urlRequest).getUrlRequestF orTesting());
66 } 179 }
67 180
68 private static native int nativeGetLoadFlags(long urlRequest); 181 private static native int nativeGetLoadFlags(long urlRequest);
182
183 private static native void nativePrepareNetworkThreadForTesting();
kapishnikov 2016/10/20 17:25:46 A static method with no context. This limits us to
184 private static native long nativeGetNativeSingleThreadTaskRunner();
185 private static native void nativeCleanupNetorkThreadForTesting();
186 private static native void nativeRunClosure(long closure);
69 } 187 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698