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

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

Issue 1419033005: [Cronet] Remove CriteriaHelper (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rework CronetSamepleTest Created 5 years, 1 month 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
(Empty)
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
3 // found in the LICENSE file.
4
5 package org.chromium.net;
6
7 import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout;
8
9 import android.os.SystemClock;
10
11 /**
12 * Helper methods for creating and managing criteria.
13 * <p>
14 * If possible, use callbacks or testing delegates instead of criteria as they
15 * do not introduce any polling delays. Should only use Criteria if no suitable
16 * other approach exists.
17 */
18 public class CriteriaHelper {
19
20 /** The default maximum time to wait for a criteria to become valid. */
21 public static final long DEFAULT_MAX_TIME_TO_POLL = scaleTimeout(3000);
22
23 /**
24 * The default polling interval to wait between checking for a satisfied
25 * criteria.
26 */
27 public static final long DEFAULT_POLLING_INTERVAL = 50;
28
29 /**
30 * Checks whether the given Criteria is satisfied at a given interval, until
31 * either the criteria is satisfied, or the specified maxTimeoutMs number of
32 * ms has elapsed.
33 *
34 * @param criteria The Criteria that will be checked.
35 * @param maxTimeoutMs The maximum number of ms that this check will be
36 * performed for before timeout.
37 * @param checkIntervalMs The number of ms between checks.
38 * @return {@code true} if checking has ended with the criteria being
39 * satisfied.
40 * @throws InterruptedException
41 */
42 public static boolean pollForCriteria(Criteria criteria, long maxTimeoutMs,
43 long checkIntervalMs) throws InterruptedException {
44 boolean isSatisfied = criteria.isSatisfied();
45 long startTime = SystemClock.uptimeMillis();
46 while (!isSatisfied
47 && SystemClock.uptimeMillis() - startTime < maxTimeoutMs) {
48 Thread.sleep(checkIntervalMs);
49 isSatisfied = criteria.isSatisfied();
50 }
51 return isSatisfied;
52 }
53
54 /**
55 * Checks whether the given Criteria is satisfied polling at a default
56 * interval.
57 *
58 * @param criteria The Criteria that will be checked.
59 * @return {@code true} if checking has ended with the criteria being
60 * satisfied.
61 * @throws InterruptedException
62 * @see #pollForCriteria(Criteria, long, long)
63 */
64 public static boolean pollForCriteria(Criteria criteria)
65 throws InterruptedException {
66 return pollForCriteria(criteria, DEFAULT_MAX_TIME_TO_POLL,
67 DEFAULT_POLLING_INTERVAL);
68 }
69
70 /**
71 * Performs the runnable action, then checks whether the given criteria are
72 * satisfied until the specified timeout, using the pollForCriteria method.
73 * If not, then the runnable action is performed again, to a maximum of
74 * maxAttempts tries.
75 */
76 public static boolean runUntilCriteria(Runnable runnable, Criteria criteria,
77 int maxAttempts, long maxTimeoutMs, long checkIntervalMs)
78 throws InterruptedException {
79 int count = 0;
80 boolean success = false;
81 while (count < maxAttempts && !success) {
82 count++;
83 runnable.run();
84 success = pollForCriteria(criteria, maxTimeoutMs, checkIntervalMs);
85 }
86 return success;
87 }
88 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698