blob: 556d473661934a4ad23cf6a5e6955f3d609905bb [file] [log] [blame]
Tyson Henningee3fb4b2018-05-16 15:51:37 -07001/*
2 * Copyright 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package androidx.room.integration.testapp.test;
18
Aurimas Liutikasb7eeda42018-07-10 11:57:16 -070019import static androidx.test.InstrumentationRegistry.getInstrumentation;
Tyson Henningee3fb4b2018-05-16 15:51:37 -070020
21import static org.hamcrest.CoreMatchers.is;
22import static org.hamcrest.MatcherAssert.assertThat;
23import static org.hamcrest.Matchers.greaterThan;
24
Tyson Henningee3fb4b2018-05-16 15:51:37 -070025import androidx.arch.core.executor.ArchTaskExecutor;
26import androidx.arch.core.executor.DefaultTaskExecutor;
27import androidx.lifecycle.LiveData;
28import androidx.room.Room;
29import androidx.room.integration.testapp.TestDatabase;
30import androidx.room.integration.testapp.dao.PetDao;
31import androidx.room.integration.testapp.vo.Pet;
Aurimas Liutikasb7eeda42018-07-10 11:57:16 -070032import androidx.test.InstrumentationRegistry;
33import androidx.test.filters.SmallTest;
34import androidx.test.runner.AndroidJUnit4;
Tyson Henningee3fb4b2018-05-16 15:51:37 -070035
36import com.google.common.base.Optional;
37import com.google.common.util.concurrent.ListenableFuture;
38import com.google.common.util.concurrent.SettableFuture;
39
40import org.junit.After;
41import org.junit.Before;
42import org.junit.Test;
43import org.junit.runner.RunWith;
44
45import java.util.ArrayDeque;
46import java.util.Queue;
47import java.util.concurrent.ExecutionException;
48import java.util.concurrent.Executor;
49import java.util.concurrent.ExecutorService;
50import java.util.concurrent.Executors;
51
52@SmallTest
53@RunWith(AndroidJUnit4.class)
54public class QueryExecutorTest {
55
56 /**
57 * Blocks the calling thread while executing input commands on a background thread. Also records
58 * executed tasks into a queue.
59 */
60 private static class RecordingExecutor implements Executor {
61 private final Queue<Runnable> mTaskQueue = new ArrayDeque<>();
62
63 private ExecutorService mExecutorService = Executors.newFixedThreadPool(3);
64
65 @Override
66 public void execute(Runnable command) {
67 try {
68 mExecutorService.submit(command).get();
69 } catch (Exception e) {
70 throw new RuntimeException(e);
71 }
72 mTaskQueue.add(command);
73 }
74 }
75
76 private RecordingExecutor mRecordingExecutor;
77 private TestDatabase mTestDatabase;
78 private PetDao mPetDao;
79
80 @Before
81 public void setUp() {
82 // Set ArchTaskExecutor to crash. It should never be invoked.
83 ArchTaskExecutor.getInstance().setDelegate(new DefaultTaskExecutor() {
84 @Override
85 public void executeOnDiskIO(Runnable runnable) {
86 throw new IllegalStateException(
87 "ArchTestExecutor was used instead of the set Executor.");
88 }
89 });
90
91 mRecordingExecutor = new RecordingExecutor();
92 mTestDatabase = Room.inMemoryDatabaseBuilder(
93 InstrumentationRegistry.getContext(),
94 TestDatabase.class)
95 .setQueryExecutor(mRecordingExecutor)
96 .build();
97 mPetDao = mTestDatabase.getPetDao();
98 }
99
100 @After
101 public void tearDown() {
102 mTestDatabase.close();
103 }
104
105 @Test
106 public void testInitDoesNotUseExecutor() {
107 assertThat(
108 mRecordingExecutor.mTaskQueue.isEmpty(),
109 is(true));
110 }
111
112 @Test
113 public void testGuavaListenableFutureUsesExecutorOnQuery()
114 throws ExecutionException, InterruptedException {
115 mPetDao.insertOrReplace(TestUtil.createPet(2));
116 ListenableFuture<Optional<Pet>> petsNamedFuture = mPetDao.petWithIdFuture(2);
117
118 assertThat(
119 mRecordingExecutor.mTaskQueue.size(),
120 greaterThan(0));
121 assertThat(
122 petsNamedFuture.get().isPresent(),
123 is(true));
124 assertThat(
125 petsNamedFuture.get().get().getPetId(),
126 is(2));
127 }
128
129 @Test
130 public void testRxUsesExecutorOnConsume() {
131 mPetDao.insertOrReplace(TestUtil.createPet(2));
132 Pet pet = mPetDao.petWithIdFlowable(2).blockingFirst();
133
134 assertThat(
135 mRecordingExecutor.mTaskQueue.size(),
136 greaterThan(0));
137 assertThat(
138 pet.getPetId(),
139 is(2));
140 }
141
142 @Test
143 public void testLiveDataUsesExecutorOnObserve()
144 throws ExecutionException, InterruptedException {
145 mPetDao.insertOrReplace(TestUtil.createPet(2));
146 LiveData<Pet> petLiveData = mPetDao.petWithIdLiveData(2);
147
148 SettableFuture<Pet> observedPet = SettableFuture.create();
149 getInstrumentation().runOnMainSync(() -> petLiveData.observeForever(pet -> {
150 observedPet.set(pet);
151 }));
152 getInstrumentation().waitForIdleSync();
153
154 assertThat(
155 mRecordingExecutor.mTaskQueue.size(),
156 greaterThan(0));
157 assertThat(petLiveData.getValue().getPetId(), is(2));
158 assertThat(observedPet.get().getPetId(), is(2));
159 }
160}