parallelasynctests.java

来自「开源的axis2框架的源码。用于开发WEBSERVER」· Java 代码 · 共 605 行 · 第 1/2 页

JAVA
605
字号


    /**
     * @testStrategy Test for ordering an executor to shutdownNow while there
     *               is a request being processed.  Uses an application executor
     *               service.
     */
    public void testService_ExecutorShutdownNow_2() throws Exception {
        final String MESSAGE = "testExecutorShutdownNow_2";

        String title = myClassName + " : " + getName() + " : ";

        AsyncService service = getService(null);
        AsyncPort port = getPort(service);

		// get the default executor and check to make sure it is an executor service
		ExecutorService ex = Executors.newSingleThreadExecutor();
		service.setExecutor(ex);


        // submit a request to the server that will wait until we ask for it
		CallbackHandler<SleepResponse> sleepCallbackHandler1 = new CallbackHandler<SleepResponse>();

        String request1 = "sleepAsync_with_Callback_1";

        TestLogger.logger.debug(title + " port.sleepAsync(" + request1 +
                ", callbackHander1)  #1 being submitted....");
		Future<?> sr1 = port.sleepAsync(request1, sleepCallbackHandler1);
        TestLogger.logger.debug(
                title + " port.sleepAsync(" + request1 + ", callbackHander1)  #1 .....submitted.");

        // wait a bit to make sure that the server has the request
        Thread.sleep(1000);

		// tell the executor to shutdown immediately, which 
        // attempts to stop all actively executing tasks via Thread.interrupt()
        // and should prevent new tasks from being submitted
        TestLogger.logger
                .debug(title + " shutting down executor [" + ex.getClass().getName() + "]");
        ex.shutdownNow();

        // check the waiting request 
        TestLogger.logger.debug(title + " port.isAsleep(" + request1 + ") #1 being submitted....");
        String asleepWithCallback1 = port.isAsleep(request1);
        TestLogger.logger.debug(
                title + " port.isAsleep(" + request1 + ") #1 = [" + asleepWithCallback1 + "]");

        // wakeup the waiting request
        TestLogger.logger.debug(title + " port.wakeUp(request1) #1 being submitted....");
        String wake1 = port.wakeUp(request1);
        TestLogger.logger.debug(title + " port.wakeUp(" + request1 + ") #1 = [" + wake1 + "]");

        // wait a bit..
        Thread.sleep(2000);

        // check the Future
        if (sr1.isDone())
        {
            TestLogger.logger.debug(title + " sr1.isDone[TRUE] ");
        }

        // try to get the response
        boolean gotException = false;
        try {

            SleepResponse sleepResp1 = sleepCallbackHandler1.get();

            if (sleepResp1 != null)
            {
                TestLogger.logger.debug(title + " request [" + request1 +
                        "] #1:  sleepResponse [NOT NULL] from callback handler");
                String result1 = sleepResp1.getMessage();
                TestLogger.logger.debug(
                        title + " request [" + request1 + "] #1:  result [" + result1 + "] ");
            }
            else
            {
                TestLogger.logger.debug(title + " request [" + request1 +
                        "] #1:  sleepResponse [NULL] from callback handler");

                // see what the Future says
                TestLogger.logger.debug(
                        title + " request [" + request1 + "] #1:  ....check Future response...");
                Object futureResult = sr1.get();
                TestLogger.logger.debug(
                        title + " request [" + request1 + "] #1:  ....Future response [" +
                                futureResult + "]...");
            }

        } catch (Exception exc) {

            TestLogger.logger.debug(title + " request [" + request1 + "] :  got exception [" +
                    exc.getClass().getName() + "]  [" + exc.getMessage() + "] ");
            gotException = true;
        }

        assertTrue("Did not receive an exception from trying to access the response when the executor service is shutdown.",gotException);
    }

    /**
     * @testStrategy Test for ordering an executor to shutdownNow before there
     *               is a request.  Uses the default executor.
     *               
     */
    public void testService_ExecutorShutdownNow_3() throws Exception {
        final String MESSAGE = "testExecutorShutdownNow_3";

        String title = myClassName + " : " + getName() + " : ";

        AsyncService service = getService(null);
        AsyncPort port = getPort(service);

		// get the default executor and check to make sure it is an executor service
        ExecutorService ex = null;
        Executor executor = service.getExecutor();
        if ((executor != null) && (executor instanceof ExecutorService))
        {
            ex = (ExecutorService) executor;

            // tell the executor to shutdown immediately, which 
            // attempts to stop all actively executing tasks via Thread.interrupt()
            // and should prevent new tasks from being submitted
            TestLogger.logger
                    .debug(title + " shutting down executor [" + ex.getClass().getName() + "]");
            ex.shutdownNow();
        }
        else
        {
            TestLogger.logger.debug(title + " No executor service available. Nothing to test.");
            return;
        }


        boolean gotRequestException = false;

        String request1 = "sleepAsync_with_Callback_1";
        CallbackHandler<SleepResponse> sleepCallbackHandler1 = new CallbackHandler<SleepResponse>();
        Future<?> sr1 = null;

        try
        {
            // submit a request to the server that will wait until we ask for it
            TestLogger.logger.debug(title + " port.sleepAsync(" + request1 +
                    ", callbackHander1)  #1 being submitted....");
            sr1 = port.sleepAsync(request1, sleepCallbackHandler1);
            TestLogger.logger.debug(title + " port.sleepAsync(" + request1 +
                    ", callbackHander1)  #1 .....submitted.");
        }
        catch (Exception exc)
        {
            TestLogger.logger.debug(title + " request [" + request1 + "] :  got exception [" +
                    exc.getClass().getName() + "]  [" + exc.getMessage() + "] ");
            gotRequestException = true;
        }

        // if the request went through, continue processing to see if the response is stopped
        // this makes sure that the server doesn't keep the request forever
        boolean gotResponseException = false;

        if (!gotRequestException)
        {
            // wakeup the waiting request
            TestLogger.logger.debug(title + " port.wakeUp(request1) #1 being submitted....");
            String wake1 = port.wakeUp(request1);
            TestLogger.logger.debug(title + " port.wakeUp(" + request1 + ") #1 = [" + wake1 + "]");

            // try to get the response
            try {

                SleepResponse sleepResp1 = sleepCallbackHandler1.get();

                if (sleepResp1 != null)
                {
                    TestLogger.logger.debug(title + " request [" + request1 +
                            "] #1:  sleepResponse [NOT NULL] from callback handler");
                    String result1 = sleepResp1.getMessage();
                    TestLogger.logger.debug(
                            title + " request [" + request1 + "] #1:  result [" + result1 + "] ");
                }
                else
                {
                    TestLogger.logger.debug(title + " request [" + request1 +
                            "] #1:  sleepResponse [NULL] from callback handler");

                    // see what the Future says
                    TestLogger.logger.debug(title + " request [" + request1 +
                            "] #1:  ....check Future response...");
                    Object futureResult = sr1.get();
                    TestLogger.logger.debug(title + " request [" + request1 +
                            "] #1:  ....Future response [" + futureResult + "]...");
                }

            } catch (Exception exc) {

                TestLogger.logger.debug(title + " request [" + request1 + "] :  got exception [" +
                        exc.getClass().getName() + "]  [" + exc.getMessage() + "] ");
                gotResponseException = true;
            }
        }

        assertTrue("Did not receive an exception from trying to submit the request when the executor service is shutdown.",gotRequestException);

        //assertTrue("Did not receive an exception from trying to access the response when the executor service is shutdown.",gotResponseException);
    }




    /**
     * Auxiliary method used for doing isAsleep checks. Will perform isAsleep
     * up to a MAX_ISASLEEP_CHECK number of checks. Will sleep for
     * SLEEP_ISASLEEP_SEC seconds in between requests. If reaches maximum number
     * fo retries then will fail the test
     */
    private boolean isAsleepCheck(String MESSAGE, AsyncPort port) {
        boolean asleep = false;
        int check = 30;
        String msg = null;
        do {
            msg = port.isAsleep(MESSAGE);
            asleep = (msg != null);

            // fail the test if we ran out of checks
            if ((check--) == 0)
                fail("Serve did not receive sleep after several retries");

            // sleep for a bit
            try {
                Thread.sleep(30);
            } 
            catch (InterruptedException e) {
            }

        } while (!asleep);

        if (asleep) {
            assertTrue("Sleeping on an incorrect message", MESSAGE.equals(msg));
        }

        return true;
    }
    

    private AsyncService getService(Executor ex) {
        AsyncService service = new AsyncService();

        if (ex!= null)
            service.setExecutor(ex);
        
        if (service.getExecutor() == null)
        {
            TestLogger.logger.debug(myClassName + " : getService() : executor is null");
        }
        else
        {
            TestLogger.logger.debug(myClassName + " : getService() : executor is available ");
        }

        return service;
    }


    private AsyncPort getPort(AsyncService service) {

        AsyncPort port = service.getAsyncPort();
        assertNotNull("Port is null", port);

        Map<String, Object> rc = ((BindingProvider) port).getRequestContext();
        rc.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
                DOCLITWR_ASYNC_ENDPOINT);
        
        return port;

    }

    /**
     * Auxiliary method used for obtaining a proxy pre-configured with a
     * specific Executor
     */
    private AsyncPort getPort(Executor ex) {
        AsyncService service = getService(ex);

        AsyncPort port = service.getAsyncPort();
        assertNotNull("Port is null", port);

        Map<String, Object> rc = ((BindingProvider) port).getRequestContext();
        rc.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
                DOCLITWR_ASYNC_ENDPOINT);
        
        return port;
    }
    
    private void waitBlocking(Future<?> monitor){
        while (!monitor.isDone()){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?