2014-09-23 1 views
0

내 코드에 '재부팅 대기'작업 단위 테스트가 필요합니다. 코드는 사용자가 지정한 시간 초과 값까지 기다렸다가 서버가 다시 시작되고 다시 시작됩니다. 타임 아웃 값이 경과하면 Timed out 예외가 발생합니다.Unit Testing - Java에 대한 서버 재부팅 작업을 시뮬레이트하는 방법은 무엇입니까?

테스트하려면 서버 재부팅 동작을 시뮬레이트해야합니다. 그게 정말로 가능합니까 ??

참고 : 내 단위 테스트를 위해 TestNG 프레임 워크를 사용하고 있습니다.

+0

세부 사항을 조금 더 추가 할 수 있습니까? 아마도 귀하의 코드가 클라이언트로 실행되고있을 것입니다. 서버가 재부팅 된 것을 어떻게 감지합니까? – DNA

+0

이 작업은 수행 할 수 있지만 사용중인 추상화에 따라 다릅니다. 자신이있는 곳을 보여주는 작은 스 니펫을 게시 할 수 있습니까? –

답변

0

음, 간단한 방법으로 다른 포트 또는 다른 URL을 수신하십시오. 그것은 서버가 다운 시뮬레이션합니다.

스레드를 잠시 기다렸다가 URL을 다시 바꿔서 서버를 시뮬레이션 할 수 있습니다.

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); 
     }