Skip to content

Commit fc66d58

Browse files
authored
fix: flaky test com.google.datastore.snippets.ConceptsTest: testStale… (#1012)
Fixes #905 ☕️ Please follow the explanation in comment https://togithub.com/googleapis/java-datastore/issues/905#issuecomment-1460077763.
1 parent a999a66 commit fc66d58

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

samples/snippets/src/test/java/com/google/datastore/snippets/ConceptsTest.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import com.google.cloud.datastore.IncompleteKey;
3535
import com.google.cloud.datastore.Key;
3636
import com.google.cloud.datastore.KeyFactory;
37+
import com.google.cloud.datastore.KeyQuery;
3738
import com.google.cloud.datastore.ListValue;
3839
import com.google.cloud.datastore.PathElement;
3940
import com.google.cloud.datastore.ProjectionEntity;
@@ -65,7 +66,9 @@
6566
import java.util.List;
6667
import java.util.Map;
6768
import java.util.TimeZone;
69+
import java.util.concurrent.TimeUnit;
6870
import java.util.concurrent.TimeoutException;
71+
import org.junit.After;
6972
import org.junit.AfterClass;
7073
import org.junit.Before;
7174
import org.junit.BeforeClass;
@@ -129,6 +132,13 @@ public void setUp() {
129132
datastoreRealBackend = DatastoreOptions.getDefaultInstance().getService();
130133
}
131134

135+
@After
136+
public void tearDown() throws Exception {
137+
KeyQuery taskQuery = Query.newKeyQueryBuilder().setKind("Task").build();
138+
Key[] taskKeysToDelete = Iterators.toArray(datastoreRealBackend.run(taskQuery), Key.class);
139+
datastoreRealBackend.delete(taskKeysToDelete);
140+
}
141+
132142
/**
133143
* Stops the local Datastore emulator.
134144
*
@@ -1147,8 +1157,10 @@ public void testInQuerySorted() {
11471157
}
11481158

11491159
@Test
1150-
public void testStaleReads() {
1160+
public void testStaleReads() throws InterruptedException {
11511161
setUpQueryTestsRealBackend();
1162+
// waiting for 6 seconds, so that we can query with read time of 5 seconds ago
1163+
TimeUnit.SECONDS.sleep(6);
11521164
Datastore datastoreClient = datastoreRealBackend;
11531165
// [START datastore_stale_read]
11541166
Key taskKey =
@@ -1158,10 +1170,10 @@ public void testStaleReads() {
11581170
.addAncestors(PathElement.of("TaskList", "default"))
11591171
.newKey("someTask");
11601172

1161-
Timestamp fifteenSecondsAgo =
1162-
Timestamp.ofTimeSecondsAndNanos(Timestamp.now().getSeconds() - 15L, 0);
1163-
// Create a readOption with read time fifteenSecondsAgo
1164-
ReadOption readOption = ReadOption.readTime(fifteenSecondsAgo);
1173+
Timestamp fiveSecondsAgo =
1174+
Timestamp.ofTimeSecondsAndNanos(Timestamp.now().getSeconds() - 5L, 0);
1175+
// Create a readOption with read time fiveSecondsAgo
1176+
ReadOption readOption = ReadOption.readTime(fiveSecondsAgo);
11651177
// Use the readOption to Fetch entity
11661178
Entity entity = datastoreClient.get(taskKey, readOption);
11671179

0 commit comments

Comments
 (0)