0

基于guava-libraries 示例,我正在使用 ListenableFuture。

我正在使用:
java 1.6
JDeveloper
11.1.1.6.0 guava-13.0.1.jar
junit-4.5.jar
easymock-3.1.jar
powermock-easymock-1.4.12-full.jar

我试图确保在异步模式下调用被测方法。

我的 Manager.java 代码是:

    ...

    public synchronized void   refreshAsync(final String id) {
    m_Log.entry(id);

    ListeningExecutorService service =
        MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());

    ListenableFuture<Details> getDetailsTask =
        service.submit(new Callable<Details>() {
            @Override
            public Details call() {
                System.out.println(Thread.currentThread().toString());
                return MyCacheManager.getDetails(id);
            }
        });

    Futures.addCallback(getDetailsTask ,
                        new FutureCallback<Details>() {
            // we want this handler to run immediately after we push the big red button!

            public void onSuccess(Details details) {
                System.out.println("Success");
                //TODO: publish event
            }

            public void onFailure(Throwable thrown) {
                System.out.println("Failed");
                //TODO: log
            }
        });

    service.shutdown();

    m_Log.exit("done async");
}

...

我的测试是:

    @RunWith(PowerMockRunner.class)
    @PrepareForTest( { Manager.class,  MyCacheManager.class, TWResourceManager.class, Logger.class })
    public class DetailsTests {
    {

        ...

        @Test (timeout = 4000) 
        public void refreshAsync_RequestedInAsyncMode_NoWaitForComplete() throws Exception {

            // Creating nice mock - because we are not caring about methods call order
            mockStatic(MyCacheManager.class);

            // Setup
            final int operationTimeMilis = 5000;
            expect(MyCacheManager.getDetails(anyObject(String.class))).andStubAnswer(new IAnswer<Details>() {
                    public Details answer() {
                        try {
                            System.out.println("start waiting 5 sec");
                            System.out.println(Thread.currentThread().toString());
                            Thread.sleep(operationTimeMilis);
                            System.out.println("finished waiting 5 sec");
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        return Details.getEmpty();
                    }
                });

            replay(MyCacheManager.class);

            replayAll();

            ISchemaActionsContract controller = new TWManager();
            controller.refreshSchemaDetailsAsync("schema_id");

            // We need not to verify mocks, since all we are testing is timeout
            // verifyAll();
        }
    }

当我运行/调试测试时 - 它总是在超时时失败。似乎在“同步”模式下调用了模拟方法“MyCacheManager.getDetails”。

但是当我从常规代码/调试中调用相同的函数时 - 它在异步模式下运行(我将 Thread.sleep(10000) 放入 MyCacheManager.getDetails 方法,并且 Manager.refreshAsync 方法在没有等待/被阻止的情况下退出。

此外,如果我将方法更改为使用常规 FutureTask,测试将按预期通过。

        ...

        Object res = null;
        FutureTask task = new FutureTask(new Runnable() {
                @Override
                public void run() {
                    MyCacheManager.getDetails(id);
                }
            }, res);            

        m_Log.debug("async mode - send request and do not wait for answer.");

        Executors.newCachedThreadPool().submit(task);

任何想法都会更受欢迎!:)
谢谢!

4

0 回答 0