Google App Engine Datastore - Testing Queries fails

后端 未结 3 1127
终归单人心
终归单人心 2020-12-11 07:42

I am currently trying to test a piece of my code that runs a query on the datastore before putting in a new entity to ensure that duplicates are not created. The code I wrot

相关标签:
3条回答
  • 2020-12-11 08:24

    The way to do this is to force the datastore to be strongly consistent by setting up the context like this:

    c, err := aetest.NewContext(&aetest.Options{StronglyConsistentDatastore: true})
        if err != nil {
            t.Fatal(err)
        }
    

    Now the datastore won't need any sleep to work, which is faster, and better practice in general.


    Update: This only works with the old aetest package which was imported via appengine/aetest. It does not work with the newer aetest package which is imported with google.golang.org/appengine/aetest. App Engine has changed from using an appengine.Context to using a context.Context, and consequently the way that the test package now works is quite different.

    0 讨论(0)
  • 2020-12-11 08:43

    To compliment @JohnGB's answer in the latest version of aetest, there are more steps to get a context with strong consistency. First create an instance, then create a request from that instance, which you can use to produce a context.

    inst, err := aetest.NewInstance(
    &aetest.Options{StronglyConsistentDatastore: true})
    
    if err != nil {
        t.Fatal(err)
    }
    defer inst.Close()
    
    req, err := inst.NewRequest("GET", "/", nil)
    if err != nil {
        t.Fatal(err)
    }
    
    ctx := appengine.NewContext(req)
    
    0 讨论(0)
  • 2020-12-11 08:45

    There is nothing wrong with your test code. The issue lies in the Datastore itself. Most queries in the HR Datastore are not "immediately consistent" but eventually consistent. You can read more about this in the Datastore documentation.

    So basically what happens is that you put an entity into the Datastore, and the SDK's Datastore "simulates" the latency that you can observe in production, so if you run a query right after that (which is not an ancestor query), the query result will not include the new entity you just saved.

    If you put a few seconds sleep between the datastore.Put() and q.GetAll(), you will see the test passes. Try it. In my test it was enough to sleep just 100ms, and the test always passed. But when writing tests for such cases, use the StronglyConsistentDatastore: true option as can be seen in JonhGB's answer.

    You would also see the test pass without sleep if you'd use Ancestor queries because they are strongly consistent.

    0 讨论(0)
提交回复
热议问题