繁体   English   中英

如何在testNG类中使用completableFuture

[英]How to use completableFuture in testNG class

我想在带有@Test注释的TestNG类中运行CompletableFuture

下面是代码片段, recursionFuture方法通过main()方法递归调用来执行某个任务。 现在,如果我使用main()作为public static void main(String[] args))那么一切都按预期工作。 但是当我使用带有@Test注释的main() ,TestNG会在两者之间停止执行,并且不会执行整个任务。

我应该怎么做才能让@Test等到recursionFuture完成所有任务?

我将使用CompletableFuture进行异步任务,并且还需要使用@Test

任何帮助,将不胜感激。 谢谢。

//public static void main(String[] args) throws FileNotFoundException, InterruptedException, ExecutionException
    @Test // --> this logic of recursion with threads is problematic with testNG
    public static void main() throws FileNotFoundException, InterruptedException, ExecutionException 
    {
        System.setOut(new PrintStream(new File("/Users/Pankaj/Desktop/Thread")));

        Method[] method = ConcurrencyPoC_CompletableFuture.class.getMethods();

        for(int i=0; i<method.length; i++)
        {
            if(method[i].getName().startsWith("task"))
            {
                TreeMap<Object, Object> m = new TreeMap<>();
                m.put(method[i].getName(), method[i]);
                m.put("status", new AtomicBoolean(true));

                taskmap.put(method[i].getName(), m);
            }
        }

        //submitting tasks 
        ExecutorService ex = Executors.newCachedThreadPool();
        CompletableFuture<?> [] arrayFutures = new CompletableFuture[3];
        recursionFuture(ex, arrayFutures);
    }


    public static String recursionFuture(ExecutorService ex, CompletableFuture<?> [] arrayFutures) 
    {
        try
        {

            //check if any of submitted future is completed - to be used in case of array 
            for(CompletableFuture<?> future : arrayFutures)
            {
                future = CompletableFuture.supplyAsync(() -> new ConcurrencyPoC_CompletableFuture().executeTask(), ex);

                //storing future in a final variable for further calculation
                final CompletableFuture<?> task = future;

                CompletableFuture.anyOf(task).thenRunAsync(() ->
                {
                    try {

                        //apply recursion only when future's output is not null, otherwise there will be hell lot of futures get created which 
                        //don't do anything just eating up memory and executing the else block of executeTask() method.
                        // Latest change, as soon as any task is free, create a new array of 1 size to do the next task
                        if(task.get() != null)
                            recursionFuture(ex, new CompletableFuture[1]);

                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                , ex);
            }

        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

        return "ALl TASK COMPLETED";
    }

TestNG @Test注释与您可完成的未来之间没有对应关系。 问题出在你的tast.get()方法中。 它应该阻止运行线程,直到完成所有可完成的期货。

我在测试中使用Completable Future,从来没有遇到过TestNG的问题。

问题是你未来的一个完成,你回到测试方法,而不是等待所有的未来。 您应该将所有期货与thenApplyAsync()或compose()结合起来,因为您的Future是最终的,您只等待一个未来。 另外,您不应该使用CompletbleFuture.Any(),因为它在第一个未来完成时返回执行。

暂无
暂无

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

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