|
| 1 | +// Copyright 2023 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package com.google.firebase.firestore.remote; |
| 16 | + |
| 17 | +import static com.google.firebase.firestore.util.Preconditions.checkNotNull; |
| 18 | + |
| 19 | +import androidx.annotation.AnyThread; |
| 20 | +import androidx.annotation.NonNull; |
| 21 | +import androidx.annotation.VisibleForTesting; |
| 22 | +import com.google.auto.value.AutoValue; |
| 23 | +import com.google.firebase.firestore.ListenerRegistration; |
| 24 | +import com.google.firebase.firestore.util.Executors; |
| 25 | +import java.util.concurrent.CopyOnWriteArrayList; |
| 26 | +import java.util.concurrent.atomic.AtomicReference; |
| 27 | + |
| 28 | +/** |
| 29 | + * Manages "testing hooks", hooks into the internals of the SDK to verify internal state and events |
| 30 | + * during integration tests. |
| 31 | + * |
| 32 | + * <p>Do not use this class except for testing purposes. |
| 33 | + */ |
| 34 | +@VisibleForTesting |
| 35 | +final class TestingHooks { |
| 36 | + |
| 37 | + private static final TestingHooks instance = new TestingHooks(); |
| 38 | + |
| 39 | + // Use CopyOnWriteArrayList to store the listeners so that we don't need to worry about |
| 40 | + // synchronizing adds, removes, and traversals. |
| 41 | + private final CopyOnWriteArrayList<AtomicReference<ExistenceFilterMismatchListener>> |
| 42 | + existenceFilterMismatchListeners = new CopyOnWriteArrayList<>(); |
| 43 | + |
| 44 | + private TestingHooks() {} |
| 45 | + |
| 46 | + /** Returns the singleton instance of this class. */ |
| 47 | + @NonNull |
| 48 | + static TestingHooks getInstance() { |
| 49 | + return instance; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Asynchronously notifies all registered {@link ExistenceFilterMismatchListener}` listeners |
| 54 | + * registered via {@link #addExistenceFilterMismatchListener}. |
| 55 | + * |
| 56 | + * @param info Information about the existence filter mismatch to deliver to the listeners. |
| 57 | + */ |
| 58 | + void notifyOnExistenceFilterMismatch(@NonNull ExistenceFilterMismatchInfo info) { |
| 59 | + for (AtomicReference<ExistenceFilterMismatchListener> listenerRef : |
| 60 | + existenceFilterMismatchListeners) { |
| 61 | + Executors.BACKGROUND_EXECUTOR.execute( |
| 62 | + () -> { |
| 63 | + ExistenceFilterMismatchListener listener = listenerRef.get(); |
| 64 | + if (listener != null) { |
| 65 | + listener.onExistenceFilterMismatch(info); |
| 66 | + } |
| 67 | + }); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Registers a {@link ExistenceFilterMismatchListener} to be notified when an existence filter |
| 73 | + * mismatch occurs in the Watch listen stream. |
| 74 | + * |
| 75 | + * <p>The relative order in which callbacks are notified is unspecified; do not rely on any |
| 76 | + * particular ordering. If a given callback is registered multiple times then it will be notified |
| 77 | + * multiple times, once per registration. |
| 78 | + * |
| 79 | + * <p>The thread on which the callback occurs is unspecified; listeners should perform their work |
| 80 | + * as quickly as possible and return to avoid blocking any critical work. In particular, the |
| 81 | + * listener callbacks should <em>not</em> block or perform long-running operations. Listener |
| 82 | + * callbacks can occur concurrently with other callbacks on the same and other listeners. |
| 83 | + * |
| 84 | + * @param listener the listener to register. |
| 85 | + * @return an object that unregisters the given listener via its {@link |
| 86 | + * ListenerRegistration#remove} method; only the first unregistration request does anything; |
| 87 | + * all subsequent requests do nothing. |
| 88 | + */ |
| 89 | + ListenerRegistration addExistenceFilterMismatchListener( |
| 90 | + @NonNull ExistenceFilterMismatchListener listener) { |
| 91 | + checkNotNull(listener, "a null listener is not allowed"); |
| 92 | + |
| 93 | + AtomicReference<ExistenceFilterMismatchListener> listenerRef = new AtomicReference<>(listener); |
| 94 | + existenceFilterMismatchListeners.add(listenerRef); |
| 95 | + |
| 96 | + return () -> { |
| 97 | + listenerRef.set(null); |
| 98 | + existenceFilterMismatchListeners.remove(listenerRef); |
| 99 | + }; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Implementations of this interface can be registered with {@link |
| 104 | + * #addExistenceFilterMismatchListener}. |
| 105 | + */ |
| 106 | + interface ExistenceFilterMismatchListener { |
| 107 | + |
| 108 | + /** |
| 109 | + * Invoked when an existence filter mismatch occurs. |
| 110 | + * |
| 111 | + * @param info information about the existence filter mismatch. |
| 112 | + */ |
| 113 | + @AnyThread |
| 114 | + void onExistenceFilterMismatch(@NonNull ExistenceFilterMismatchInfo info); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Information about an existence filter mismatch, as specified to listeners registered with |
| 119 | + * {@link #addExistenceFilterMismatchListener}. |
| 120 | + */ |
| 121 | + @AutoValue |
| 122 | + abstract static class ExistenceFilterMismatchInfo { |
| 123 | + |
| 124 | + /** |
| 125 | + * Creates and returns a new instance of {@link ExistenceFilterMismatchInfo} with the given |
| 126 | + * values. |
| 127 | + */ |
| 128 | + static ExistenceFilterMismatchInfo create(int localCacheCount, int existenceFilterCount) { |
| 129 | + return new AutoValue_TestingHooks_ExistenceFilterMismatchInfo( |
| 130 | + localCacheCount, existenceFilterCount); |
| 131 | + } |
| 132 | + |
| 133 | + /** Returns the number of documents that matched the query in the local cache. */ |
| 134 | + abstract int localCacheCount(); |
| 135 | + |
| 136 | + /** |
| 137 | + * Returns the number of documents that matched the query on the server, as specified in the |
| 138 | + * ExistenceFilter message's `count` field. |
| 139 | + */ |
| 140 | + abstract int existenceFilterCount(); |
| 141 | + |
| 142 | + /** |
| 143 | + * Convenience method to create and return a new instance of {@link ExistenceFilterMismatchInfo} |
| 144 | + * with the values taken from the given arguments. |
| 145 | + */ |
| 146 | + static ExistenceFilterMismatchInfo from(int localCacheCount, ExistenceFilter existenceFilter) { |
| 147 | + return create(localCacheCount, existenceFilter.getCount()); |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments