Google App Engine 数据存储区 - 测试查询失败 [英] Google App Engine Datastore - Testing Queries fails

查看:28
本文介绍了Google App Engine 数据存储区 - 测试查询失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试测试我的一段代码,该代码在放入新实体之前对数据存储运行查询,以确保不会创建重复项.我编写的代码在应用程序的上下文中运行良好,但我为该方法编写的测试失败了.在测试包的上下文中,我似乎无法通过查询访问放入数据存储区的数据.

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 wrote works fine in the context of the app, but the tests I wrote for that methods are failing. It seems that I cannot access data put into the datastore through queries in the context of the testing package.

一种可能性可能存在于 goapp test 的输出中,其内容为:应用所有待处理的事务并保存数据存储.这行在 get 和 put 方法被调用后打印出来(我用日志语句验证了这一点).

One possibility might lie in the output from goapp test which reads: Applying all pending transactions and saving the datastore. This line prints out after both the get and put methods are called (I verified this with log statements).

我尝试关闭上下文并为不同的操作创建一个新的上下文,但不幸的是,这也无济于事.下面是一个简单的测试用例,它放入一个对象,然后对其运行查询.任何帮助将不胜感激.

I tried closing the context and creating a new one for the different operations, but unfortunately that didn't help either. Below is a simple test case that Puts in an object and then runs a query on it. Any help would be appreciated.

type Entity struct {
    Value string
}

func TestEntityQuery(t *testing.T) {
    c, err := aetest.NewContext(nil)
    if err != nil {
        t.Fatal(err)
    }
    defer c.Close()

    key := datastore.NewIncompleteKey(c, "Entity", nil)
    key, err = datastore.Put(c, key, &Entity{Value: "test"})
    if err != nil {
        t.Fatal(err)
    }

    q := datastore.NewQuery("Entity").Filter("Value =", "test")
    var entities []Entity
    keys, err := q.GetAll(c, &entities)
    if err != nil {
        t.Fatal(err)
    }
    if len(keys) == 0 {
        t.Error("No keys found in query")
    }
    if len(entities) == 0 {
        t.Error("No entities found in query")
    }
}

推荐答案

您的测试代码没有任何问题.问题在于数据存储本身.HR 数据存储区中的大多数查询都不是立即一致",而是最终一致.您可以在 Datastore 文档中阅读更多相关信息.

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.

所以基本上发生的事情是你将一个实体放入数据存储区,SDK 的数据存储区模拟"你可以在生产中观察到的延迟,所以如果你在这之后运行查询(这不是祖先查询),查询结果将不包括您刚刚保存的新实体.

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.

如果您在 datastore.Put()q.GetAll() 之间休眠几秒钟,您将看到测试通过.试试看.在我的测试中,只睡 100 毫秒就足够了,而且测试总是通过.但是在为此类情况编写测试时,请使用 StronglyConsistentDatastore: true 选项,如 JonhGB 的回答所示.

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.

这篇关于Google App Engine 数据存储区 - 测试查询失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆