如何在testNG类中使用completableFuture

时间:2017-01-26 15:09:16

标签: java multithreading testng

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

以下是代码段,recursionFuture方法通过main()方法递归调用以执行某项任务。 现在,如果我使用main()作为public static void main(String[] args)),那么一切都按预期工作。但是当我使用带有main()注释的@Test时,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";
    }

1 个答案:

答案 0 :(得分:0)

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

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

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