-
Notifications
You must be signed in to change notification settings - Fork 616
Firestore: QueryTest.java: improve the test that resumes a query with existence filter to actually validate the existence filter. #4813
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
13aae05
Firestore: QueryTest.java: improve the test that resumes a query with…
dconeybe 81a8f73
Refactor: WatchChangeAggregatorTestingHooks -> TestingHooks and Watch…
dconeybe a4726d9
Merge branch 'master' into dconeybe/QueryExistenceFilterTest2
dconeybe db3e7c3
cleanup: rename waitForExistenceFilterMismatch -> getOrWaitForExisten…
dconeybe f62d94f
TestingHooks.java: use CopyOnWriteArrayList and AtomicReference to ma…
dconeybe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
173 changes: 173 additions & 0 deletions
173
.../java/com/google/firebase/firestore/remote/WatchChangeAggregatorTestingHooksAccessor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// 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.google.firebase.firestore.remote; | ||
|
||
import static com.google.firebase.firestore.util.Preconditions.checkNotNull; | ||
|
||
import android.os.SystemClock; | ||
import androidx.annotation.AnyThread; | ||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
import com.google.firebase.firestore.ListenerRegistration; | ||
import java.util.ArrayList; | ||
|
||
/** | ||
* Provides access to the {@link WatchChangeAggregatorTestingHooks} class and its methods. | ||
* | ||
* <p>The {@link WatchChangeAggregatorTestingHooks} class has default visibility, and, therefore, is | ||
* only visible to other classes declared in the same package. This class effectively "re-exports" | ||
* the functionality from {@link WatchChangeAggregatorTestingHooks} in a class with {@code public} | ||
* visibility so that tests written in other packages can access its functionality. | ||
*/ | ||
public final class WatchChangeAggregatorTestingHooksAccessor { | ||
|
||
private WatchChangeAggregatorTestingHooksAccessor() {} | ||
|
||
/** @see WatchChangeAggregatorTestingHooks#addExistenceFilterMismatchListener */ | ||
public static ListenerRegistration addExistenceFilterMismatchListener( | ||
@NonNull ExistenceFilterMismatchListener listener) { | ||
checkNotNull(listener, "a null listener is not allowed"); | ||
return WatchChangeAggregatorTestingHooks.addExistenceFilterMismatchListener( | ||
new ExistenceFilterMismatchListenerWrapper(listener)); | ||
} | ||
|
||
/** @see WatchChangeAggregatorTestingHooks.ExistenceFilterMismatchListener */ | ||
public interface ExistenceFilterMismatchListener { | ||
@AnyThread | ||
void onExistenceFilterMismatch(ExistenceFilterMismatchInfo info); | ||
} | ||
|
||
/** @see WatchChangeAggregatorTestingHooks.ExistenceFilterMismatchInfo */ | ||
public interface ExistenceFilterMismatchInfo { | ||
int localCacheCount(); | ||
|
||
int existenceFilterCount(); | ||
} | ||
|
||
private static final class ExistenceFilterMismatchInfoImpl | ||
implements ExistenceFilterMismatchInfo { | ||
|
||
private final WatchChangeAggregatorTestingHooks.ExistenceFilterMismatchInfo info; | ||
|
||
ExistenceFilterMismatchInfoImpl( | ||
@NonNull WatchChangeAggregatorTestingHooks.ExistenceFilterMismatchInfo info) { | ||
this.info = info; | ||
} | ||
|
||
@Override | ||
public int localCacheCount() { | ||
return info.localCacheCount(); | ||
} | ||
|
||
@Override | ||
public int existenceFilterCount() { | ||
return info.existenceFilterCount(); | ||
} | ||
} | ||
|
||
private static final class ExistenceFilterMismatchListenerWrapper | ||
implements WatchChangeAggregatorTestingHooks.ExistenceFilterMismatchListener { | ||
|
||
private final ExistenceFilterMismatchListener wrappedListener; | ||
|
||
ExistenceFilterMismatchListenerWrapper( | ||
@NonNull ExistenceFilterMismatchListener listenerToWrap) { | ||
this.wrappedListener = listenerToWrap; | ||
} | ||
|
||
@Override | ||
public void onExistenceFilterMismatch( | ||
WatchChangeAggregatorTestingHooks.ExistenceFilterMismatchInfo info) { | ||
this.wrappedListener.onExistenceFilterMismatch(new ExistenceFilterMismatchInfoImpl(info)); | ||
} | ||
} | ||
|
||
public static final class ExistenceFilterMismatchAccumulator { | ||
|
||
private ExistenceFilterMismatchListenerImpl listener; | ||
private ListenerRegistration listenerRegistration = null; | ||
|
||
/** Registers the accumulator to begin listening for existence filter mismatches. */ | ||
public synchronized void register() { | ||
if (listener != null) { | ||
throw new IllegalStateException("already registered"); | ||
} | ||
listener = new ExistenceFilterMismatchListenerImpl(); | ||
listenerRegistration = | ||
WatchChangeAggregatorTestingHooksAccessor.addExistenceFilterMismatchListener(listener); | ||
} | ||
|
||
/** Unregisters the accumulator from listening for existence filter mismatches. */ | ||
public synchronized void unregister() { | ||
if (listener == null) { | ||
return; | ||
} | ||
listenerRegistration.remove(); | ||
listenerRegistration = null; | ||
listener = null; | ||
} | ||
|
||
@Nullable | ||
public WatchChangeAggregatorTestingHooksAccessor.ExistenceFilterMismatchInfo | ||
waitForExistenceFilterMismatch(long timeoutMillis) throws InterruptedException { | ||
ExistenceFilterMismatchListenerImpl capturedListener; | ||
synchronized (this) { | ||
capturedListener = listener; | ||
} | ||
if (capturedListener == null) { | ||
throw new IllegalStateException( | ||
"must be registered before waiting for an existence filter mismatch"); | ||
} | ||
return capturedListener.waitForExistenceFilterMismatch(timeoutMillis); | ||
} | ||
|
||
private static final class ExistenceFilterMismatchListenerImpl | ||
implements WatchChangeAggregatorTestingHooksAccessor.ExistenceFilterMismatchListener { | ||
|
||
private final ArrayList<ExistenceFilterMismatchInfo> existenceFilterMismatches = | ||
new ArrayList<>(); | ||
|
||
@Override | ||
public void onExistenceFilterMismatch( | ||
WatchChangeAggregatorTestingHooksAccessor.ExistenceFilterMismatchInfo info) { | ||
synchronized (existenceFilterMismatches) { | ||
existenceFilterMismatches.add(info); | ||
existenceFilterMismatches.notifyAll(); | ||
} | ||
} | ||
|
||
@Nullable | ||
WatchChangeAggregatorTestingHooksAccessor.ExistenceFilterMismatchInfo | ||
waitForExistenceFilterMismatch(long timeoutMillis) throws InterruptedException { | ||
if (timeoutMillis <= 0) { | ||
throw new IllegalArgumentException("invalid timeout: " + timeoutMillis); | ||
} | ||
synchronized (existenceFilterMismatches) { | ||
long endTimeMillis = SystemClock.uptimeMillis() + timeoutMillis; | ||
while (true) { | ||
if (existenceFilterMismatches.size() > 0) { | ||
return existenceFilterMismatches.remove(0); | ||
} | ||
long currentWaitMillis = endTimeMillis - SystemClock.uptimeMillis(); | ||
if (currentWaitMillis <= 0) { | ||
return null; | ||
} | ||
existenceFilterMismatches.wait(currentWaitMillis); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
...src/main/java/com/google/firebase/firestore/remote/WatchChangeAggregatorTestingHooks.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// 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.google.firebase.firestore.remote; | ||
|
||
import static com.google.firebase.firestore.util.Preconditions.checkNotNull; | ||
|
||
import androidx.annotation.AnyThread; | ||
import androidx.annotation.NonNull; | ||
import androidx.annotation.VisibleForTesting; | ||
import com.google.auto.value.AutoValue; | ||
import com.google.firebase.firestore.ListenerRegistration; | ||
import com.google.firebase.firestore.util.Executors; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
final class WatchChangeAggregatorTestingHooks { | ||
|
||
private WatchChangeAggregatorTestingHooks() {} | ||
|
||
private static final Map<Object, ExistenceFilterMismatchListener> | ||
existenceFilterMismatchListeners = new HashMap<>(); | ||
|
||
/** | ||
* Notifies all registered {@link ExistenceFilterMismatchListener}` listeners registered via | ||
* {@link #addExistenceFilterMismatchListener}. | ||
* | ||
* @param info Information about the existence filter mismatch to deliver to the listeners. | ||
*/ | ||
static void notifyOnExistenceFilterMismatch(ExistenceFilterMismatchInfo info) { | ||
synchronized (existenceFilterMismatchListeners) { | ||
for (ExistenceFilterMismatchListener listener : existenceFilterMismatchListeners.values()) { | ||
Executors.BACKGROUND_EXECUTOR.execute(() -> listener.onExistenceFilterMismatch(info)); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Registers a {@link ExistenceFilterMismatchListener} to be notified when an existence filter | ||
* mismatch occurs in the Watch listen stream. | ||
* | ||
* <p>The relative order in which callbacks are notified is unspecified; do not rely on any | ||
* particular ordering. If a given callback is registered multiple times then it will be notified | ||
* multiple times, once per registration. | ||
* | ||
* <p>The thread on which the callback occurs is unspecified; listeners should perform their work | ||
* as quickly as possible and return to avoid blocking any critical work. In particular, the | ||
* listener callbacks should <em>not</em> block or perform long-running operations. Listener | ||
* callbacks can occur concurrently with other callbacks on the same and other listeners. | ||
* | ||
* @param listener the listener to register. | ||
* @return an object that unregisters the given listener via its {@link | ||
* ListenerRegistration#remove} method; only the first unregistration request does anything; | ||
* all subsequent requests do nothing. | ||
*/ | ||
@VisibleForTesting | ||
static ListenerRegistration addExistenceFilterMismatchListener( | ||
@NonNull ExistenceFilterMismatchListener listener) { | ||
checkNotNull(listener, "a null listener is not allowed"); | ||
|
||
Object listenerId = new Object(); | ||
synchronized (existenceFilterMismatchListeners) { | ||
existenceFilterMismatchListeners.put(listenerId, listener); | ||
} | ||
|
||
return () -> { | ||
synchronized (existenceFilterMismatchListeners) { | ||
existenceFilterMismatchListeners.remove(listenerId); | ||
} | ||
}; | ||
} | ||
|
||
interface ExistenceFilterMismatchListener { | ||
@AnyThread | ||
void onExistenceFilterMismatch(ExistenceFilterMismatchInfo info); | ||
} | ||
|
||
@AutoValue | ||
abstract static class ExistenceFilterMismatchInfo { | ||
|
||
static ExistenceFilterMismatchInfo create(int localCacheCount, int existenceFilterCount) { | ||
return new AutoValue_WatchChangeAggregatorTestingHooks_ExistenceFilterMismatchInfo( | ||
milaGGL marked this conversation as resolved.
Show resolved
Hide resolved
|
||
localCacheCount, existenceFilterCount); | ||
} | ||
|
||
/** Returns the number of documents that matched the query in the local cache. */ | ||
abstract int localCacheCount(); | ||
|
||
/** | ||
* Returns the number of documents that matched the query on the server, as specified in the | ||
* ExistenceFilter message's `count` field. | ||
*/ | ||
abstract int existenceFilterCount(); | ||
|
||
static ExistenceFilterMismatchInfo from(int localCacheCount, ExistenceFilter existenceFilter) { | ||
return create(localCacheCount, existenceFilter.getCount()); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.