am 5f304d0d: Merge "Add waitAllTasksForTests for testing."

* commit '5f304d0d71b422d1fd4edc41c71e99181b6e8174':
  Add waitAllTasksForTests for testing.
main
Keisuke Kuroyanagi 2013-12-12 00:18:28 -08:00 committed by Android Git Automerger
commit 5a190037e4
2 changed files with 21 additions and 27 deletions

View File

@ -36,6 +36,7 @@ import java.util.HashMap;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
@ -738,13 +739,19 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
} }
@UsedForTesting @UsedForTesting
public void shutdownExecutorForTests() { public void waitAllTasksForTests() {
getExecutor(mDictName).shutdown(); final CountDownLatch countDownLatch = new CountDownLatch(1);
getExecutor(mDictName).execute(new Runnable() {
@Override
public void run() {
countDownLatch.countDown();
}
});
try {
countDownLatch.await();
} catch (InterruptedException e) {
Log.e(TAG, "Interrupted while waiting for finishing dictionary operations.", e);
} }
@UsedForTesting
public boolean isTerminatedForTests() {
return getExecutor(mDictName).isTerminated();
} }
@UsedForTesting @UsedForTesting

View File

@ -43,9 +43,6 @@ public class UserHistoryDictionaryTests extends AndroidTestCase {
"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"
}; };
private static final int MIN_USER_HISTORY_DICTIONARY_FILE_SIZE = 1000;
private static final int WAIT_TERMINATING_IN_MILLISECONDS = 100;
/** /**
* Generates a random word. * Generates a random word.
*/ */
@ -71,8 +68,8 @@ public class UserHistoryDictionaryTests extends AndroidTestCase {
private void addToDict(final UserHistoryDictionary dict, final List<String> words) { private void addToDict(final UserHistoryDictionary dict, final List<String> words) {
String prevWord = null; String prevWord = null;
for (String word : words) { for (String word : words) {
// TODO: Use timestamp properly. dict.addToDictionary(prevWord, word, true,
dict.addToDictionary(prevWord, word, true, 0 /* timestamp */); (int)TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()));
prevWord = word; prevWord = word;
} }
} }
@ -90,10 +87,7 @@ public class UserHistoryDictionaryTests extends AndroidTestCase {
// Add random words to the user history dictionary. // Add random words to the user history dictionary.
addToDict(dict, words); addToDict(dict, words);
if (checkContents) { if (checkContents) {
try { dict.waitAllTasksForTests();
Thread.sleep(TimeUnit.MILLISECONDS.convert(5L, TimeUnit.SECONDS));
} catch (InterruptedException e) {
}
for (int i = 0; i < numberOfWords; ++i) { for (int i = 0; i < numberOfWords; ++i) {
final String word = words.get(i); final String word = words.get(i);
assertTrue(dict.isInDictionaryForTests(word)); assertTrue(dict.isInDictionaryForTests(word));
@ -120,17 +114,10 @@ public class UserHistoryDictionaryTests extends AndroidTestCase {
* @param testFilenameSuffix file name suffix used for testing. * @param testFilenameSuffix file name suffix used for testing.
*/ */
private void waitForWriting(final String testFilenameSuffix) { private void waitForWriting(final String testFilenameSuffix) {
try {
final UserHistoryDictionary dict = final UserHistoryDictionary dict =
PersonalizationHelper.getUserHistoryDictionary(getContext(), PersonalizationHelper.getUserHistoryDictionary(getContext(),
new Locale(testFilenameSuffix)); new Locale(testFilenameSuffix));
dict.shutdownExecutorForTests(); dict.waitAllTasksForTests();
while (!dict.isTerminatedForTests()) {
Thread.sleep(WAIT_TERMINATING_IN_MILLISECONDS);
}
} catch (InterruptedException e) {
Log.d(TAG, "InterruptedException: ", e);
}
} }
public void testRandomWords() { public void testRandomWords() {