1515"""
1616System tests for metadata.
1717"""
18+ import time
1819
1920import pytest
2021
@@ -216,7 +217,7 @@ class AnyKind(ndb.Model):
216217 entity1 .put ()
217218 dispose_of (entity1 .key ._key )
218219
219- properties = get_properties_of_kind ( "AnyKind" )
220+ properties = _wait_for_metadata_update ( get_properties_of_kind , "AnyKind" )
220221 assert properties == ["bar" , "baz" , "foo" , "qux" ]
221222
222223 properties = get_properties_of_kind ("AnyKind" , start = "c" )
@@ -246,7 +247,7 @@ class AnyKind(ndb.Model):
246247 entity1 .put ()
247248 dispose_of (entity1 .key ._key )
248249
249- properties = get_properties_of_kind ( "AnyKind" )
250+ properties = _wait_for_metadata_update ( get_properties_of_kind , "AnyKind" )
250251 assert properties == ["bar" , "baz" , "foo" , "qux" ]
251252
252253 properties = get_properties_of_kind ("AnyKind" , start = "c" )
@@ -273,7 +274,9 @@ class AnyKind(ndb.Model):
273274 entity1 .put ()
274275 dispose_of (entity1 .key ._key )
275276
276- representations = get_representations_of_kind ("AnyKind" )
277+ representations = _wait_for_metadata_update (
278+ get_representations_of_kind , "AnyKind"
279+ )
277280 assert representations == {
278281 "bar" : ["STRING" ],
279282 "baz" : ["INT64" ],
@@ -291,3 +294,20 @@ class AnyKind(ndb.Model):
291294 "AnyKind" , start = "c" , end = "p"
292295 )
293296 assert representations == {"foo" : ["INT64" ]}
297+
298+
299+ def _wait_for_metadata_update (func , arg ):
300+ # Datastore apparently takes some time to update the metadata
301+ # before queries can be made. We'll give it a few seconds to see
302+ # if the query works.
303+
304+ deadline = time .time () + 30
305+ while True :
306+ result = func (arg )
307+ if result :
308+ break
309+
310+ assert time .time () < deadline , "Metadata was not updated in time."
311+
312+ time .sleep (1 )
313+ return result
0 commit comments