简体   繁体   English

如何在JUnit中刷新App Engine SearchServiceFactory异步操作?

[英]How to flush App Engine SearchServiceFactory async operation in JUnit?

I'm using App engine Standard env and my services use the Search API . 我正在使用App Engine标准环境,而我的服务使用Search API

So basically in my service I do something like: 因此,基本上在我的服务中,我会执行以下操作:

IndexSpec indexSpec = IndexSpec.newBuilder().setName(indexName).build();
Index index = SearchServiceFactory.getSearchService().getIndex(indexSpec);
index.putAsync(document);

I'd like to implement a JUnit test to validate my document has been saved in the index. 我想实现一个JUnit测试,以验证我的文档已保存在索引中。

But if I do in the same JUnit Thread: 但是,如果我在同一个JUnit线程中这样做:

...
index.putAsync(document);
index.search("");

it won't return my document (because I used Index#putAsync). 它不会返回我的文档(因为我使用了Index#putAsync)。

A simple hack is to use Thread#sleep: 一个简单的技巧是使用Thread#sleep:

...
index.putAsync(document);
Thread.sleep(10);
index.search("");

But that's really ugly. 但这真的很丑。

How can I flush/execute all waiting async operations before running my search? 如何在运行搜索之前刷新/执行所有等待的异步操作?

(I know there is a synchronous Index#put method but I want to use Index#putAsync in my service so the goal here is really to flush all waiting async operations) (我知道有一个同步的Index#put方法,但是我想在我的服务中使用Index#putAsync,所以这里的目标实际上是清除所有等待的异步操作)

Thanks 谢谢

I ended up doing something like that: 我最终做了这样的事情:

...
Future future = index.putAsync(document);
while (!future.isDone()) {
  Thread.yield();
}
index.search("");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM