Search in sources :

Example 26 with Timestamp

use of com.google.firebase.Timestamp in project firebase-android-sdk by firebase.

the class ServerTimestampTest method verifyTimestampsAreResolved.

/**
 * Verifies a snapshot containing setData but with resolved server timestamps.
 */
private void verifyTimestampsAreResolved(DocumentSnapshot snapshot) {
    assertTrue(snapshot.exists());
    Timestamp when = snapshot.getTimestamp("when");
    assertNotNull(when);
    // Tolerate up to 48*60*60 seconds of clock skew between client and server. This should be more
    // than enough to compensate for timezone issues (even after taking daylight saving into
    // account) and should allow local clocks to deviate from true time slightly and still pass the
    // test.
    int deltaSec = 48 * 60 * 60;
    Timestamp now = Timestamp.now();
    assertTrue("resolved timestamp (" + when + ") should be within " + deltaSec + "s of now (" + now + ")", Math.abs(when.getSeconds() - now.getSeconds()) < deltaSec);
    // Validate the rest of the document.
    assertEquals(expectedDataWithTimestamp(when), snapshot.getData());
}
Also used : Timestamp(com.google.firebase.Timestamp)

Example 27 with Timestamp

use of com.google.firebase.Timestamp in project firebase-android-sdk by firebase.

the class ServerTimestampTest method verifyTimestampsAreEstimates.

/**
 * Verifies a snapshot containing setData but with local estimates for server timestamps.
 */
private void verifyTimestampsAreEstimates(DocumentSnapshot snapshot) {
    assertTrue(snapshot.exists());
    Timestamp when = snapshot.getTimestamp("when", ServerTimestampBehavior.ESTIMATE);
    assertNotNull(when);
    assertEquals(expectedDataWithTimestamp(when), snapshot.getData(ServerTimestampBehavior.ESTIMATE));
}
Also used : Timestamp(com.google.firebase.Timestamp)

Example 28 with Timestamp

use of com.google.firebase.Timestamp in project firebase-android-sdk by firebase.

the class TypeTest method testCanReadAndWriteTimestamps.

@Test
public void testCanReadAndWriteTimestamps() {
    Timestamp timestamp = new Timestamp(100, 123000000);
    verifySuccessfulWriteReadCycle(map("timestamp", timestamp), testDoc());
}
Also used : Timestamp(com.google.firebase.Timestamp) Test(org.junit.Test)

Example 29 with Timestamp

use of com.google.firebase.Timestamp in project firebase-android-sdk by firebase.

the class WriteBatchTest method testWriteTheSameServerTimestampAcrossWrites.

@Test
public void testWriteTheSameServerTimestampAcrossWrites() {
    CollectionReference collection = testCollection();
    DocumentReference docA = collection.document("a");
    DocumentReference docB = collection.document("b");
    EventAccumulator<QuerySnapshot> accumulator = new EventAccumulator<>();
    collection.addSnapshotListener(MetadataChanges.INCLUDE, accumulator.listener());
    QuerySnapshot initialSnap = accumulator.await();
    assertEquals(0, initialSnap.size());
    // Atomically write two documents with server timestamps.
    waitFor(collection.getFirestore().batch().set(docA, map("when", FieldValue.serverTimestamp())).set(docB, map("when", FieldValue.serverTimestamp())).commit());
    QuerySnapshot localSnap = accumulator.await();
    assertTrue(localSnap.getMetadata().hasPendingWrites());
    assertEquals(asList(map("when", null), map("when", null)), querySnapshotToValues(localSnap));
    QuerySnapshot serverSnap = accumulator.awaitRemoteEvent();
    assertFalse(serverSnap.getMetadata().hasPendingWrites());
    assertEquals(2, serverSnap.size());
    Timestamp when = serverSnap.getDocuments().get(0).getTimestamp("when");
    assertNotNull(when);
    assertEquals(asList(map("when", when), map("when", when)), querySnapshotToValues(serverSnap));
}
Also used : EventAccumulator(com.google.firebase.firestore.testutil.EventAccumulator) Timestamp(com.google.firebase.Timestamp) Test(org.junit.Test)

Example 30 with Timestamp

use of com.google.firebase.Timestamp in project firebase-android-sdk by firebase.

the class SQLiteTargetCache method start.

void start() {
    // Store exactly one row in the table. If the row exists at all, it's the global metadata.
    int found = db.query("SELECT highest_target_id, highest_listen_sequence_number, " + "last_remote_snapshot_version_seconds, last_remote_snapshot_version_nanos, " + "target_count FROM target_globals LIMIT 1").first(row -> {
        highestTargetId = row.getInt(0);
        lastListenSequenceNumber = row.getInt(1);
        lastRemoteSnapshotVersion = new SnapshotVersion(new Timestamp(row.getLong(2), row.getInt(3)));
        targetCount = row.getLong(4);
    });
    hardAssert(found == 1, "Missing target_globals entry");
}
Also used : SnapshotVersion(com.google.firebase.firestore.model.SnapshotVersion) Timestamp(com.google.firebase.Timestamp)

Aggregations

Timestamp (com.google.firebase.Timestamp)36 Test (org.junit.Test)18 MutableDocument (com.google.firebase.firestore.model.MutableDocument)7 Date (java.util.Date)6 DocumentKey (com.google.firebase.firestore.model.DocumentKey)5 SnapshotVersion (com.google.firebase.firestore.model.SnapshotVersion)5 ArrayList (java.util.ArrayList)5 GeoPoint (com.google.firebase.firestore.GeoPoint)4 ObjectValue (com.google.firebase.firestore.model.ObjectValue)4 TestUtil.wrapObject (com.google.firebase.firestore.testutil.TestUtil.wrapObject)4 Mutation (com.google.firebase.firestore.model.mutation.Mutation)3 Mutation.calculateOverlayMutation (com.google.firebase.firestore.model.mutation.Mutation.calculateOverlayMutation)3 TestUtil.deleteMutation (com.google.firebase.firestore.testutil.TestUtil.deleteMutation)3 TestUtil.mergeMutation (com.google.firebase.firestore.testutil.TestUtil.mergeMutation)3 TestUtil.patchMutation (com.google.firebase.firestore.testutil.TestUtil.patchMutation)3 TestUtil.setMutation (com.google.firebase.firestore.testutil.TestUtil.setMutation)3 Value (com.google.firestore.v1.Value)3 HashMap (java.util.HashMap)3 FieldValue (com.google.firebase.firestore.FieldValue)2 Query (com.google.firebase.firestore.core.Query)2