3434import com .google .cloud .datastore .IncompleteKey ;
3535import com .google .cloud .datastore .Key ;
3636import com .google .cloud .datastore .KeyFactory ;
37+ import com .google .cloud .datastore .KeyQuery ;
3738import com .google .cloud .datastore .ListValue ;
3839import com .google .cloud .datastore .PathElement ;
3940import com .google .cloud .datastore .ProjectionEntity ;
6566import java .util .List ;
6667import java .util .Map ;
6768import java .util .TimeZone ;
69+ import java .util .concurrent .TimeUnit ;
6870import java .util .concurrent .TimeoutException ;
71+ import org .junit .After ;
6972import org .junit .AfterClass ;
7073import org .junit .Before ;
7174import 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