| 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 | 9 |
| 10 import android.os.ConditionVariable; |
| 11 |
| 10 import java.util.LinkedList; | 12 import java.util.LinkedList; |
| 11 import java.util.NoSuchElementException; | 13 import java.util.NoSuchElementException; |
| 12 import java.util.concurrent.Executor; | 14 import java.util.concurrent.Executor; |
| 15 import java.util.concurrent.Executors; |
| 13 | 16 |
| 14 /** | 17 /** |
| 15 * Classes which are useful for testing Cronet's metrics implementation and are
needed in more than | 18 * Classes which are useful for testing Cronet's metrics implementation and are
needed in more than |
| 16 * one test file. | 19 * one test file. |
| 17 */ | 20 */ |
| 18 public class MetricsTestUtil { | 21 public class MetricsTestUtil { |
| 19 /** | 22 /** |
| 20 * Executor which runs tasks only when told to with runAllTasks(). | 23 * Executor which runs tasks only when told to with runAllTasks(). |
| 21 */ | 24 */ |
| 22 public static class TestExecutor implements Executor { | 25 public static class TestExecutor implements Executor { |
| 23 private final LinkedList<Runnable> mTaskQueue = new LinkedList<Runnable>
(); | 26 private final LinkedList<Runnable> mTaskQueue = new LinkedList<Runnable>
(); |
| 24 | 27 |
| 25 @Override | 28 @Override |
| 26 public void execute(Runnable task) { | 29 public void execute(Runnable task) { |
| 27 mTaskQueue.add(task); | 30 mTaskQueue.add(task); |
| 28 } | 31 } |
| 29 | 32 |
| 30 public void runAllTasks() { | 33 public void runAllTasks() { |
| 31 try { | 34 try { |
| 32 while (mTaskQueue.size() > 0) { | 35 while (mTaskQueue.size() > 0) { |
| 33 mTaskQueue.remove().run(); | 36 mTaskQueue.remove().run(); |
| 34 } | 37 } |
| 35 } catch (NoSuchElementException e) { | 38 } catch (NoSuchElementException e) { |
| 36 throw new RuntimeException("Task was removed during iteration",
e); | 39 throw new RuntimeException("Task was removed during iteration",
e); |
| 37 } | 40 } |
| 38 } | 41 } |
| 39 } | 42 } |
| 40 | 43 |
| 41 /** | 44 /** |
| 42 * RequestFinishedInfo.Listener for testing, which saves the RequestFinished
Info | 45 * RequestFinishedInfo.Listener for testing, which saves the RequestFinished
Info |
| 43 */ | 46 */ |
| 44 public static class TestRequestFinishedListener extends RequestFinishedInfo.
Listener { | 47 public static class TestRequestFinishedListener extends RequestFinishedInfo.
Listener { |
| 45 private RequestFinishedInfo mRequestInfo; | 48 private RequestFinishedInfo mRequestInfo; |
| 49 private ConditionVariable mBlock; |
| 50 private int mNumExpectedRequests = -1; |
| 46 | 51 |
| 47 public TestRequestFinishedListener(Executor executor) { | 52 public TestRequestFinishedListener(Executor executor) { |
| 48 super(executor); | 53 super(executor); |
| 49 } | 54 } |
| 50 | 55 |
| 56 public TestRequestFinishedListener(int numExpectedRequests) { |
| 57 super(Executors.newSingleThreadExecutor()); |
| 58 mNumExpectedRequests = numExpectedRequests; |
| 59 mBlock = new ConditionVariable(); |
| 60 } |
| 61 |
| 62 public TestRequestFinishedListener() { |
| 63 super(Executors.newSingleThreadExecutor()); |
| 64 mNumExpectedRequests = 1; |
| 65 mBlock = new ConditionVariable(); |
| 66 } |
| 67 |
| 51 public RequestFinishedInfo getRequestInfo() { | 68 public RequestFinishedInfo getRequestInfo() { |
| 52 return mRequestInfo; | 69 return mRequestInfo; |
| 53 } | 70 } |
| 54 | 71 |
| 55 @Override | 72 @Override |
| 56 public void onRequestFinished(RequestFinishedInfo requestInfo) { | 73 public void onRequestFinished(RequestFinishedInfo requestInfo) { |
| 57 assertNull("onRequestFinished called repeatedly", mRequestInfo); | 74 assertNull("onRequestFinished called repeatedly", mRequestInfo); |
| 58 assertNotNull(requestInfo); | 75 assertNotNull(requestInfo); |
| 59 mRequestInfo = requestInfo; | 76 mRequestInfo = requestInfo; |
| 77 mNumExpectedRequests--; |
| 78 if (mNumExpectedRequests == 0) { |
| 79 mBlock.open(); |
| 80 } |
| 81 } |
| 82 |
| 83 public void blockUntilDone() { |
| 84 mBlock.block(); |
| 60 } | 85 } |
| 61 } | 86 } |
| 62 } | 87 } |
| OLD | NEW |