|
| 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 android.os.SystemClock; |
| 20 | +import androidx.annotation.AnyThread; |
| 21 | +import androidx.annotation.NonNull; |
| 22 | +import androidx.annotation.Nullable; |
| 23 | +import com.google.firebase.firestore.ListenerRegistration; |
| 24 | +import java.util.ArrayList; |
| 25 | + |
| 26 | +/** |
| 27 | + * Provides access to the {@link WatchChangeAggregatorTestingHooks} class and its methods. |
| 28 | + * |
| 29 | + * <p>The {@link WatchChangeAggregatorTestingHooks} class has default visibility, and, therefore, is |
| 30 | + * only visible to other classes declared in the same package. This class effectively "re-exports" |
| 31 | + * the functionality from {@link WatchChangeAggregatorTestingHooks} in a class with {@code public} |
| 32 | + * visibility so that tests written in other packages can access its functionality. |
| 33 | + */ |
| 34 | +public final class WatchChangeAggregatorTestingHooksAccessor { |
| 35 | + |
| 36 | + private WatchChangeAggregatorTestingHooksAccessor() {} |
| 37 | + |
| 38 | + /** @see WatchChangeAggregatorTestingHooks#addExistenceFilterMismatchListener */ |
| 39 | + public static ListenerRegistration addExistenceFilterMismatchListener( |
| 40 | + @NonNull ExistenceFilterMismatchListener listener) { |
| 41 | + checkNotNull(listener, "a null listener is not allowed"); |
| 42 | + return WatchChangeAggregatorTestingHooks.addExistenceFilterMismatchListener( |
| 43 | + new ExistenceFilterMismatchListenerWrapper(listener)); |
| 44 | + } |
| 45 | + |
| 46 | + /** @see WatchChangeAggregatorTestingHooks.ExistenceFilterMismatchListener */ |
| 47 | + public interface ExistenceFilterMismatchListener { |
| 48 | + @AnyThread |
| 49 | + void onExistenceFilterMismatch(ExistenceFilterMismatchInfo info); |
| 50 | + } |
| 51 | + |
| 52 | + /** @see WatchChangeAggregatorTestingHooks.ExistenceFilterMismatchInfo */ |
| 53 | + public interface ExistenceFilterMismatchInfo { |
| 54 | + int localCacheCount(); |
| 55 | + |
| 56 | + int existenceFilterCount(); |
| 57 | + } |
| 58 | + |
| 59 | + private static final class ExistenceFilterMismatchInfoImpl |
| 60 | + implements ExistenceFilterMismatchInfo { |
| 61 | + |
| 62 | + private final WatchChangeAggregatorTestingHooks.ExistenceFilterMismatchInfo info; |
| 63 | + |
| 64 | + ExistenceFilterMismatchInfoImpl( |
| 65 | + @NonNull WatchChangeAggregatorTestingHooks.ExistenceFilterMismatchInfo info) { |
| 66 | + this.info = info; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public int localCacheCount() { |
| 71 | + return info.localCacheCount(); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public int existenceFilterCount() { |
| 76 | + return info.existenceFilterCount(); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private static final class ExistenceFilterMismatchListenerWrapper |
| 81 | + implements WatchChangeAggregatorTestingHooks.ExistenceFilterMismatchListener { |
| 82 | + |
| 83 | + private final ExistenceFilterMismatchListener wrappedListener; |
| 84 | + |
| 85 | + ExistenceFilterMismatchListenerWrapper( |
| 86 | + @NonNull ExistenceFilterMismatchListener listenerToWrap) { |
| 87 | + this.wrappedListener = listenerToWrap; |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public void onExistenceFilterMismatch( |
| 92 | + WatchChangeAggregatorTestingHooks.ExistenceFilterMismatchInfo info) { |
| 93 | + this.wrappedListener.onExistenceFilterMismatch(new ExistenceFilterMismatchInfoImpl(info)); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + public static final class ExistenceFilterMismatchAccumulator { |
| 98 | + |
| 99 | + private ExistenceFilterMismatchListenerImpl listener; |
| 100 | + private ListenerRegistration listenerRegistration = null; |
| 101 | + |
| 102 | + /** Registers the accumulator to begin listening for existence filter mismatches. */ |
| 103 | + public synchronized void register() { |
| 104 | + if (listener != null) { |
| 105 | + throw new IllegalStateException("already registered"); |
| 106 | + } |
| 107 | + listener = new ExistenceFilterMismatchListenerImpl(); |
| 108 | + listenerRegistration = |
| 109 | + WatchChangeAggregatorTestingHooksAccessor.addExistenceFilterMismatchListener(listener); |
| 110 | + } |
| 111 | + |
| 112 | + /** Unregisters the accumulator from listening for existence filter mismatches. */ |
| 113 | + public synchronized void unregister() { |
| 114 | + if (listener == null) { |
| 115 | + return; |
| 116 | + } |
| 117 | + listenerRegistration.remove(); |
| 118 | + listenerRegistration = null; |
| 119 | + listener = null; |
| 120 | + } |
| 121 | + |
| 122 | + @Nullable |
| 123 | + public WatchChangeAggregatorTestingHooksAccessor.ExistenceFilterMismatchInfo |
| 124 | + waitForExistenceFilterMismatch(long timeoutMillis) throws InterruptedException { |
| 125 | + ExistenceFilterMismatchListenerImpl capturedListener; |
| 126 | + synchronized (this) { |
| 127 | + capturedListener = listener; |
| 128 | + } |
| 129 | + if (capturedListener == null) { |
| 130 | + throw new IllegalStateException( |
| 131 | + "must be registered before waiting for an existence filter mismatch"); |
| 132 | + } |
| 133 | + return capturedListener.waitForExistenceFilterMismatch(timeoutMillis); |
| 134 | + } |
| 135 | + |
| 136 | + private static final class ExistenceFilterMismatchListenerImpl |
| 137 | + implements WatchChangeAggregatorTestingHooksAccessor.ExistenceFilterMismatchListener { |
| 138 | + |
| 139 | + private final ArrayList<ExistenceFilterMismatchInfo> existenceFilterMismatches = |
| 140 | + new ArrayList<>(); |
| 141 | + |
| 142 | + @Override |
| 143 | + public void onExistenceFilterMismatch( |
| 144 | + WatchChangeAggregatorTestingHooksAccessor.ExistenceFilterMismatchInfo info) { |
| 145 | + synchronized (existenceFilterMismatches) { |
| 146 | + existenceFilterMismatches.add(info); |
| 147 | + existenceFilterMismatches.notifyAll(); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + @Nullable |
| 152 | + WatchChangeAggregatorTestingHooksAccessor.ExistenceFilterMismatchInfo |
| 153 | + waitForExistenceFilterMismatch(long timeoutMillis) throws InterruptedException { |
| 154 | + if (timeoutMillis <= 0) { |
| 155 | + throw new IllegalArgumentException("invalid timeout: " + timeoutMillis); |
| 156 | + } |
| 157 | + synchronized (existenceFilterMismatches) { |
| 158 | + long endTimeMillis = SystemClock.uptimeMillis() + timeoutMillis; |
| 159 | + while (true) { |
| 160 | + if (existenceFilterMismatches.size() > 0) { |
| 161 | + return existenceFilterMismatches.remove(0); |
| 162 | + } |
| 163 | + long currentWaitMillis = endTimeMillis - SystemClock.uptimeMillis(); |
| 164 | + if (currentWaitMillis <= 0) { |
| 165 | + return null; |
| 166 | + } |
| 167 | + existenceFilterMismatches.wait(currentWaitMillis); |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | +} |
0 commit comments