I'm trying to test a function that should fetch all the objects of a certain kind from the datastore.
Within the test it appears I have to insert a sleep to make the query find all the saved items.
The code below is a reproducible example. The first log trace is from when the sleep line is commented out, and the second when the sleep is uncommented. Note the length: 0 and length: 3
I'm assuming this is an eventual consistency issue, which would arise in production if I persisted several objects and immediately queried for them. But in production, these items were persisted long beforehand. Since this situation only arises for my test, is there something I'm meant to be doing to force the datastore to wait till it has fully saved the items before continuing?
I tried wrapping the test saves in a transaction, but got an "Only ancestor queries are allowed inside transactions" error.
type Thing struct {
Str1 string
Str2 string
}
func (thing Thing) Save(c appengine.Context) error {
k := datastore.NewKey(c, "Thing", thing.Str1 + "_" + thing.Str2, 0, nil)
if _, err := datastore.Put(c, k, &thing); err != nil {
return err
}
return nil
}
func GetThings(c appengine.Context) ([]Thing, error) {
var things []Thing
q := datastore.NewQuery("Thing").
Filter("Str1=", "thing")
_, err := q.GetAll(c, &things)
if err != nil {
return nil, err
}
return things, nil
}
func TestGetThings(t *testing.T) {
c, _ := aetest.NewContext(nil)
defer c.Close()
thing1 := Thing{"thing", "1"}
thing2 := Thing{"thing", "2"}
thing3 := Thing{"thing", "3"}
thing1.Save(c)
thing2.Save(c)
thing3.Save(c)
// time.Sleep(2000 * time.Millisecond)
things, err := GetThings(c)
if err != nil {
t.Fatal(err)
}
t.Log("length:" + strconv.Itoa(len(things)))
}
Logs when sleep commented out
C:\Users\XXXX>goapp test thing -test.v
2014/09/23 21:24:05 appengine: not running under devappserver2; using some default configuration
=== RUN TestGetThings
INFO 2014-09-23 21:24:07,328 devappserver2.py:725] Skipping SDK update check.
WARNING 2014-09-23 21:24:07,328 devappserver2.py:741] DEFAULT_VERSION_HOSTNAME will not be set correctly with --port=0
WARNING 2014-09-23 21:24:07,351 api_server.py:383] Could not initialize images API; you are likely missing the Python "PIL" module.
INFO 2014-09-23 21:24:07,365 api_server.py:171] Starting API server at: http://localhost:50153
INFO 2014-09-23 21:24:07,371 dispatcher.py:183] Starting module "default" running at: http://localhost:50154
INFO 2014-09-23 21:24:07,377 admin_server.py:117] Starting admin server at: http://localhost:50155
INFO 2014-09-23 21:24:08,378 api_server.py:583] Applying all pending transactions and saving the datastore
INFO 2014-09-23 21:24:08,388 api_server.py:586] Saving search indexes --- PASS: TestGetThings (4.60 seconds)
thing_test.go:87: length:0
PASS
ok thing 4.729s
Logs when sleep not commented out
C:\Users\XXXX>goapp test thing -test.v
2014/09/23 21:24:28 appengine: not running under devappserver2; using some default configuration
=== RUN TestGetThings
INFO 2014-09-23 21:24:31,124 devappserver2.py:725] Skipping SDK update check.
WARNING 2014-09-23 21:24:31,124 devappserver2.py:741] DEFAULT_VERSION_HOSTNAME will not be set correctly with --port=0
WARNING 2014-09-23 21:24:31,148 api_server.py:383] Could not initialize images API; you are likely missing the Python "PIL" module.
INFO 2014-09-23 21:24:31,164 api_server.py:171] Starting API server at: http://localhost:50191
INFO 2014-09-23 21:24:31,171 dispatcher.py:183] Starting module "default" running at: http://localhost:50192
INFO 2014-09-23 21:24:31,176 admin_server.py:117] Starting admin server at: http://localhost:50193
INFO 2014-09-23 21:24:34,176 api_server.py:583] Applying all pending transactions and saving the datastore
INFO 2014-09-23 21:24:34,176 api_server.py:586] Saving search indexes --- PASS: TestGetThings (6.83 seconds)
thing_test.go:87: length:3
PASS
ok thing 6.987s