summaryrefslogtreecommitdiff
path: root/adservices/tests/unittest/service-core/src/com/android/adservices/service/adselection/FrequencyCapFilteringE2ETest.java
blob: 9a1f32df63a77915a29453ee12fda5097c24fd79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * 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.android.adservices.service.adselection;

import static com.android.adservices.service.stats.AdServicesStatsLog.AD_SERVICES_API_CALLED__API_NAME__UPDATE_AD_COUNTER_HISTOGRAM;
import static com.android.dx.mockito.inline.extended.ExtendedMockito.any;
import static com.android.dx.mockito.inline.extended.ExtendedMockito.anyInt;
import static com.android.dx.mockito.inline.extended.ExtendedMockito.doNothing;
import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn;
import static com.android.dx.mockito.inline.extended.ExtendedMockito.times;
import static com.android.dx.mockito.inline.extended.ExtendedMockito.verify;
import static com.android.dx.mockito.inline.extended.ExtendedMockito.verifyNoMoreInteractions;

import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;

import android.adservices.adselection.AdSelectionConfig;
import android.adservices.adselection.AdSelectionConfigFixture;
import android.adservices.adselection.AdSelectionInput;
import android.adservices.adselection.CustomAudienceSignalsFixture;
import android.adservices.adselection.UpdateAdCounterHistogramInput;
import android.adservices.common.AdDataFixture;
import android.adservices.common.AdFilters;
import android.adservices.common.AdSelectionSignals;
import android.adservices.common.AdServicesStatusUtils;
import android.adservices.common.CallerMetadata;
import android.adservices.common.CallingAppUidSupplierProcessImpl;
import android.adservices.common.CommonFixture;
import android.adservices.common.FrequencyCapFilters;
import android.adservices.common.KeyedFrequencyCap;
import android.adservices.common.KeyedFrequencyCapFixture;
import android.content.Context;
import android.net.Uri;

import androidx.room.Room;
import androidx.test.core.app.ApplicationProvider;

import com.android.adservices.common.AdServicesExtendedMockitoTestCase;
import com.android.adservices.common.DBAdDataFixture;
import com.android.adservices.concurrency.AdServicesExecutors;
import com.android.adservices.customaudience.DBCustomAudienceFixture;
import com.android.adservices.data.DbTestUtil;
import com.android.adservices.data.adselection.AdSelectionDatabase;
import com.android.adservices.data.adselection.AdSelectionDebugReportDao;
import com.android.adservices.data.adselection.AdSelectionEntryDao;
import com.android.adservices.data.adselection.AppInstallDao;
import com.android.adservices.data.adselection.DBAdSelection;
import com.android.adservices.data.adselection.DBAdSelectionHistogramInfo;
import com.android.adservices.data.adselection.FrequencyCapDao;
import com.android.adservices.data.adselection.SharedStorageDatabase;
import com.android.adservices.data.common.DBAdData;
import com.android.adservices.data.customaudience.CustomAudienceDao;
import com.android.adservices.data.customaudience.CustomAudienceDatabase;
import com.android.adservices.data.customaudience.DBCustomAudience;
import com.android.adservices.data.encryptionkey.EncryptionKeyDao;
import com.android.adservices.data.enrollment.EnrollmentDao;
import com.android.adservices.data.signals.EncodedPayloadDao;
import com.android.adservices.data.signals.ProtectedSignalsDatabase;
import com.android.adservices.service.Flags;
import com.android.adservices.service.FlagsFactory;
import com.android.adservices.service.adselection.AdSelectionE2ETest.AdSelectionTestCallback;
import com.android.adservices.service.adselection.UpdateAdCounterHistogramWorkerTest.FlagsOverridingAdFiltering;
import com.android.adservices.service.adselection.UpdateAdCounterHistogramWorkerTest.UpdateAdCounterHistogramTestCallback;
import com.android.adservices.service.adselection.encryption.ObliviousHttpEncryptor;
import com.android.adservices.service.common.AdSelectionServiceFilter;
import com.android.adservices.service.common.AppImportanceFilter;
import com.android.adservices.service.common.FledgeAllowListsFilter;
import com.android.adservices.service.common.FledgeAuthorizationFilter;
import com.android.adservices.service.common.Throttler;
import com.android.adservices.service.common.cache.HttpCache;
import com.android.adservices.service.common.httpclient.AdServicesHttpClientRequest;
import com.android.adservices.service.common.httpclient.AdServicesHttpClientResponse;
import com.android.adservices.service.common.httpclient.AdServicesHttpsClient;
import com.android.adservices.service.consent.ConsentManager;
import com.android.adservices.service.devapi.DevContext;
import com.android.adservices.service.devapi.DevContextFilter;
import com.android.adservices.service.js.JSScriptEngine;
import com.android.adservices.service.stats.AdServicesLogger;
import com.android.adservices.service.stats.Clock;
import com.android.dx.mockito.inline.extended.ExtendedMockito;
import com.android.modules.utils.testing.ExtendedMockitoRule.SpyStatic;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.util.concurrent.Futures;

import org.junit.Assume;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.Spy;

import java.io.File;
import java.time.Duration;
import java.util.Arrays;
import java.util.Collections;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@SpyStatic(FlagsFactory.class)
public final class FrequencyCapFilteringE2ETest extends AdServicesExtendedMockitoTestCase {
    private static final int CALLBACK_WAIT_MS = 500;
    private static final int SELECT_ADS_CALLBACK_WAIT_MS = 10_000;
    private static final long AD_SELECTION_ID_BUYER_1 = 20;
    private static final long AD_SELECTION_ID_BUYER_2 = 21;

    private static final DBAdSelection EXISTING_PREVIOUS_AD_SELECTION_BUYER_1 =
            new DBAdSelection.Builder()
                    .setAdSelectionId(AD_SELECTION_ID_BUYER_1)
                    .setCustomAudienceSignals(CustomAudienceSignalsFixture.aCustomAudienceSignals())
                    .setBuyerContextualSignals(AdSelectionSignals.EMPTY.toString())
                    .setBiddingLogicUri(
                            CommonFixture.getUri(CommonFixture.VALID_BUYER_1, "/bidding"))
                    .setWinningAdRenderUri(
                            CommonFixture.getUri(CommonFixture.VALID_BUYER_1, "/ad1"))
                    .setWinningAdBid(0.5)
                    .setCreationTimestamp(CommonFixture.FIXED_NOW_TRUNCATED_TO_MILLI)
                    .setCallerPackageName(CommonFixture.TEST_PACKAGE_NAME)
                    .setAdCounterIntKeys(AdDataFixture.getAdCounterKeys())
                    .build();

    private static final DBAdSelection EXISTING_PREVIOUS_AD_SELECTION_BUYER_2 =
            new DBAdSelection.Builder()
                    .setAdSelectionId(AD_SELECTION_ID_BUYER_2)
                    .setCustomAudienceSignals(
                            CustomAudienceSignalsFixture.aCustomAudienceSignalsBuilder()
                                    .setBuyer(CommonFixture.VALID_BUYER_2)
                                    .build())
                    .setBuyerContextualSignals(AdSelectionSignals.EMPTY.toString())
                    .setBiddingLogicUri(
                            CommonFixture.getUri(CommonFixture.VALID_BUYER_2, "/bidding"))
                    .setWinningAdRenderUri(
                            CommonFixture.getUri(CommonFixture.VALID_BUYER_2, "/ad1"))
                    .setWinningAdBid(0.5)
                    .setCreationTimestamp(CommonFixture.FIXED_NOW_TRUNCATED_TO_MILLI)
                    .setCallerPackageName(CommonFixture.TEST_PACKAGE_NAME)
                    .setAdCounterIntKeys(AdDataFixture.getAdCounterKeys())
                    .build();

    private static final ImmutableList<KeyedFrequencyCap> CLICK_FILTERS =
            ImmutableList.of(
                    new KeyedFrequencyCap.Builder(
                                    KeyedFrequencyCapFixture.KEY1,
                                    /* maxCount= */ 1,
                                    Duration.ofSeconds(5))
                            .build());

    private static final DBAdData AD_WITH_FILTER =
            DBAdDataFixture.getValidDbAdDataNoFiltersBuilder()
                    .setMetadata("{\"result\":1}")
                    .setAdCounterKeys(AdDataFixture.getAdCounterKeys())
                    .setAdFilters(
                            new AdFilters.Builder()
                                    .setFrequencyCapFilters(
                                            new FrequencyCapFilters.Builder()
                                                    .setKeyedFrequencyCapsForClickEvents(
                                                            CLICK_FILTERS)
                                                    .build())
                                    .build())
                    .build();
    @Spy private final Context mContextSpy = ApplicationProvider.getApplicationContext();
    @Mock private AdServicesHttpsClient mAdServicesHttpsClientMock;
    @Mock private HttpCache mHttpCacheMock;
    @Mock private DevContextFilter mDevContextFilterMock;
    @Mock private AdServicesLogger mAdServicesLoggerMock;
    @Mock private AdSelectionServiceFilter mServiceFilterMock;
    @Mock private ConsentManager mConsentManagerMock;
    @Mock private CallerMetadata mCallerMetadataMock;
    @Mock private File mAdSelectionDbFileMock;
    @Mock private AppImportanceFilter mAppImportanceFilterMock;
    @Mock private FledgeAllowListsFilter mFledgeAllowListsFilterMock;

    private AdSelectionEntryDao mAdSelectionEntryDao;
    private CustomAudienceDao mCustomAudienceDao;
    private EncodedPayloadDao mEncodedPayloadDao;
    private AppInstallDao mAppInstallDao;
    private FrequencyCapDao mFrequencyCapDaoSpy;
    private EncryptionKeyDao mEncryptionKeyDao;
    private EnrollmentDao mEnrollmentDao;
    private ExecutorService mLightweightExecutorService;
    private ExecutorService mBackgroundExecutorService;
    private ScheduledThreadPoolExecutor mScheduledExecutor;

    private FledgeAuthorizationFilter mFledgeAuthorizationFilterSpy;
    private AdFilteringFeatureFactory mAdFilteringFeatureFactory;

    private AdSelectionServiceImpl mAdSelectionServiceImpl;
    private UpdateAdCounterHistogramInput mInputParams;
    @Mock private ObliviousHttpEncryptor mObliviousHttpEncryptor;
    @Mock private AdSelectionDebugReportDao mAdSelectionDebugReportDao;
    @Mock private AdIdFetcher mAdIdFetcher;

    @Before
    public void setup() {
        mAdSelectionEntryDao =
                Room.inMemoryDatabaseBuilder(mContextSpy, AdSelectionDatabase.class)
                        .build()
                        .adSelectionEntryDao();
        mCustomAudienceDao =
                Room.inMemoryDatabaseBuilder(mContextSpy, CustomAudienceDatabase.class)
                        .addTypeConverter(new DBCustomAudience.Converters(true, true))
                        .build()
                        .customAudienceDao();
        mEncodedPayloadDao =
                Room.inMemoryDatabaseBuilder(mContextSpy, ProtectedSignalsDatabase.class)
                        .build()
                        .getEncodedPayloadDao();
        mAppInstallDao =
                Room.inMemoryDatabaseBuilder(mContextSpy, SharedStorageDatabase.class)
                        .build()
                        .appInstallDao();
        mFrequencyCapDaoSpy =
                Mockito.spy(
                        Room.inMemoryDatabaseBuilder(mContextSpy, SharedStorageDatabase.class)
                                .build()
                                .frequencyCapDao());

        Flags flagsEnablingAdFiltering = new FlagsOverridingAdFiltering(true);
        doReturn(flagsEnablingAdFiltering).when(FlagsFactory::getFlags);

        mEncryptionKeyDao = EncryptionKeyDao.getInstance(mContextSpy);
        mEnrollmentDao = EnrollmentDao.getInstance(mContextSpy);
        mLightweightExecutorService = AdServicesExecutors.getLightWeightExecutor();
        mBackgroundExecutorService = AdServicesExecutors.getBackgroundExecutor();
        mScheduledExecutor = AdServicesExecutors.getScheduler();

        mFledgeAuthorizationFilterSpy =
                ExtendedMockito.spy(
                        new FledgeAuthorizationFilter(
                                mContextSpy.getPackageManager(),
                                new EnrollmentDao(
                                        mContextSpy,
                                        DbTestUtil.getSharedDbHelperForTest(),
                                        flagsEnablingAdFiltering),
                                mAdServicesLoggerMock));

        mAdFilteringFeatureFactory =
                new AdFilteringFeatureFactory(
                        mAppInstallDao, mFrequencyCapDaoSpy, flagsEnablingAdFiltering);

        mAdSelectionServiceImpl =
                new AdSelectionServiceImpl(
                        mAdSelectionEntryDao,
                        mAppInstallDao,
                        mCustomAudienceDao,
                        mEncodedPayloadDao,
                        mFrequencyCapDaoSpy,
                        mEncryptionKeyDao,
                        mEnrollmentDao,
                        mAdServicesHttpsClientMock,
                        mDevContextFilterMock,
                        mLightweightExecutorService,
                        mBackgroundExecutorService,
                        mScheduledExecutor,
                        mContextSpy,
                        mAdServicesLoggerMock,
                        flagsEnablingAdFiltering,
                        CallingAppUidSupplierProcessImpl.create(),
                        mFledgeAuthorizationFilterSpy,
                        mServiceFilterMock,
                        mAdFilteringFeatureFactory,
                        mConsentManagerMock,
                        mObliviousHttpEncryptor,
                        mAdSelectionDebugReportDao,
                        mAdIdFetcher,
                        false);

        mInputParams =
                new UpdateAdCounterHistogramInput.Builder(
                                AD_SELECTION_ID_BUYER_1,
                                FrequencyCapFilters.AD_EVENT_TYPE_CLICK,
                                CommonFixture.VALID_BUYER_1,
                                CommonFixture.TEST_PACKAGE_NAME)
                        .build();

        // Required stub for Custom Audience DB persistence
        extendedMockito.mockGetFlags(flagsEnablingAdFiltering);

        // Required stub for Ad Selection call
        doReturn(DevContext.createForDevOptionsDisabled())
                .when(mDevContextFilterMock)
                .createDevContext();

        // Required stubs for Ad Selection loggers
        doReturn(Clock.SYSTEM_CLOCK.elapsedRealtime() - 100)
                .when(mCallerMetadataMock)
                .getBinderElapsedTimestamp();
        doReturn(mAdSelectionDbFileMock).when(mContextSpy).getDatabasePath(any());
        doReturn(10L).when(mAdSelectionDbFileMock).length();

        // Required stubs for Ad Selection signals/logic fetching
        doReturn(mHttpCacheMock).when(mAdServicesHttpsClientMock).getAssociatedCache();
        doReturn(
                        // Bidding signals
                        Futures.immediateFuture(
                                AdServicesHttpClientResponse.builder()
                                        .setResponseBody("{}")
                                        .build()))
                .when(mAdServicesHttpsClientMock)
                .fetchPayload(any(Uri.class), any(ImmutableSet.class), any(DevContext.class));
        doReturn(
                        // Scoring signals
                        Futures.immediateFuture(
                                AdServicesHttpClientResponse.builder()
                                        .setResponseBody("{}")
                                        .build()))
                .when(mAdServicesHttpsClientMock)
                .fetchPayload(any(Uri.class), any(DevContext.class));
        doReturn(
                        // Bidding logic
                        Futures.immediateFuture(
                                AdServicesHttpClientResponse.builder()
                                        .setResponseBody(
                                                AdSelectionE2ETest.READ_BID_FROM_AD_METADATA_JS)
                                        .build()))
                .doReturn(
                        // Scoring logic
                        Futures.immediateFuture(
                                AdServicesHttpClientResponse.builder()
                                        .setResponseBody(AdSelectionE2ETest.USE_BID_AS_SCORE_JS)
                                        .build()))
                .when(mAdServicesHttpsClientMock)
                .fetchPayload(any(AdServicesHttpClientRequest.class));
    }

    @Test
    public void testUpdateHistogramMissingAdSelectionDoesNothing() throws InterruptedException {
        UpdateAdCounterHistogramTestCallback callback = callUpdateAdCounterHistogram(mInputParams);

        assertWithMessage("Callback failed, was: %s", callback).that(callback.mIsSuccess).isTrue();

        verifyNoMoreInteractions(mFrequencyCapDaoSpy);
    }

    @Test
    public void testUpdateHistogramForAdSelectionAddsHistogramEvents() throws InterruptedException {
        mAdSelectionEntryDao.persistAdSelection(EXISTING_PREVIOUS_AD_SELECTION_BUYER_1);

        UpdateAdCounterHistogramTestCallback callback = callUpdateAdCounterHistogram(mInputParams);

        assertWithMessage("Callback failed, was: %s", callback).that(callback.mIsSuccess).isTrue();

        verify(mFrequencyCapDaoSpy, times(AdDataFixture.getAdCounterKeys().size()))
                .insertHistogramEvent(any(), anyInt(), anyInt(), anyInt(), anyInt());

        for (Integer key : AdDataFixture.getAdCounterKeys()) {
            assertThat(
                            mFrequencyCapDaoSpy.getNumEventsForBuyerAfterTime(
                                    key,
                                    CommonFixture.VALID_BUYER_1,
                                    mInputParams.getAdEventType(),
                                    CommonFixture.FIXED_EARLIER_ONE_DAY))
                    .isEqualTo(1);
        }
    }

    @Test
    public void testUpdateHistogramForAdSelectionFromOtherAppDoesNotAddHistogramEvents()
            throws InterruptedException {
        // Bypass the permission check since it's enforced before the package name check
        doNothing()
                .when(mFledgeAuthorizationFilterSpy)
                .assertAppDeclaredCustomAudiencePermission(
                        mContextSpy,
                        CommonFixture.TEST_PACKAGE_NAME_1,
                        AD_SERVICES_API_CALLED__API_NAME__UPDATE_AD_COUNTER_HISTOGRAM);

        mAdSelectionEntryDao.persistAdSelection(EXISTING_PREVIOUS_AD_SELECTION_BUYER_1);

        // Caller does not match previous ad selection
        UpdateAdCounterHistogramInput inputParamsOtherPackage =
                new UpdateAdCounterHistogramInput.Builder(
                                AD_SELECTION_ID_BUYER_1,
                                FrequencyCapFilters.AD_EVENT_TYPE_CLICK,
                                CommonFixture.VALID_BUYER_1,
                                CommonFixture.TEST_PACKAGE_NAME_1)
                        .build();

        UpdateAdCounterHistogramTestCallback callback =
                callUpdateAdCounterHistogram(inputParamsOtherPackage);

        assertWithMessage("Callback failed, was: %s", callback).that(callback.mIsSuccess).isTrue();

        verifyNoMoreInteractions(mFrequencyCapDaoSpy);

        verify(mFledgeAuthorizationFilterSpy)
                .assertAppDeclaredCustomAudiencePermission(
                        mContextSpy,
                        CommonFixture.TEST_PACKAGE_NAME_1,
                        AD_SERVICES_API_CALLED__API_NAME__UPDATE_AD_COUNTER_HISTOGRAM);
    }

    @Test
    public void testUpdateHistogramDisabledFeatureFlagNotifiesError() throws InterruptedException {
        Flags flagsWithDisabledAdFiltering = new FlagsOverridingAdFiltering(false);

        mAdFilteringFeatureFactory =
                new AdFilteringFeatureFactory(
                        mAppInstallDao, mFrequencyCapDaoSpy, flagsWithDisabledAdFiltering);

        mAdSelectionServiceImpl =
                new AdSelectionServiceImpl(
                        mAdSelectionEntryDao,
                        mAppInstallDao,
                        mCustomAudienceDao,
                        mEncodedPayloadDao,
                        mFrequencyCapDaoSpy,
                        mEncryptionKeyDao,
                        mEnrollmentDao,
                        mAdServicesHttpsClientMock,
                        mDevContextFilterMock,
                        mLightweightExecutorService,
                        mBackgroundExecutorService,
                        mScheduledExecutor,
                        mContextSpy,
                        mAdServicesLoggerMock,
                        flagsWithDisabledAdFiltering,
                        CallingAppUidSupplierProcessImpl.create(),
                        mFledgeAuthorizationFilterSpy,
                        mServiceFilterMock,
                        mAdFilteringFeatureFactory,
                        mConsentManagerMock,
                        mObliviousHttpEncryptor,
                        mAdSelectionDebugReportDao,
                        mAdIdFetcher,
                        false);

        UpdateAdCounterHistogramTestCallback callback = callUpdateAdCounterHistogram(mInputParams);

        assertThat(callback.mIsSuccess).isFalse();
        assertThat(callback.mFledgeErrorResponse.getStatusCode())
                .isEqualTo(AdServicesStatusUtils.STATUS_INTERNAL_ERROR);

        verifyNoMoreInteractions(mFrequencyCapDaoSpy);
    }

    @Test
    public void testUpdateHistogramExceedingRateLimitNotifiesError() throws InterruptedException {
        class FlagsWithLowRateLimit implements Flags {
            @Override
            public boolean getFledgeAdSelectionFilteringEnabled() {
                return true;
            }

            @Override
            public float getSdkRequestPermitsPerSecond() {
                return 1f;
            }
        }

        Throttler.destroyExistingThrottler();

        doNothing()
                .when(mFledgeAuthorizationFilterSpy)
                .assertAdTechAllowed(any(), any(), any(), anyInt());

        try {
            Flags flagsWithLowRateLimit = new FlagsWithLowRateLimit();

            mAdFilteringFeatureFactory =
                    new AdFilteringFeatureFactory(
                            mAppInstallDao, mFrequencyCapDaoSpy, flagsWithLowRateLimit);

            mAdSelectionServiceImpl =
                    new AdSelectionServiceImpl(
                            mAdSelectionEntryDao,
                            mAppInstallDao,
                            mCustomAudienceDao,
                            mEncodedPayloadDao,
                            mFrequencyCapDaoSpy,
                            mEncryptionKeyDao,
                            mEnrollmentDao,
                            mAdServicesHttpsClientMock,
                            mDevContextFilterMock,
                            mLightweightExecutorService,
                            mBackgroundExecutorService,
                            mScheduledExecutor,
                            mContextSpy,
                            mAdServicesLoggerMock,
                            flagsWithLowRateLimit,
                            CallingAppUidSupplierProcessImpl.create(),
                            mFledgeAuthorizationFilterSpy,
                            new AdSelectionServiceFilter(
                                    mContextSpy,
                                    mConsentManagerMock,
                                    flagsWithLowRateLimit,
                                    mAppImportanceFilterMock,
                                    mFledgeAuthorizationFilterSpy,
                                    mFledgeAllowListsFilterMock,
                                    Throttler.getInstance(flagsWithLowRateLimit)),
                            mAdFilteringFeatureFactory,
                            mConsentManagerMock,
                            mObliviousHttpEncryptor,
                            mAdSelectionDebugReportDao,
                            mAdIdFetcher,
                            false);

            UpdateAdCounterHistogramTestCallback callback =
                    callUpdateAdCounterHistogram(mInputParams);

            assertWithMessage("Callback failed, was: %s", callback)
                    .that(callback.mIsSuccess)
                    .isTrue();

            // Call again within the rate limit
            callback = callUpdateAdCounterHistogram(mInputParams);

            assertThat(callback.mIsSuccess).isFalse();
            assertThat(callback.mFledgeErrorResponse.getStatusCode())
                    .isEqualTo(AdServicesStatusUtils.STATUS_RATE_LIMIT_REACHED);

            verifyNoMoreInteractions(mFrequencyCapDaoSpy);
        } finally {
            Throttler.destroyExistingThrottler();
        }
    }

    @Test
    public void testAdSelectionPersistsAdCounterKeys() throws InterruptedException {
        // The JS Sandbox availability depends on an external component (the system webview) being
        // higher than a certain minimum version.
        Assume.assumeTrue(JSScriptEngine.AvailabilityChecker.isJSSandboxAvailable());

        mCustomAudienceDao.insertOrOverwriteCustomAudience(
                DBCustomAudienceFixture.getValidBuilderByBuyerNoFilters(CommonFixture.VALID_BUYER_1)
                        .setAds(Collections.singletonList(AD_WITH_FILTER))
                        .build(),
                CommonFixture.getUri(CommonFixture.VALID_BUYER_1, "/update"),
                /*debuggable=*/ false);

        AdSelectionTestCallback adSelectionCallback = callSelectAds();

        assertWithMessage("Callback failed, was: %s", adSelectionCallback)
                .that(adSelectionCallback.mIsSuccess)
                .isTrue();
        assertWithMessage(
                        "Unexpected winning ad, ad selection responded with: %s",
                        adSelectionCallback.mAdSelectionResponse)
                .that(adSelectionCallback.mAdSelectionResponse.getRenderUri())
                .isEqualTo(AD_WITH_FILTER.getRenderUri());

        DBAdSelectionHistogramInfo histogramInfo =
                mAdSelectionEntryDao.getAdSelectionHistogramInfoInOnDeviceTable(
                        adSelectionCallback.mAdSelectionResponse.getAdSelectionId(),
                        CommonFixture.TEST_PACKAGE_NAME);
        assertThat(histogramInfo).isNotNull();
        assertThat(histogramInfo.getBuyer()).isEqualTo(CommonFixture.VALID_BUYER_1);
        assertThat(histogramInfo.getAdCounterKeys())
                .containsExactlyElementsIn(AdDataFixture.getAdCounterKeys());
    }

    @Test
    public void testEmptyHistogramDoesNotFilterAds() throws InterruptedException {
        // The JS Sandbox availability depends on an external component (the system webview) being
        // higher than a certain minimum version.
        Assume.assumeTrue(JSScriptEngine.AvailabilityChecker.isJSSandboxAvailable());

        mCustomAudienceDao.insertOrOverwriteCustomAudience(
                DBCustomAudienceFixture.getValidBuilderByBuyerNoFilters(CommonFixture.VALID_BUYER_1)
                        .setAds(Arrays.asList(AD_WITH_FILTER))
                        .build(),
                CommonFixture.getUri(CommonFixture.VALID_BUYER_1, "/update"),
                /*debuggable=*/ false);

        AdSelectionTestCallback callback = callSelectAds();

        assertWithMessage("Callback failed, was: %s", callback).that(callback.mIsSuccess).isTrue();
        assertWithMessage(
                        "Unexpected winning ad, ad selection responded with: %s",
                        callback.mAdSelectionResponse)
                .that(callback.mAdSelectionResponse.getRenderUri())
                .isEqualTo(AD_WITH_FILTER.getRenderUri());
    }

    @Test
    public void testUpdatedHistogramFiltersAdsForBuyerWithinInterval() throws InterruptedException {
        // The JS Sandbox availability depends on an external component (the system webview) being
        // higher than a certain minimum version.
        Assume.assumeTrue(JSScriptEngine.AvailabilityChecker.isJSSandboxAvailable());

        // Persist histogram events
        mAdSelectionEntryDao.persistAdSelection(EXISTING_PREVIOUS_AD_SELECTION_BUYER_1);

        UpdateAdCounterHistogramTestCallback updateHistogramCallback =
                callUpdateAdCounterHistogram(mInputParams);

        assertWithMessage("Callback failed, was: %s", updateHistogramCallback)
                .that(updateHistogramCallback.mIsSuccess)
                .isTrue();

        // Run ad selection for buyer
        mCustomAudienceDao.insertOrOverwriteCustomAudience(
                DBCustomAudienceFixture.getValidBuilderByBuyerNoFilters(CommonFixture.VALID_BUYER_1)
                        .setAds(Arrays.asList(AD_WITH_FILTER))
                        .build(),
                CommonFixture.getUri(CommonFixture.VALID_BUYER_1, "/update"),
                /*debuggable=*/ false);

        AdSelectionTestCallback adSelectionCallback = callSelectAds();

        assertWithMessage("Callback succeeded unexpectedly")
                .that(adSelectionCallback.mIsSuccess)
                .isFalse();
        assertWithMessage(
                        "Unexpected error response, ad selection responded with: %s",
                        adSelectionCallback.mFledgeErrorResponse)
                .that(adSelectionCallback.mFledgeErrorResponse.getStatusCode())
                .isEqualTo(AdServicesStatusUtils.STATUS_INTERNAL_ERROR);
        assertWithMessage(
                        "Unexpected error response, ad selection responded with: %s",
                        adSelectionCallback.mFledgeErrorResponse)
                .that(adSelectionCallback.mFledgeErrorResponse.getErrorMessage())
                .contains("No valid bids");
    }

    @Test
    public void testUpdatedHistogramDoesNotFilterAdsForBuyerOutsideInterval()
            throws InterruptedException {
        // The JS Sandbox availability depends on an external component (the system webview) being
        // higher than a certain minimum version.
        Assume.assumeTrue(JSScriptEngine.AvailabilityChecker.isJSSandboxAvailable());

        // Persist histogram events
        mAdSelectionEntryDao.persistAdSelection(EXISTING_PREVIOUS_AD_SELECTION_BUYER_1);

        UpdateAdCounterHistogramTestCallback updateHistogramCallback =
                callUpdateAdCounterHistogram(mInputParams);

        assertWithMessage("Callback failed, was: %s", updateHistogramCallback)
                .that(updateHistogramCallback.mIsSuccess)
                .isTrue();

        // Frequency cap intervals are truncated to seconds, so the test must wait so that the
        // ad filter no longer matches the events in the histogram table
        Thread.sleep(6000);

        // Run ad selection for buyer
        mCustomAudienceDao.insertOrOverwriteCustomAudience(
                DBCustomAudienceFixture.getValidBuilderByBuyerNoFilters(CommonFixture.VALID_BUYER_1)
                        .setAds(Arrays.asList(AD_WITH_FILTER))
                        .build(),
                CommonFixture.getUri(CommonFixture.VALID_BUYER_1, "/update"),
                /*debuggable=*/ false);

        AdSelectionTestCallback adSelectionCallback = callSelectAds();

        assertWithMessage("Callback failed, was: %s", adSelectionCallback)
                .that(adSelectionCallback.mIsSuccess)
                .isTrue();
        assertWithMessage(
                        "Unexpected winning ad, ad selection responded with: %s",
                        adSelectionCallback.mAdSelectionResponse)
                .that(adSelectionCallback.mAdSelectionResponse.getRenderUri())
                .isEqualTo(AD_WITH_FILTER.getRenderUri());
    }

    @Test
    public void testUpdatedHistogramDoesNotFilterAdsForOtherBuyer() throws InterruptedException {
        // The JS Sandbox availability depends on an external component (the system webview) being
        // higher than a certain minimum version.
        Assume.assumeTrue(JSScriptEngine.AvailabilityChecker.isJSSandboxAvailable());

        // Persist histogram events for BUYER_1
        mAdSelectionEntryDao.persistAdSelection(EXISTING_PREVIOUS_AD_SELECTION_BUYER_1);

        UpdateAdCounterHistogramTestCallback updateHistogramCallback =
                callUpdateAdCounterHistogram(mInputParams);

        assertWithMessage("Callback failed, was: %s", updateHistogramCallback)
                .that(updateHistogramCallback.mIsSuccess)
                .isTrue();

        // Run ad selection for BUYER_2
        mCustomAudienceDao.insertOrOverwriteCustomAudience(
                DBCustomAudienceFixture.getValidBuilderByBuyerNoFilters(CommonFixture.VALID_BUYER_2)
                        .setAds(Arrays.asList(AD_WITH_FILTER))
                        .build(),
                CommonFixture.getUri(CommonFixture.VALID_BUYER_2, "/update"),
                /*debuggable=*/ false);

        AdSelectionTestCallback adSelectionCallback = callSelectAds();

        assertWithMessage("Callback failed, was: %s", adSelectionCallback)
                .that(adSelectionCallback.mIsSuccess)
                .isTrue();
        assertWithMessage(
                        "Unexpected winning ad, ad selection responded with: %s",
                        adSelectionCallback.mAdSelectionResponse)
                .that(adSelectionCallback.mAdSelectionResponse.getRenderUri())
                .isEqualTo(AD_WITH_FILTER.getRenderUri());
    }

    @Test
    public void testUpdateHistogramBeyondMaxTotalEventCountDoesNotFilterAds()
            throws InterruptedException {
        // The JS Sandbox availability depends on an external component (the system webview) being
        // higher than a certain minimum version.
        Assume.assumeTrue(JSScriptEngine.AvailabilityChecker.isJSSandboxAvailable());

        class FlagsWithLowEventCounts extends FlagsOverridingAdFiltering implements Flags {
            @Override
            public boolean getEnforceIsolateMaxHeapSize() {
                return false;
            }

            @Override
            public boolean getFledgeAdSelectionFilteringEnabled() {
                return true;
            }

            @Override
            public int getFledgeAdCounterHistogramAbsoluteMaxTotalEventCount() {
                return 5;
            }

            @Override
            public int getFledgeAdCounterHistogramLowerMaxTotalEventCount() {
                return 1;
            }
        }

        mAdSelectionServiceImpl =
                new AdSelectionServiceImpl(
                        mAdSelectionEntryDao,
                        mAppInstallDao,
                        mCustomAudienceDao,
                        mEncodedPayloadDao,
                        mFrequencyCapDaoSpy,
                        mEncryptionKeyDao,
                        mEnrollmentDao,
                        mAdServicesHttpsClientMock,
                        mDevContextFilterMock,
                        mLightweightExecutorService,
                        mBackgroundExecutorService,
                        mScheduledExecutor,
                        mContextSpy,
                        mAdServicesLoggerMock,
                        new FlagsWithLowEventCounts(),
                        CallingAppUidSupplierProcessImpl.create(),
                        mFledgeAuthorizationFilterSpy,
                        mServiceFilterMock,
                        mAdFilteringFeatureFactory,
                        mConsentManagerMock,
                        mObliviousHttpEncryptor,
                        mAdSelectionDebugReportDao,
                        mAdIdFetcher,
                        false);

        // Persist ad selections
        mAdSelectionEntryDao.persistAdSelection(EXISTING_PREVIOUS_AD_SELECTION_BUYER_1);
        mAdSelectionEntryDao.persistAdSelection(EXISTING_PREVIOUS_AD_SELECTION_BUYER_2);

        // Update for BUYER_1 and verify ads are filtered
        // T0 - BUYER_1 events (4 events entered)
        UpdateAdCounterHistogramTestCallback updateHistogramCallback =
                callUpdateAdCounterHistogram(mInputParams);

        assertWithMessage("Callback failed, was: %s", updateHistogramCallback)
                .that(updateHistogramCallback.mIsSuccess)
                .isTrue();

        mCustomAudienceDao.insertOrOverwriteCustomAudience(
                DBCustomAudienceFixture.getValidBuilderByBuyerNoFilters(CommonFixture.VALID_BUYER_1)
                        .setAds(Arrays.asList(AD_WITH_FILTER))
                        .build(),
                CommonFixture.getUri(CommonFixture.VALID_BUYER_1, "/update"),
                /*debuggable=*/ false);

        AdSelectionTestCallback adSelectionCallback = callSelectAds();

        assertWithMessage("Callback succeeded unexpectedly")
                .that(adSelectionCallback.mIsSuccess)
                .isFalse();

        // Sleep for ensured separation of timestamps
        Thread.sleep(200);

        // Update events for BUYER_2 to fill the event table and evict the first entries for BUYER_1
        // T1 - BUYER_2 events trigger table eviction of the oldest events (which are for BUYER_1)
        UpdateAdCounterHistogramInput inputParamsForBuyer2 =
                new UpdateAdCounterHistogramInput.Builder(
                                AD_SELECTION_ID_BUYER_2,
                                FrequencyCapFilters.AD_EVENT_TYPE_CLICK,
                                CommonFixture.VALID_BUYER_2,
                                CommonFixture.TEST_PACKAGE_NAME)
                        .build();

        updateHistogramCallback = callUpdateAdCounterHistogram(inputParamsForBuyer2);

        assertWithMessage("Callback failed, was: %s", updateHistogramCallback)
                .that(updateHistogramCallback.mIsSuccess)
                .isTrue();

        // Verify that the events for BUYER_1 were evicted and the ad for BUYER_1 should now win
        adSelectionCallback = callSelectAds();

        assertWithMessage("Ad selection callback failed, was: %s", adSelectionCallback)
                .that(adSelectionCallback.mIsSuccess)
                .isTrue();
        assertWithMessage(
                        "Unexpected winning ad, ad selection responded with: %s",
                        adSelectionCallback.mAdSelectionResponse)
                .that(adSelectionCallback.mAdSelectionResponse.getRenderUri())
                .isEqualTo(AD_WITH_FILTER.getRenderUri());
    }

    @Test
    public void testUpdateHistogramBeyondMaxPerBuyerEventCountDoesNotFilterAds() throws Exception {
        // The JS Sandbox availability depends on an external component (the system webview) being
        // higher than a certain minimum version.
        Assume.assumeTrue(
                "JS Sandbox is not available",
                JSScriptEngine.AvailabilityChecker.isJSSandboxAvailable());

        final class FlagsWithLowPerBuyerEventCounts extends FlagsOverridingAdFiltering
                implements Flags {
            @Override
            public boolean getEnforceIsolateMaxHeapSize() {
                return false;
            }

            @Override
            public boolean getFledgeAdSelectionFilteringEnabled() {
                return true;
            }

            @Override
            public int getFledgeAdCounterHistogramAbsoluteMaxPerBuyerEventCount() {
                return 5;
            }

            @Override
            public int getFledgeAdCounterHistogramLowerMaxPerBuyerEventCount() {
                return 1;
            }
        }

        mAdSelectionServiceImpl =
                new AdSelectionServiceImpl(
                        mAdSelectionEntryDao,
                        mAppInstallDao,
                        mCustomAudienceDao,
                        mEncodedPayloadDao,
                        mFrequencyCapDaoSpy,
                        mEncryptionKeyDao,
                        mEnrollmentDao,
                        mAdServicesHttpsClientMock,
                        mDevContextFilterMock,
                        mLightweightExecutorService,
                        mBackgroundExecutorService,
                        mScheduledExecutor,
                        mContextSpy,
                        mAdServicesLoggerMock,
                        new FlagsWithLowPerBuyerEventCounts(),
                        CallingAppUidSupplierProcessImpl.create(),
                        mFledgeAuthorizationFilterSpy,
                        mServiceFilterMock,
                        mAdFilteringFeatureFactory,
                        mConsentManagerMock,
                        mObliviousHttpEncryptor,
                        mAdSelectionDebugReportDao,
                        mAdIdFetcher,
                        false);

        // Persist ad selections
        mAdSelectionEntryDao.persistAdSelection(EXISTING_PREVIOUS_AD_SELECTION_BUYER_1);
        mAdSelectionEntryDao.persistAdSelection(EXISTING_PREVIOUS_AD_SELECTION_BUYER_2);

        // Update for BUYER_1 and verify ads are filtered
        // T0 - BUYER_1 events (4 events entered)
        UpdateAdCounterHistogramTestCallback updateHistogramCallback =
                callUpdateAdCounterHistogram(mInputParams);

        assertWithMessage("Callback failed, was: %s", updateHistogramCallback)
                .that(updateHistogramCallback.mIsSuccess)
                .isTrue();

        mCustomAudienceDao.insertOrOverwriteCustomAudience(
                DBCustomAudienceFixture.getValidBuilderByBuyerNoFilters(CommonFixture.VALID_BUYER_1)
                        .setAds(Arrays.asList(AD_WITH_FILTER))
                        .build(),
                CommonFixture.getUri(CommonFixture.VALID_BUYER_1, "/update"),
                /*debuggable=*/ false);

        AdSelectionTestCallback adSelectionCallback = callSelectAds();

        assertWithMessage("Callback succeeded unexpectedly")
                .that(adSelectionCallback.mIsSuccess)
                .isFalse();

        // Sleep for ensured separation of timestamps
        Thread.sleep(200);

        // Update events for BUYER_2 to fill the event table which will not evict the first
        // entries for BUYER_1
        // T1 - BUYER_2 events do not trigger table eviction of the oldest events (which are
        // for BUYER_1)
        UpdateAdCounterHistogramInput inputParamsForBuyer2 =
                new UpdateAdCounterHistogramInput.Builder(
                                AD_SELECTION_ID_BUYER_2,
                                FrequencyCapFilters.AD_EVENT_TYPE_CLICK,
                                CommonFixture.VALID_BUYER_2,
                                CommonFixture.TEST_PACKAGE_NAME)
                        .build();

        updateHistogramCallback = callUpdateAdCounterHistogram(inputParamsForBuyer2);

        assertWithMessage("Callback failed, was: %s", updateHistogramCallback)
                .that(updateHistogramCallback.mIsSuccess)
                .isTrue();

        // Verify that the events for BUYER_1 were not evicted and the ad for BUYER_1 should not win
        adSelectionCallback = callSelectAds();

        assertWithMessage("Callback succeeded unexpectedly")
                .that(adSelectionCallback.mIsSuccess)
                .isFalse();

        // Update event for BUYER_1 to fill the event table for BUYER_1
        // T2 - BUYER_1 events trigger table eviction of the oldest events (which are for BUYER_1)
        UpdateAdCounterHistogramInput inputParamsForBuyer1 =
                new UpdateAdCounterHistogramInput.Builder(
                                AD_SELECTION_ID_BUYER_1,
                                FrequencyCapFilters.AD_EVENT_TYPE_VIEW,
                                CommonFixture.VALID_BUYER_1,
                                CommonFixture.TEST_PACKAGE_NAME)
                        .build();

        updateHistogramCallback = callUpdateAdCounterHistogram(inputParamsForBuyer1);

        assertWithMessage("Callback failed, was: %s", updateHistogramCallback)
                .that(updateHistogramCallback.mIsSuccess)
                .isTrue();

        // Verify that the events for BUYER_1 were evicted and the ad for BUYER_1 should now win
        // since the only event left is the new VIEW event
        adSelectionCallback = callSelectAds();

        assertWithMessage("Ad selection callback failed, was: %s", adSelectionCallback)
                .that(adSelectionCallback.mIsSuccess)
                .isTrue();
        assertWithMessage(
                        "Unexpected winning ad, ad selection responded with: %s",
                        adSelectionCallback.mAdSelectionResponse)
                .that(adSelectionCallback.mAdSelectionResponse.getRenderUri())
                .isEqualTo(AD_WITH_FILTER.getRenderUri());
    }

    private UpdateAdCounterHistogramTestCallback callUpdateAdCounterHistogram(
            UpdateAdCounterHistogramInput inputParams) throws InterruptedException {
        CountDownLatch callbackLatch = new CountDownLatch(1);
        UpdateAdCounterHistogramTestCallback callback =
                new UpdateAdCounterHistogramTestCallback(callbackLatch);

        mAdSelectionServiceImpl.updateAdCounterHistogram(inputParams, callback);

        assertThat(callbackLatch.await(CALLBACK_WAIT_MS, TimeUnit.MILLISECONDS)).isTrue();

        return callback;
    }

    private AdSelectionTestCallback callSelectAds() throws InterruptedException {
        AdSelectionConfig config =
                AdSelectionConfigFixture.anAdSelectionConfigBuilder()
                        .setCustomAudienceBuyers(
                                Arrays.asList(
                                        CommonFixture.VALID_BUYER_1, CommonFixture.VALID_BUYER_2))
                        .build();

        CountDownLatch callbackLatch = new CountDownLatch(1);
        AdSelectionTestCallback callback = new AdSelectionTestCallback(callbackLatch);

        mAdSelectionServiceImpl.selectAds(
                new AdSelectionInput.Builder()
                        .setAdSelectionConfig(config)
                        .setCallerPackageName(CommonFixture.TEST_PACKAGE_NAME)
                        .build(),
                mCallerMetadataMock,
                callback);

        assertThat(callbackLatch.await(SELECT_ADS_CALLBACK_WAIT_MS, TimeUnit.MILLISECONDS))
                .isTrue();

        return callback;
    }
}