21
21
import static org .junit .Assert .assertTrue ;
22
22
import static org .junit .Assert .fail ;
23
23
24
- import android .util .Log ;
25
24
import androidx .annotation .NonNull ;
26
25
import androidx .test .ext .junit .runners .AndroidJUnit4 ;
27
- import com .google .android .gms .tasks .Continuation ;
28
- import com .google .android .gms .tasks .OnCompleteListener ;
29
- import com .google .android .gms .tasks .Task ;
26
+ import androidx .test .platform .app .InstrumentationRegistry ;
27
+ import com .google .android .gms .tasks .Tasks ;
28
+ import com .google .firebase .FirebaseApp ;
29
+ import com .google .firebase .FirebaseOptions ;
30
30
import com .google .firebase .database .core .DatabaseConfig ;
31
31
import com .google .firebase .database .core .Path ;
32
32
import com .google .firebase .database .core .RepoManager ;
36
36
import java .util .Arrays ;
37
37
import java .util .List ;
38
38
import java .util .Map ;
39
+ import java .util .Objects ;
40
+ import java .util .UUID ;
39
41
import java .util .concurrent .ExecutionException ;
40
42
import java .util .concurrent .Semaphore ;
41
43
import java .util .concurrent .TimeoutException ;
@@ -3406,99 +3408,170 @@ public void onComplete(DatabaseError error, DatabaseReference ref) {
3406
3408
IntegrationTestHelpers .waitFor (semaphore );
3407
3409
}
3408
3410
3411
+ private static FirebaseApp appForDatabaseUrl (String url , String name ) {
3412
+ return FirebaseApp .initializeApp (
3413
+ InstrumentationRegistry .getInstrumentation ().getTargetContext (),
3414
+ new FirebaseOptions .Builder ()
3415
+ .setApplicationId ("appid" )
3416
+ .setApiKey ("apikey" )
3417
+ .setDatabaseUrl (url )
3418
+ .build (),
3419
+ name );
3420
+ }
3421
+
3409
3422
@ Test
3410
3423
public void emptyQueryGet () throws DatabaseException , InterruptedException {
3411
- DatabaseReference node = IntegrationTestHelpers .getRandomNode ();
3412
- DatabaseConfig cfg = IntegrationTestHelpers .newTestConfig ();
3413
- final Semaphore semaphore = new Semaphore (0 );
3414
- node .get ()
3415
- .addOnCompleteListener (
3416
- new OnCompleteListener <DataSnapshot >() {
3417
- @ Override
3418
- public void onComplete (@ NonNull Task <DataSnapshot > task ) {
3419
- assertTrue (task .isSuccessful ());
3420
- assertNotNull (task .getException ());
3421
- assertEquals (task .getException ().getMessage (), "Client offline with empty cache!" );
3422
- semaphore .release ();
3423
- }
3424
- });
3425
- IntegrationTestHelpers .waitFor (semaphore );
3424
+ FirebaseApp app =
3425
+ appForDatabaseUrl (IntegrationTestValues .getAltNamespace (), UUID .randomUUID ().toString ());
3426
+ FirebaseDatabase db = FirebaseDatabase .getInstance (app );
3427
+ db .useEmulator ("10.0.2.2" , 9000 );
3428
+ DatabaseReference node = db .getReference ();
3429
+ try {
3430
+ Tasks .await (node .get ());
3431
+ } catch (ExecutionException e ) {
3432
+ assertEquals (e .getCause ().getMessage (), "Client is offline" );
3433
+ return ;
3434
+ }
3435
+ fail ();
3426
3436
}
3427
3437
3428
3438
@ Test
3429
- public void offlineQueryGet () throws DatabaseException {
3439
+ public void offlineQueryGet () throws DatabaseException , InterruptedException {
3440
+ FirebaseApp app =
3441
+ appForDatabaseUrl (IntegrationTestValues .getAltNamespace (), UUID .randomUUID ().toString ());
3442
+ FirebaseDatabase db = FirebaseDatabase .getInstance (app );
3443
+ db .useEmulator ("10.0.2.2" , 9000 );
3444
+ DatabaseReference node = db .getReference ();
3430
3445
DatabaseConfig cfg = IntegrationTestHelpers .newTestConfig ();
3431
3446
IntegrationTestHelpers .goOffline (cfg );
3432
- DatabaseReference node = IntegrationTestHelpers .getRandomNode ();
3433
- node .get ()
3434
- .addOnCompleteListener (
3435
- new OnCompleteListener <DataSnapshot >() {
3436
- @ Override
3437
- public void onComplete (@ NonNull Task <DataSnapshot > task ) {
3438
- Log .d ("QueryTest" , "offlineQueryGet onCompleteListener running." );
3439
- assertFalse (task .isSuccessful ());
3440
- assertNotNull (task .getException ());
3441
- assertEquals (task .getException ().getMessage (), "Client offline with empty cache!" );
3442
- }
3443
- });
3447
+ try {
3448
+ Tasks .await (node .get ());
3449
+ } catch (ExecutionException e ) {
3450
+ assertEquals (e .getCause ().getMessage (), "Client is offline" );
3451
+ return ;
3452
+ }
3453
+ fail ();
3444
3454
}
3445
3455
3446
3456
@ Test
3447
3457
public void getQueryBasic () throws DatabaseException , InterruptedException {
3448
- DatabaseReference ref = IntegrationTestHelpers .getRandomNode ();
3458
+ FirebaseApp app =
3459
+ appForDatabaseUrl (IntegrationTestValues .getAltNamespace (), UUID .randomUUID ().toString ());
3460
+ FirebaseDatabase db = FirebaseDatabase .getInstance (app );
3461
+ db .useEmulator ("10.0.2.2" , 9000 );
3462
+ DatabaseReference node = db .getReference ();
3449
3463
final Semaphore semaphore = new Semaphore (0 );
3450
- ref .setValue (42 )
3451
- .continueWithTask (
3452
- new Continuation <Void , Task <DataSnapshot >>() {
3453
- @ Override
3454
- public Task <DataSnapshot > then (@ NonNull Task <Void > task ) throws Exception {
3455
- assertTrue (task .isSuccessful ());
3456
- return ref .get ()
3457
- .addOnCompleteListener (
3458
- new OnCompleteListener <DataSnapshot >() {
3459
- @ Override
3460
- public void onComplete (@ NonNull Task <DataSnapshot > task ) {
3461
- assertTrue (task .isSuccessful ());
3462
- DataSnapshot snap = task .getResult ();
3463
- assertNotNull (snap );
3464
- assertEquals (42 , snap .getValue ());
3465
- semaphore .release ();
3466
- }
3467
- });
3468
- }
3469
- });
3470
- IntegrationTestHelpers .waitFor (semaphore );
3464
+ try {
3465
+ Tasks .await (node .setValue (42 ));
3466
+ assertEquals (42L , Tasks .await (node .get ()).getValue ());
3467
+ } catch (ExecutionException e ) {
3468
+ fail ();
3469
+ }
3471
3470
}
3472
3471
3473
3472
@ Test
3474
3473
public void getQueryCached ()
3475
3474
throws DatabaseException , InterruptedException , TimeoutException , TestFailure {
3476
- DatabaseReference ref = IntegrationTestHelpers .getRandomNode ();
3475
+ FirebaseApp app =
3476
+ appForDatabaseUrl (IntegrationTestValues .getAltNamespace (), UUID .randomUUID ().toString ());
3477
+ FirebaseDatabase db = FirebaseDatabase .getInstance (app );
3478
+ db .useEmulator ("10.0.2.2" , 9000 );
3479
+ DatabaseReference ref = db .getReference ();
3477
3480
DatabaseConfig cfg = IntegrationTestHelpers .newTestConfig ();
3478
3481
ReadFuture future = ReadFuture .untilNonNull (ref );
3479
3482
ref .setValue (42 );
3480
- assertEquals (42 , future .waitForLastValue ());
3483
+ assertEquals (42L , future .waitForLastValue ());
3481
3484
IntegrationTestHelpers .goOffline (cfg );
3485
+ try {
3486
+ assertEquals (42L , Tasks .await (ref .get ()).getValue ());
3487
+ } catch (ExecutionException e ) {
3488
+ fail ();
3489
+ }
3490
+ }
3491
+
3492
+ @ Test
3493
+ public void getQueryHitsCacheWhenOffline ()
3494
+ throws InterruptedException , ExecutionException , TimeoutException , TestFailure {
3495
+ FirebaseApp readerApp =
3496
+ appForDatabaseUrl (IntegrationTestValues .getNamespace (), UUID .randomUUID ().toString ());
3497
+ FirebaseApp writerApp =
3498
+ appForDatabaseUrl (IntegrationTestValues .getNamespace (), UUID .randomUUID ().toString ());
3499
+ FirebaseDatabase readerDb = FirebaseDatabase .getInstance (readerApp );
3500
+ FirebaseDatabase writerDb = FirebaseDatabase .getInstance (writerApp );
3501
+ readerDb .useEmulator ("10.0.2.2" , 9000 );
3502
+ writerDb .useEmulator ("10.0.2.2" , 9000 );
3503
+ DatabaseReference reader = readerDb .getReference ("/foo" );
3504
+ DatabaseReference writer = writerDb .getReference ("/foo" );
3505
+
3506
+ WriteFuture write = new WriteFuture (writer , 42L );
3507
+ assertNull (write .timedGet ());
3508
+
3482
3509
final Semaphore semaphore = new Semaphore (0 );
3483
- ref . get ()
3484
- . addOnCompleteListener (
3485
- new OnCompleteListener < DataSnapshot >() {
3486
- @ Override
3487
- public void onComplete ( @ NonNull Task < DataSnapshot > task ) {
3488
- assertTrue ( task . isSuccessful () );
3489
- DataSnapshot snapshot = task . getResult ();
3490
- assertNotNull ( snapshot );
3491
- assertEquals ( 42 , snapshot . getValue ());
3492
- semaphore . release ();
3493
- }
3494
- });
3510
+ reader . addValueEventListener (
3511
+ new ValueEventListener () {
3512
+ @ Override
3513
+ public void onDataChange ( @ NonNull DataSnapshot snapshot ) {
3514
+ if ( Objects . requireNonNull ( snapshot . getValue ()). equals ( 42L ) ) {
3515
+ semaphore . release ( );
3516
+ }
3517
+ }
3518
+
3519
+ @ Override
3520
+ public void onCancelled ( @ NonNull DatabaseError error ) { }
3521
+ });
3495
3522
IntegrationTestHelpers .waitFor (semaphore );
3523
+ reader .getDatabase ().goOffline ();
3524
+ try {
3525
+ DataSnapshot snapshot = Tasks .await (reader .get ());
3526
+ assertEquals (42L , snapshot .getValue ());
3527
+ } catch (ExecutionException e ) {
3528
+ fail ();
3529
+ }
3496
3530
}
3497
3531
3498
3532
@ Test
3499
- public void getQuerySkipsCache () throws DatabaseException {
3500
- DatabaseReference ref = IntegrationTestHelpers .getRandomNode ();
3501
- DatabaseConfig cfg = IntegrationTestHelpers .newTestConfig ();
3533
+ public void getQuerySkipsCacheWhenOnline ()
3534
+ throws DatabaseException , InterruptedException , ExecutionException , TestFailure ,
3535
+ TimeoutException {
3536
+ FirebaseApp readerApp =
3537
+ appForDatabaseUrl (IntegrationTestValues .getNamespace (), UUID .randomUUID ().toString ());
3538
+ FirebaseApp writerApp =
3539
+ appForDatabaseUrl (IntegrationTestValues .getNamespace (), UUID .randomUUID ().toString ());
3540
+ FirebaseDatabase readerDb = FirebaseDatabase .getInstance (readerApp );
3541
+ FirebaseDatabase writerDb = FirebaseDatabase .getInstance (writerApp );
3542
+ readerDb .useEmulator ("10.0.2.2" , 9000 );
3543
+ writerDb .useEmulator ("10.0.2.2" , 9000 );
3544
+ DatabaseReference reader = readerDb .getReference ();
3545
+ DatabaseReference writer = writerDb .getReference ();
3546
+
3547
+ reader .addValueEventListener (
3548
+ new ValueEventListener () {
3549
+ @ Override
3550
+ public void onDataChange (@ NonNull DataSnapshot snapshot ) {}
3551
+
3552
+ @ Override
3553
+ public void onCancelled (@ NonNull DatabaseError error ) {}
3554
+ });
3555
+
3556
+ WriteFuture write = new WriteFuture (writer , 42L );
3557
+ assertNull (write .timedGet ());
3558
+
3559
+ final Semaphore semaphore = new Semaphore (0 );
3560
+
3561
+ try {
3562
+ assertEquals (42L , Tasks .await (reader .get ()).getValue ());
3563
+ } catch (ExecutionException e ) {
3564
+ fail ();
3565
+ }
3566
+
3567
+ write = new WriteFuture (writer , 43L );
3568
+ assertNull (write .timedGet ());
3569
+
3570
+ try {
3571
+ assertEquals (43L , Tasks .await (reader .get ()).getValue ());
3572
+ } catch (ExecutionException e ) {
3573
+ fail ();
3574
+ }
3502
3575
}
3503
3576
3504
3577
@ Test
@@ -3508,6 +3581,7 @@ public void querySnapshotChildrenRespectDefaultOrdering()
3508
3581
List <DatabaseReference > refs = IntegrationTestHelpers .getRandomNode (2 );
3509
3582
DatabaseReference writer = refs .get (0 );
3510
3583
DatabaseReference reader = refs .get (1 );
3584
+
3511
3585
final Semaphore semaphore = new Semaphore (0 );
3512
3586
3513
3587
final Map list =
0 commit comments