Skip to content

Commit b6d62b5

Browse files
committed
minor refactor
1 parent a44de2e commit b6d62b5

File tree

2 files changed

+82
-74
lines changed

2 files changed

+82
-74
lines changed

firebase-firestore/src/androidTest/java/com/google/firebase/firestore/QueryTest.java

Lines changed: 3 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@
3232
import static org.junit.Assert.assertNull;
3333
import static org.junit.Assert.assertTrue;
3434

35-
import android.os.SystemClock;
36-
import androidx.annotation.Nullable;
3735
import androidx.test.ext.junit.runners.AndroidJUnit4;
3836
import com.google.android.gms.tasks.Task;
3937
import com.google.common.collect.Lists;
@@ -1089,8 +1087,9 @@ public void resumingAQueryShouldUseBloomFilterToAvoidFullRequery() throws Except
10891087
QuerySnapshot snapshot2;
10901088
WatchChangeAggregatorTestingHooksAccessor.ExistenceFilterMismatchInfo
10911089
existenceFilterMismatchInfo;
1092-
ExistenceFilterMismatchAccumulator existenceFilterMismatchAccumulator =
1093-
new ExistenceFilterMismatchAccumulator();
1090+
WatchChangeAggregatorTestingHooksAccessor.ExistenceFilterMismatchAccumulator
1091+
existenceFilterMismatchAccumulator =
1092+
new WatchChangeAggregatorTestingHooksAccessor.ExistenceFilterMismatchAccumulator();
10941093
existenceFilterMismatchAccumulator.register();
10951094
try {
10961095
snapshot2 = waitFor(collection.get());
@@ -1183,76 +1182,6 @@ public void resumingAQueryShouldUseBloomFilterToAvoidFullRequery() throws Except
11831182
}
11841183
}
11851184

1186-
private static final class ExistenceFilterMismatchAccumulator {
1187-
1188-
private final ExistenceFilterMismatchListenerImpl listener =
1189-
new ExistenceFilterMismatchListenerImpl();
1190-
private ListenerRegistration listenerRegistration = null;
1191-
1192-
void register() {
1193-
if (listenerRegistration != null) {
1194-
throw new IllegalStateException("already registered");
1195-
}
1196-
listenerRegistration =
1197-
WatchChangeAggregatorTestingHooksAccessor.addExistenceFilterMismatchListener(listener);
1198-
}
1199-
1200-
void unregister() {
1201-
if (listenerRegistration == null) {
1202-
return;
1203-
}
1204-
listenerRegistration.remove();
1205-
listenerRegistration = null;
1206-
}
1207-
1208-
@Nullable
1209-
WatchChangeAggregatorTestingHooksAccessor.ExistenceFilterMismatchInfo
1210-
waitForExistenceFilterMismatch(long timeoutMillis) throws InterruptedException {
1211-
if (listenerRegistration == null) {
1212-
throw new IllegalStateException(
1213-
"must be registered before waiting for an existence filter mismatch");
1214-
}
1215-
return listener.waitForExistenceFilterMismatch(timeoutMillis);
1216-
}
1217-
1218-
private final class ExistenceFilterMismatchListenerImpl
1219-
implements WatchChangeAggregatorTestingHooksAccessor.ExistenceFilterMismatchListener {
1220-
1221-
private final ArrayList<WatchChangeAggregatorTestingHooksAccessor.ExistenceFilterMismatchInfo>
1222-
existenceFilterMismatches = new ArrayList<>();
1223-
1224-
@Override
1225-
public void onExistenceFilterMismatch(
1226-
WatchChangeAggregatorTestingHooksAccessor.ExistenceFilterMismatchInfo info) {
1227-
synchronized (existenceFilterMismatches) {
1228-
existenceFilterMismatches.add(info);
1229-
existenceFilterMismatches.notifyAll();
1230-
}
1231-
}
1232-
1233-
@Nullable
1234-
WatchChangeAggregatorTestingHooksAccessor.ExistenceFilterMismatchInfo
1235-
waitForExistenceFilterMismatch(long timeoutMillis) throws InterruptedException {
1236-
if (timeoutMillis <= 0) {
1237-
throw new IllegalArgumentException("invalid timeout: " + timeoutMillis);
1238-
}
1239-
synchronized (existenceFilterMismatches) {
1240-
long endTimeMillis = SystemClock.uptimeMillis() + timeoutMillis;
1241-
while (true) {
1242-
if (existenceFilterMismatches.size() > 0) {
1243-
return existenceFilterMismatches.remove(0);
1244-
}
1245-
long currentWaitMillis = endTimeMillis - SystemClock.uptimeMillis();
1246-
if (currentWaitMillis <= 0) {
1247-
return null;
1248-
}
1249-
existenceFilterMismatches.wait(currentWaitMillis);
1250-
}
1251-
}
1252-
}
1253-
}
1254-
}
1255-
12561185
// TODO(orquery): Enable this test when prod supports OR queries.
12571186
@Ignore
12581187
@Test

firebase-firestore/src/androidTest/java/com/google/firebase/firestore/remote/WatchChangeAggregatorTestingHooksAccessor.java

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616

1717
import static com.google.firebase.firestore.util.Preconditions.checkNotNull;
1818

19+
import android.os.SystemClock;
1920
import androidx.annotation.AnyThread;
2021
import androidx.annotation.NonNull;
2122
import androidx.annotation.Nullable;
2223
import com.google.firebase.firestore.ListenerRegistration;
24+
import java.util.ArrayList;
2325

2426
/**
2527
* Provides access to the {@link WatchChangeAggregatorTestingHooks} class and its methods.
@@ -146,4 +148,81 @@ public void onExistenceFilterMismatch(
146148
this.wrappedListener.onExistenceFilterMismatch(new ExistenceFilterMismatchInfoImpl(info));
147149
}
148150
}
151+
152+
public static final class ExistenceFilterMismatchAccumulator {
153+
154+
private ExistenceFilterMismatchListenerImpl listener;
155+
private ListenerRegistration listenerRegistration = null;
156+
157+
/** Registers the accumulator to begin listening for existence filter mismatches. */
158+
public synchronized void register() {
159+
if (listener != null) {
160+
throw new IllegalStateException("already registered");
161+
}
162+
listener = new ExistenceFilterMismatchListenerImpl();
163+
listenerRegistration =
164+
WatchChangeAggregatorTestingHooksAccessor.addExistenceFilterMismatchListener(listener);
165+
}
166+
167+
/** Unregisters the accumulator from listening for existence filter mismatches. */
168+
public synchronized void unregister() {
169+
if (listener == null) {
170+
return;
171+
}
172+
listenerRegistration.remove();
173+
listenerRegistration = null;
174+
listener = null;
175+
}
176+
177+
@Nullable
178+
public WatchChangeAggregatorTestingHooksAccessor.ExistenceFilterMismatchInfo
179+
waitForExistenceFilterMismatch(long timeoutMillis) throws InterruptedException {
180+
ExistenceFilterMismatchListenerImpl capturedListener;
181+
synchronized (this) {
182+
capturedListener = listener;
183+
}
184+
if (capturedListener == null) {
185+
throw new IllegalStateException(
186+
"must be registered before waiting for an existence filter mismatch");
187+
}
188+
return capturedListener.waitForExistenceFilterMismatch(timeoutMillis);
189+
}
190+
191+
private static final class ExistenceFilterMismatchListenerImpl
192+
implements WatchChangeAggregatorTestingHooksAccessor.ExistenceFilterMismatchListener {
193+
194+
private final ArrayList<ExistenceFilterMismatchInfo> existenceFilterMismatches =
195+
new ArrayList<>();
196+
197+
@Override
198+
public void onExistenceFilterMismatch(
199+
WatchChangeAggregatorTestingHooksAccessor.ExistenceFilterMismatchInfo info) {
200+
synchronized (existenceFilterMismatches) {
201+
existenceFilterMismatches.add(info);
202+
existenceFilterMismatches.notifyAll();
203+
}
204+
}
205+
206+
@Nullable
207+
WatchChangeAggregatorTestingHooksAccessor.ExistenceFilterMismatchInfo
208+
waitForExistenceFilterMismatch(long timeoutMillis) throws InterruptedException {
209+
if (timeoutMillis <= 0) {
210+
throw new IllegalArgumentException("invalid timeout: " + timeoutMillis);
211+
}
212+
synchronized (existenceFilterMismatches) {
213+
long endTimeMillis = SystemClock.uptimeMillis() + timeoutMillis;
214+
while (true) {
215+
if (existenceFilterMismatches.size() > 0) {
216+
return existenceFilterMismatches.remove(0);
217+
}
218+
long currentWaitMillis = endTimeMillis - SystemClock.uptimeMillis();
219+
if (currentWaitMillis <= 0) {
220+
return null;
221+
}
222+
existenceFilterMismatches.wait(currentWaitMillis);
223+
}
224+
}
225+
}
226+
}
227+
}
149228
}

0 commit comments

Comments
 (0)