<commit_before>/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.support.room.integration.testapp.test;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;

import com.android.support.executors.AppToolkitTaskExecutor;
import com.android.support.room.InvalidationTracker;
import com.android.support.room.Room;
import com.android.support.room.integration.testapp.TestDatabase;
import com.android.support.room.integration.testapp.dao.UserDao;
import com.android.support.room.integration.testapp.vo.User;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

/**
 * Tests invalidation tracking.
 */
@SmallTest
@RunWith(AndroidJUnit4.class)
public class InvalidationTest {
    private UserDao mUserDao;
    private TestDatabase mDb;

    @Before
    public void createDb() {
        Context context = InstrumentationRegistry.getTargetContext();
        mDb = Room.inMemoryDatabaseBuilder(context, TestDatabase.class).build();
        mUserDao = mDb.getUserDao();
    }

    private void waitUntilIOThreadIsIdle() {
        FutureTask<Void> future = new FutureTask<>(new Callable<Void>() {
            @Override
            public Void call() throws Exception {
                return null;
            }
        });
        AppToolkitTaskExecutor.getInstance().executeOnDiskIO(future);
        //noinspection TryWithIdenticalCatches
        try {
            future.get();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }
    }

    @Test
    public void testInvalidationOnUpdate() throws InterruptedException {
        User user = TestUtil.createUser(3);
        mUserDao.insert(user);
        LatchObserver observer = new LatchObserver(1, "User");
        mDb.getInvalidationTracker().addObserver(observer);
        mUserDao.updateById(3, "foo2");
        waitUntilIOThreadIsIdle();
        assertThat(observer.await(), is(true));
    }

    @Test
    public void testInvalidationOnDelete() throws InterruptedException {
        User user = TestUtil.createUser(3);
        mUserDao.insert(user);
        LatchObserver observer = new LatchObserver(1, "User");
        mDb.getInvalidationTracker().addObserver(observer);
        mUserDao.delete(user);
        waitUntilIOThreadIsIdle();
        assertThat(observer.await(), is(true));
    }

    @Test
    public void testInvalidationOnInsert() throws InterruptedException {
        LatchObserver observer = new LatchObserver(1, "User");
        mDb.getInvalidationTracker().addObserver(observer);
        mUserDao.insert(TestUtil.createUser(3));
        waitUntilIOThreadIsIdle();
        assertThat(observer.await(), is(true));
    }

    @Test
    public void testDontInvalidateOnLateInsert() throws InterruptedException {
        LatchObserver observer = new LatchObserver(1, "User");
        mUserDao.insert(TestUtil.createUser(3));
        waitUntilIOThreadIsIdle();
        mDb.getInvalidationTracker().addObserver(observer);
        waitUntilIOThreadIsIdle();
        assertThat(observer.await(), is(false));
    }

    private static class LatchObserver extends InvalidationTracker.Observer {
        CountDownLatch mLatch;

        LatchObserver(int permits, String... tables) {
            super(tables);
            mLatch = new CountDownLatch(permits);
        }

        boolean await() throws InterruptedException {
            return mLatch.await(5, TimeUnit.SECONDS);
        }

        @Override
        public void onInvalidated() {
            mLatch.countDown();
        }
    }
}
<commit_msg>Fix flaky test<commit_after>/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.support.room.integration.testapp.test;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;

import com.android.support.executors.AppToolkitTaskExecutor;
import com.android.support.executors.TaskExecutor;
import com.android.support.room.InvalidationTracker;
import com.android.support.room.Room;
import com.android.support.room.integration.testapp.TestDatabase;
import com.android.support.room.integration.testapp.dao.UserDao;
import com.android.support.room.integration.testapp.vo.User;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

/**
 * Tests invalidation tracking.
 */
@SmallTest
@RunWith(AndroidJUnit4.class)
public class InvalidationTest {
    private UserDao mUserDao;
    private TestDatabase mDb;

    @Before
    public void createDb() {
        Context context = InstrumentationRegistry.getTargetContext();
        mDb = Room.inMemoryDatabaseBuilder(context, TestDatabase.class).build();
        mUserDao = mDb.getUserDao();
    }

    @Before
    public void setSingleThreadedIO() {
        AppToolkitTaskExecutor.getInstance().setDelegate(new TaskExecutor() {
            ExecutorService mIOExecutor = Executors.newSingleThreadExecutor();
            Handler mHandler = new Handler(Looper.getMainLooper());
            @Override
            public void executeOnDiskIO(Runnable runnable) {
                mIOExecutor.execute(runnable);
            }

            @Override
            public void postToMainThread(Runnable runnable) {
                mHandler.post(runnable);
            }

            @Override
            public boolean isMainThread() {
                return Thread.currentThread() == Looper.getMainLooper().getThread();
            }
        });
    }

    @After
    public void clearExecutor() {
        AppToolkitTaskExecutor.getInstance().setDelegate(null);
    }

    private void waitUntilIOThreadIsIdle() {
        FutureTask<Void> future = new FutureTask<>(new Callable<Void>() {
            @Override
            public Void call() throws Exception {
                return null;
            }
        });
        AppToolkitTaskExecutor.getInstance().executeOnDiskIO(future);
        //noinspection TryWithIdenticalCatches
        try {
            future.get();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }
    }

    @Test
    public void testInvalidationOnUpdate() throws InterruptedException {
        User user = TestUtil.createUser(3);
        mUserDao.insert(user);
        LatchObserver observer = new LatchObserver(1, "User");
        mDb.getInvalidationTracker().addObserver(observer);
        mUserDao.updateById(3, "foo2");
        waitUntilIOThreadIsIdle();
        assertThat(observer.await(), is(true));
    }

    @Test
    public void testInvalidationOnDelete() throws InterruptedException {
        User user = TestUtil.createUser(3);
        mUserDao.insert(user);
        LatchObserver observer = new LatchObserver(1, "User");
        mDb.getInvalidationTracker().addObserver(observer);
        mUserDao.delete(user);
        waitUntilIOThreadIsIdle();
        assertThat(observer.await(), is(true));
    }

    @Test
    public void testInvalidationOnInsert() throws InterruptedException {
        LatchObserver observer = new LatchObserver(1, "User");
        mDb.getInvalidationTracker().addObserver(observer);
        mUserDao.insert(TestUtil.createUser(3));
        waitUntilIOThreadIsIdle();
        assertThat(observer.await(), is(true));
    }

    @Test
    public void testDontInvalidateOnLateInsert() throws InterruptedException {
        LatchObserver observer = new LatchObserver(1, "User");
        mUserDao.insert(TestUtil.createUser(3));
        waitUntilIOThreadIsIdle();
        mDb.getInvalidationTracker().addObserver(observer);
        waitUntilIOThreadIsIdle();
        assertThat(observer.await(), is(false));
    }

    private static class LatchObserver extends InvalidationTracker.Observer {
        CountDownLatch mLatch;

        LatchObserver(int permits, String... tables) {
            super(tables);
            mLatch = new CountDownLatch(permits);
        }

        boolean await() throws InterruptedException {
            return mLatch.await(5, TimeUnit.SECONDS);
        }

        @Override
        public void onInvalidated() {
            mLatch.countDown();
        }
    }
}

{'fraction_non_alphanumeric': 0.09574775817182528, 'fraction_numerical': 0.00404975412207116, 'mean_word_length': 3.112609040444092, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6352229', 'n_tokens_mistral': 2828, 'n_tokens_neox': 2791, 'n_words': 635}