执行空手道场景并等待它们完成返回值

问题描述 投票:0回答:1

我正在尝试异步运行我的场景,我发现这非常有用示例,但问题是在几条语句之后我需要线程完成执行然后再继续。

我需要的是以下内容:

Scenario: Demo1
    * def fun = function(){ karate.call('@something', { someValue: 'Hello'}); }
    * eval new java.lang.Thread(fun).start()
    * print 'Fist Something'
    * print 'Second Something'
    * print 'Third Something'
    * wait until this eval new java.lang.Thread(fun).start() is finished before continuing from this point
    * print 'Fourth Something'

还有一些场景,我需要返回一个值,直到某些方法完成执行。

Scenario: Demo2
    * def username = ''
    * def fun = 
    """
      function(){ 
        var responseObject = karate.call('@prepareSomething', { someValue: 'SomeValue'}); 
        karate.set('username', responseObject.username);
      }
    """
    * eval new java.lang.Thread(fun).start()
    * print 'Fist Something'
    * print 'Second Something'
    * print 'Third Something'
    * wait until this eval new java.lang.Thread(fun).start() is finished before continuing from this point
    * print 'Fourth Something'

任何指导将不胜感激,谢谢!

karate
1个回答
0
投票

我建议查看这个示例:https://twitter.com/getkarate/status/1417023536082812935

忽略 ActiveMQ 部分,但您会看到如何使用一些 Java 帮助程序代码来负责分叉线程并使用

CompletableFuture
来“阻塞”,直到在不同线程上完成某些工作等。

© www.soinside.com 2019 - 2024. All rights reserved.