Loading java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java +10 −0 Original line number Diff line number Diff line Loading @@ -617,4 +617,14 @@ abstract public class ExpandableBinaryDictionary extends Dictionary { }); return holder.get(false, TIMEOUT_FOR_READ_OPS_IN_MILLISECONDS); } @UsedForTesting public void shutdownExecutorForTests() { getExecutor(mFilename).shutdown(); } @UsedForTesting public boolean isTerminatedForTests() { return getExecutor(mFilename).isTerminated(); } } java/src/com/android/inputmethod/latin/utils/PrioritizedSerialExecutor.java +27 −6 Original line number Diff line number Diff line Loading @@ -31,6 +31,7 @@ public class PrioritizedSerialExecutor { private static final int TASK_QUEUE_CAPACITY = 1000; private final Queue<Runnable> mTasks; private final Queue<Runnable> mPrioritizedTasks; private boolean mIsShutdown; // The task which is running now. private Runnable mActive; Loading @@ -38,6 +39,7 @@ public class PrioritizedSerialExecutor { public PrioritizedSerialExecutor() { mTasks = new ArrayDeque<Runnable>(TASK_QUEUE_CAPACITY); mPrioritizedTasks = new ArrayDeque<Runnable>(TASK_QUEUE_CAPACITY); mIsShutdown = false; } /** Loading @@ -56,12 +58,14 @@ public class PrioritizedSerialExecutor { */ public void execute(final Runnable r) { synchronized(mLock) { if (!mIsShutdown) { mTasks.offer(r); if (mActive == null) { scheduleNext(); } } } } /** * Enqueues the given task into the prioritized task queue. Loading @@ -69,12 +73,14 @@ public class PrioritizedSerialExecutor { */ public void executePrioritized(final Runnable r) { synchronized(mLock) { if (!mIsShutdown) { mPrioritizedTasks.offer(r); if (mActive == null) { scheduleNext(); } } } } private boolean fetchNextTasks() { synchronized(mLock) { Loading Loading @@ -123,4 +129,19 @@ public class PrioritizedSerialExecutor { execute(newTask); } } public void shutdown() { synchronized(mLock) { mIsShutdown = true; } } public boolean isTerminated() { synchronized(mLock) { if (!mIsShutdown) { return false; } return mPrioritizedTasks.isEmpty() && mTasks.isEmpty() && mActive == null; } } } tests/src/com/android/inputmethod/latin/personalization/UserHistoryDictionaryTests.java +18 −3 Original line number Diff line number Diff line Loading @@ -46,6 +46,7 @@ public class UserHistoryDictionaryTests extends AndroidTestCase { }; private static final int MIN_USER_HISTORY_DICTIONARY_FILE_SIZE = 1000; private static final int WAIT_TERMINATING_IN_MILLISECONDS = 100; @Override public void setUp() { Loading Loading @@ -122,8 +123,14 @@ public class UserHistoryDictionaryTests extends AndroidTestCase { true /* checksContents */); } finally { try { final UserHistoryPredictionDictionary dict = PersonalizationHelper.getUserHistoryPredictionDictionary(getContext(), testFilenameSuffix, mPrefs); Log.d(TAG, "waiting for writing ..."); Thread.sleep(TimeUnit.MILLISECONDS.convert(5L, TimeUnit.SECONDS)); dict.shutdownExecutorForTests(); while (!dict.isTerminatedForTests()) { Thread.sleep(WAIT_TERMINATING_IN_MILLISECONDS); } } catch (InterruptedException e) { Log.d(TAG, "InterruptedException: " + e); } Loading @@ -146,11 +153,11 @@ public class UserHistoryDictionaryTests extends AndroidTestCase { final int numberOfWordsInsertedForEachLanguageSwitch = 100; final File dictFiles[] = new File[numberOfLanguages]; final String testFilenameSuffixes[] = new String[numberOfLanguages]; try { final Random random = new Random(123456); // Create filename suffixes for this test. String testFilenameSuffixes[] = new String[numberOfLanguages]; for (int i = 0; i < numberOfLanguages; i++) { testFilenameSuffixes[i] = "testSwitchingLanguages" + i; final String fileName = UserHistoryPredictionDictionary.NAME + "." + Loading @@ -174,7 +181,15 @@ public class UserHistoryDictionaryTests extends AndroidTestCase { } finally { try { Log.d(TAG, "waiting for writing ..."); Thread.sleep(TimeUnit.MILLISECONDS.convert(5L, TimeUnit.SECONDS)); for (int i = 0; i < numberOfLanguages; i++) { final UserHistoryPredictionDictionary dict = PersonalizationHelper.getUserHistoryPredictionDictionary(getContext(), testFilenameSuffixes[i], mPrefs); dict.shutdownExecutorForTests(); while (!dict.isTerminatedForTests()) { Thread.sleep(WAIT_TERMINATING_IN_MILLISECONDS); } } } catch (InterruptedException e) { Log.d(TAG, "InterruptedException: " + e); } Loading Loading
java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java +10 −0 Original line number Diff line number Diff line Loading @@ -617,4 +617,14 @@ abstract public class ExpandableBinaryDictionary extends Dictionary { }); return holder.get(false, TIMEOUT_FOR_READ_OPS_IN_MILLISECONDS); } @UsedForTesting public void shutdownExecutorForTests() { getExecutor(mFilename).shutdown(); } @UsedForTesting public boolean isTerminatedForTests() { return getExecutor(mFilename).isTerminated(); } }
java/src/com/android/inputmethod/latin/utils/PrioritizedSerialExecutor.java +27 −6 Original line number Diff line number Diff line Loading @@ -31,6 +31,7 @@ public class PrioritizedSerialExecutor { private static final int TASK_QUEUE_CAPACITY = 1000; private final Queue<Runnable> mTasks; private final Queue<Runnable> mPrioritizedTasks; private boolean mIsShutdown; // The task which is running now. private Runnable mActive; Loading @@ -38,6 +39,7 @@ public class PrioritizedSerialExecutor { public PrioritizedSerialExecutor() { mTasks = new ArrayDeque<Runnable>(TASK_QUEUE_CAPACITY); mPrioritizedTasks = new ArrayDeque<Runnable>(TASK_QUEUE_CAPACITY); mIsShutdown = false; } /** Loading @@ -56,12 +58,14 @@ public class PrioritizedSerialExecutor { */ public void execute(final Runnable r) { synchronized(mLock) { if (!mIsShutdown) { mTasks.offer(r); if (mActive == null) { scheduleNext(); } } } } /** * Enqueues the given task into the prioritized task queue. Loading @@ -69,12 +73,14 @@ public class PrioritizedSerialExecutor { */ public void executePrioritized(final Runnable r) { synchronized(mLock) { if (!mIsShutdown) { mPrioritizedTasks.offer(r); if (mActive == null) { scheduleNext(); } } } } private boolean fetchNextTasks() { synchronized(mLock) { Loading Loading @@ -123,4 +129,19 @@ public class PrioritizedSerialExecutor { execute(newTask); } } public void shutdown() { synchronized(mLock) { mIsShutdown = true; } } public boolean isTerminated() { synchronized(mLock) { if (!mIsShutdown) { return false; } return mPrioritizedTasks.isEmpty() && mTasks.isEmpty() && mActive == null; } } }
tests/src/com/android/inputmethod/latin/personalization/UserHistoryDictionaryTests.java +18 −3 Original line number Diff line number Diff line Loading @@ -46,6 +46,7 @@ public class UserHistoryDictionaryTests extends AndroidTestCase { }; private static final int MIN_USER_HISTORY_DICTIONARY_FILE_SIZE = 1000; private static final int WAIT_TERMINATING_IN_MILLISECONDS = 100; @Override public void setUp() { Loading Loading @@ -122,8 +123,14 @@ public class UserHistoryDictionaryTests extends AndroidTestCase { true /* checksContents */); } finally { try { final UserHistoryPredictionDictionary dict = PersonalizationHelper.getUserHistoryPredictionDictionary(getContext(), testFilenameSuffix, mPrefs); Log.d(TAG, "waiting for writing ..."); Thread.sleep(TimeUnit.MILLISECONDS.convert(5L, TimeUnit.SECONDS)); dict.shutdownExecutorForTests(); while (!dict.isTerminatedForTests()) { Thread.sleep(WAIT_TERMINATING_IN_MILLISECONDS); } } catch (InterruptedException e) { Log.d(TAG, "InterruptedException: " + e); } Loading @@ -146,11 +153,11 @@ public class UserHistoryDictionaryTests extends AndroidTestCase { final int numberOfWordsInsertedForEachLanguageSwitch = 100; final File dictFiles[] = new File[numberOfLanguages]; final String testFilenameSuffixes[] = new String[numberOfLanguages]; try { final Random random = new Random(123456); // Create filename suffixes for this test. String testFilenameSuffixes[] = new String[numberOfLanguages]; for (int i = 0; i < numberOfLanguages; i++) { testFilenameSuffixes[i] = "testSwitchingLanguages" + i; final String fileName = UserHistoryPredictionDictionary.NAME + "." + Loading @@ -174,7 +181,15 @@ public class UserHistoryDictionaryTests extends AndroidTestCase { } finally { try { Log.d(TAG, "waiting for writing ..."); Thread.sleep(TimeUnit.MILLISECONDS.convert(5L, TimeUnit.SECONDS)); for (int i = 0; i < numberOfLanguages; i++) { final UserHistoryPredictionDictionary dict = PersonalizationHelper.getUserHistoryPredictionDictionary(getContext(), testFilenameSuffixes[i], mPrefs); dict.shutdownExecutorForTests(); while (!dict.isTerminatedForTests()) { Thread.sleep(WAIT_TERMINATING_IN_MILLISECONDS); } } } catch (InterruptedException e) { Log.d(TAG, "InterruptedException: " + e); } Loading