2014-09-23 98 views
0

我需要单元测试写在我的代码中的'等待重启'操作。代码将等待用户给出的指定超时值,以便服务器重新启动并出现。超时值过期后,它将引发超时异常。如何模拟单元测试的服务器重启任务 - Java?

为了测试,我需要模拟服务器重启的行为。这真的有可能吗?

注:我使用TestNG框架进行单元测试。

+0

你可以添加更多的细节?推测你的代码是作为客户端运行的。它如何检测到服务器已重新启动? – DNA 2014-09-23 09:04:15

+0

它可以完成,但它取决于你已有的抽象。你可以发布一个小片段,显示你在哪里? – 2014-09-23 09:05:20

回答

0

那么,以简单的方式,听不同的端口或不同的网址。它模拟服务器已关闭。

您可以在线程中等待一段时间,然后将URL更改为正确以模拟服务器UP!

0
Here is my code snippet.. I want to write a TestNG stub to test this method. 

    public Response execute() throws TaskException 
    { 
     Buffer theOutputBuffer = myWorkSpace.getOutputBuffer(); 
     OutputStream theOutputStream; 
     try 
     { 
      isRebooted = false; 

      theOutputStream = theOutputBuffer.getOutputStream(); 

      Date theStartDate = new Date(); 
      long theElapsedTime = 0; 
      while(!isRebooted) 
      { 
       if (theElapsedTime > getDTO().getTimeoutValue()) 
       { 
        throw new TaskException("Timed out waiting for reboot!"); 
       } 
       else 
       { 
        try 
        { 
         if (theConsole == null) 
         { 
          waitDirectly(mySession); 
         } 
         else 
         { 
          synchronized(mySession) 
          { 
           waitThroughConsole(); 
          } 
         } 

        } 
        finally 
        { 
         Date theDate = new Date(); 
         theElapsedTime = 
          (theDate.getTime() - theStartDate.getTime())/MILLI_SECS_PER_SEC; 
        } 
       } 
      } 
     } 
     catch (SessionException ex) 
     { 
      throw new TaskException (ex); 
     } 
     catch (ConsoleHijackException ex) 
     { 
      throw new TaskException (ex); 
     } 
     catch (BufferException ex) 
     { 
      throw new TaskException (ex); 
     } 
     catch (TimedoutException ex) 
     { 
      throw new TaskException (ex); 
     } 
     catch (InterruptedException ex) 
     { 
      if (myExecutionShell != null) 
      { 
       try 
       { 
        myExecutionShell.close(); 
       } 
       catch (InterruptedException e) 
       { 
        throw new TaskException(e); 
       } 
      } 
      throw new TaskException(ex); 
     } 
     catch (ConsoleException e) 
     { 
      throw new TaskException(e); 
     } 
     catch (ShellException ex) 
     { 
      throw new TaskException (ex); 
     } 
相关问题