OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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.blimp.auth; | 5 package org.chromium.blimp.app.auth; |
6 | 6 |
7 import android.content.Intent; | 7 import android.content.Intent; |
8 import android.test.InstrumentationTestCase; | 8 import android.test.InstrumentationTestCase; |
9 import android.test.suitebuilder.annotation.SmallTest; | 9 import android.test.suitebuilder.annotation.SmallTest; |
10 | 10 |
11 import java.util.ArrayList; | 11 import java.util.ArrayList; |
12 import java.util.List; | 12 import java.util.List; |
13 import java.util.concurrent.Semaphore; | 13 import java.util.concurrent.Semaphore; |
14 import java.util.concurrent.TimeUnit; | 14 import java.util.concurrent.TimeUnit; |
15 import java.util.concurrent.atomic.AtomicReference; | 15 import java.util.concurrent.atomic.AtomicReference; |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 * @return A small delay to be used during testing. | 102 * @return A small delay to be used during testing. |
103 */ | 103 */ |
104 @Override | 104 @Override |
105 protected int finalizeRetryDelay(int delay) { | 105 protected int finalizeRetryDelay(int delay) { |
106 mRetryDelays.add(delay); | 106 mRetryDelays.add(delay); |
107 return 1; | 107 return 1; |
108 } | 108 } |
109 } | 109 } |
110 | 110 |
111 private AtomicReference<TestRetryingTokenSource> buildAndTriggerTokenSource( | 111 private AtomicReference<TestRetryingTokenSource> buildAndTriggerTokenSource( |
112 final String correctToken, | 112 final String correctToken, final int transientFailures, final boolea
n hardFailure, |
113 final int transientFailures, | 113 final Semaphore successSemaphore, final Semaphore failureSemaphore, |
114 final boolean hardFailure, | |
115 final Semaphore successSemaphore, | |
116 final Semaphore failureSemaphore, | |
117 final Semaphore needsAccountSemaphore) { | 114 final Semaphore needsAccountSemaphore) { |
118 final AtomicReference<TestRetryingTokenSource> tokenSource = | 115 final AtomicReference<TestRetryingTokenSource> tokenSource = |
119 new AtomicReference<TestRetryingTokenSource>(); | 116 new AtomicReference<TestRetryingTokenSource>(); |
120 getInstrumentation().runOnMainSync(new Runnable() { | 117 getInstrumentation().runOnMainSync(new Runnable() { |
121 @Override | 118 @Override |
122 public void run() { | 119 public void run() { |
123 TokenSource mockTokenSource = | 120 TokenSource mockTokenSource = |
124 new MockTokenSource(correctToken, transientFailures, har
dFailure); | 121 new MockTokenSource(correctToken, transientFailures, har
dFailure); |
125 tokenSource.set(new TestRetryingTokenSource(mockTokenSource, | 122 tokenSource.set(new TestRetryingTokenSource(mockTokenSource, suc
cessSemaphore, |
126 successSemaphore, failureSemaphore, needsAccountSemaphor
e)); | 123 failureSemaphore, needsAccountSemaphore)); |
127 tokenSource.get().getToken(); | 124 tokenSource.get().getToken(); |
128 assertTrue("RetryingTokenSource is not attempting to get a token
.", | 125 assertTrue("RetryingTokenSource is not attempting to get a token
.", |
129 tokenSource.get().isRetrievingToken()); | 126 tokenSource.get().isRetrievingToken()); |
130 } | 127 } |
131 }); | 128 }); |
132 return tokenSource; | 129 return tokenSource; |
133 } | 130 } |
134 | 131 |
135 private void validateTokenSourceResults( | 132 private void validateTokenSourceResults( |
136 final AtomicReference<TestRetryingTokenSource> tokenSource, | 133 final AtomicReference<TestRetryingTokenSource> tokenSource, final St
ring expectedToken, |
137 final String expectedToken, | |
138 final int expectedTransientFailures) { | 134 final int expectedTransientFailures) { |
139 getInstrumentation().runOnMainSync(new Runnable() { | 135 getInstrumentation().runOnMainSync(new Runnable() { |
140 @Override | 136 @Override |
141 public void run() { | 137 public void run() { |
142 String token = tokenSource.get().getReceivedToken(); | 138 String token = tokenSource.get().getReceivedToken(); |
143 assertEquals("getToken() resulted in the wrong token.", expected
Token, token); | 139 assertEquals("getToken() resulted in the wrong token.", expected
Token, token); |
144 | 140 |
145 List<Integer> delays = tokenSource.get().getRetryDelays(); | 141 List<Integer> delays = tokenSource.get().getRetryDelays(); |
146 assertEquals("getToken() resulted in an incorrect number of retr
ies.", | 142 assertEquals("getToken() resulted in an incorrect number of retr
ies.", |
147 expectedTransientFailures + 1, delays.size()); | 143 expectedTransientFailures + 1, delays.size()); |
148 | 144 |
149 Integer prevDelay = Integer.MIN_VALUE; | 145 Integer prevDelay = Integer.MIN_VALUE; |
150 for (int i = 0; i < delays.size(); i++) { | 146 for (int i = 0; i < delays.size(); i++) { |
151 Integer delay = delays.get(i); | 147 Integer delay = delays.get(i); |
152 assertTrue("RetryingTokenSource did not increase delays betw
een attempts " | 148 assertTrue("RetryingTokenSource did not increase delays betw
een attempts " |
153 + "(" + prevDelay + " < " + delay + " failed).", | 149 + "(" + prevDelay + " < " + delay + " failed
).", |
154 prevDelay < delay); | 150 prevDelay < delay); |
155 assertTrue("RetryingTokenSource used a negative delay.", | 151 assertTrue("RetryingTokenSource used a negative delay.", del
ay >= 0); |
156 delay >= 0); | 152 assertTrue( |
157 assertTrue("RetryingTokenSource used no delay for retries.", | 153 "RetryingTokenSource used no delay for retries.", de
lay > 0 || i == 0); |
158 delay > 0 || i == 0); | |
159 prevDelay = delay; | 154 prevDelay = delay; |
160 } | 155 } |
161 } | 156 } |
162 }); | 157 }); |
163 } | 158 } |
164 | 159 |
165 /** | 160 /** |
166 * Tests basic behavior when the token is returned successfully. | 161 * Tests basic behavior when the token is returned successfully. |
167 * @throws InterruptedException | 162 * @throws InterruptedException |
168 */ | 163 */ |
(...skipping 19 matching lines...) Expand all Loading... |
188 * returned successfully. | 183 * returned successfully. |
189 * @throws InterruptedException | 184 * @throws InterruptedException |
190 */ | 185 */ |
191 @SmallTest | 186 @SmallTest |
192 public void testRecoveringTokenSource() throws InterruptedException { | 187 public void testRecoveringTokenSource() throws InterruptedException { |
193 int failureCount = 6; | 188 int failureCount = 6; |
194 | 189 |
195 Semaphore success = new Semaphore(0); | 190 Semaphore success = new Semaphore(0); |
196 Semaphore failure = new Semaphore(0); | 191 Semaphore failure = new Semaphore(0); |
197 Semaphore needsAccount = new Semaphore(0); | 192 Semaphore needsAccount = new Semaphore(0); |
198 AtomicReference<TestRetryingTokenSource> tokenSource = | 193 AtomicReference<TestRetryingTokenSource> tokenSource = buildAndTriggerTo
kenSource( |
199 buildAndTriggerTokenSource(TEST_TOKEN, failureCount, false, succ
ess, failure, | 194 TEST_TOKEN, failureCount, false, success, failure, needsAccount)
; |
200 needsAccount); | |
201 | 195 |
202 assertTrue("Did not receive a successful token in time.", | 196 assertTrue("Did not receive a successful token in time.", |
203 success.tryAcquire(SEMAPHORE_TIMEOUT_MS, TimeUnit.MILLISECONDS))
; | 197 success.tryAcquire(SEMAPHORE_TIMEOUT_MS, TimeUnit.MILLISECONDS))
; |
204 assertFalse("getToken() failed.", failure.tryAcquire()); | 198 assertFalse("getToken() failed.", failure.tryAcquire()); |
205 assertFalse("getToken() needed an account.", needsAccount.tryAcquire()); | 199 assertFalse("getToken() needed an account.", needsAccount.tryAcquire()); |
206 | 200 |
207 validateTokenSourceResults(tokenSource, TEST_TOKEN, failureCount); | 201 validateTokenSourceResults(tokenSource, TEST_TOKEN, failureCount); |
208 } | 202 } |
209 | 203 |
210 /** | 204 /** |
211 * Tests failure behavior for when there is an unrecoverable error after mul
tiple transient | 205 * Tests failure behavior for when there is an unrecoverable error after mul
tiple transient |
212 * errors. | 206 * errors. |
213 * @throws InterruptedException | 207 * @throws InterruptedException |
214 */ | 208 */ |
215 @SmallTest | 209 @SmallTest |
216 public void testFailedTokenSource() throws InterruptedException { | 210 public void testFailedTokenSource() throws InterruptedException { |
217 int failureCount = 6; | 211 int failureCount = 6; |
218 | 212 |
219 Semaphore success = new Semaphore(0); | 213 Semaphore success = new Semaphore(0); |
220 Semaphore failure = new Semaphore(0); | 214 Semaphore failure = new Semaphore(0); |
221 Semaphore needsAccount = new Semaphore(0); | 215 Semaphore needsAccount = new Semaphore(0); |
222 AtomicReference<TestRetryingTokenSource> tokenSource = | 216 AtomicReference<TestRetryingTokenSource> tokenSource = buildAndTriggerTo
kenSource( |
223 buildAndTriggerTokenSource(TEST_TOKEN, failureCount, true, succe
ss, failure, | 217 TEST_TOKEN, failureCount, true, success, failure, needsAccount); |
224 needsAccount); | |
225 | 218 |
226 assertTrue("Did not receive a failure in time.", | 219 assertTrue("Did not receive a failure in time.", |
227 failure.tryAcquire(SEMAPHORE_TIMEOUT_MS, TimeUnit.MILLISECONDS))
; | 220 failure.tryAcquire(SEMAPHORE_TIMEOUT_MS, TimeUnit.MILLISECONDS))
; |
228 assertFalse("getToken() succeeded.", success.tryAcquire()); | 221 assertFalse("getToken() succeeded.", success.tryAcquire()); |
229 assertFalse("getToken() needed an account.", needsAccount.tryAcquire()); | 222 assertFalse("getToken() needed an account.", needsAccount.tryAcquire()); |
230 | 223 |
231 validateTokenSourceResults(tokenSource, null, failureCount); | 224 validateTokenSourceResults(tokenSource, null, failureCount); |
232 } | 225 } |
233 | 226 |
234 /** | 227 /** |
235 * Tests failure behavior for when there is an unrecoverable error after mul
tiple transient | 228 * Tests failure behavior for when there is an unrecoverable error after mul
tiple transient |
236 * errors. | 229 * errors. |
237 * @throws InterruptedException | 230 * @throws InterruptedException |
238 */ | 231 */ |
239 @SmallTest | 232 @SmallTest |
240 public void testTooManyTransientFailures() throws InterruptedException { | 233 public void testTooManyTransientFailures() throws InterruptedException { |
241 int failureCount = RetryingTokenSource.MAX_NUMBER_OF_RETRIES + 1; | 234 int failureCount = RetryingTokenSource.MAX_NUMBER_OF_RETRIES + 1; |
242 | 235 |
243 Semaphore success = new Semaphore(0); | 236 Semaphore success = new Semaphore(0); |
244 Semaphore failure = new Semaphore(0); | 237 Semaphore failure = new Semaphore(0); |
245 Semaphore needsAccount = new Semaphore(0); | 238 Semaphore needsAccount = new Semaphore(0); |
246 AtomicReference<TestRetryingTokenSource> tokenSource = | 239 AtomicReference<TestRetryingTokenSource> tokenSource = buildAndTriggerTo
kenSource( |
247 buildAndTriggerTokenSource(TEST_TOKEN, failureCount, false, succ
ess, failure, | 240 TEST_TOKEN, failureCount, false, success, failure, needsAccount)
; |
248 needsAccount); | |
249 | 241 |
250 assertTrue("Did not receive a failure in time.", | 242 assertTrue("Did not receive a failure in time.", |
251 failure.tryAcquire(SEMAPHORE_TIMEOUT_MS, TimeUnit.MILLISECONDS))
; | 243 failure.tryAcquire(SEMAPHORE_TIMEOUT_MS, TimeUnit.MILLISECONDS))
; |
252 assertFalse("getToken() succeeded.", success.tryAcquire()); | 244 assertFalse("getToken() succeeded.", success.tryAcquire()); |
253 assertFalse("getToken() needed an account.", needsAccount.tryAcquire()); | 245 assertFalse("getToken() needed an account.", needsAccount.tryAcquire()); |
254 | 246 |
255 // Expect one less transient error than MAX_NUMBER_OF_RETRIES. | 247 // Expect one less transient error than MAX_NUMBER_OF_RETRIES. |
256 validateTokenSourceResults( | 248 validateTokenSourceResults( |
257 tokenSource, null, RetryingTokenSource.MAX_NUMBER_OF_RETRIES - 1
); | 249 tokenSource, null, RetryingTokenSource.MAX_NUMBER_OF_RETRIES - 1
); |
258 } | 250 } |
259 } | 251 } |
OLD | NEW |