dongru2019 2014-09-23 20:50
浏览 31
已采纳

Appengine aetest的行为不一致

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

  • 写回答

2条回答 默认 最新

  • dongqing5925 2014-09-24 04:58
    关注

    To answer my own question, the solution is to use the Options parameter when creating the aetest.NewContext.

    c, _ := aetest.NewContext(&aetest.Options{"", true})
    

    The second item in the Options struct is a boolean named StronglyConsistentDatastore.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。