private void disposeConsoleProcess() throws InterruptedException {
    myProcessHandler.destroyProcess();

    waitFor(myProcessHandler);

    if (!myProcessHandler.isProcessTerminated()) {
      if (!waitFor(myProcessHandler)) {
        if (!myProcessHandler.isProcessTerminated()) {
          throw new RuntimeException("Cannot stop console process");
        }
      }
    }
    myProcessHandler = null;
  }
  public void initAndRun(final String... statements2execute) throws ExecutionException {
    super.initAndRun();

    if (handshake()) {

      ApplicationManager.getApplication()
          .invokeLater(
              new Runnable() {

                @Override
                public void run() {
                  // Propagate console communication to language console
                  final PythonConsoleView consoleView = getConsoleView();

                  consoleView.setConsoleCommunication(myPydevConsoleCommunication);
                  consoleView.setSdk(mySdk);
                  consoleView.setExecutionHandler(myConsoleExecuteActionHandler);
                  myProcessHandler.addProcessListener(
                      new ProcessAdapter() {
                        @Override
                        public void onTextAvailable(ProcessEvent event, Key outputType) {
                          consoleView.print(event.getText(), outputType);
                        }
                      });

                  enableConsoleExecuteAction();

                  for (String statement : statements2execute) {
                    consoleView.executeStatement(statement + "\n", ProcessOutputTypes.SYSTEM);
                  }

                  fireConsoleInitializedEvent(consoleView);
                }
              });
    } else {
      getConsoleView().print("Couldn't connect to console process.", ProcessOutputTypes.STDERR);
      myProcessHandler.destroyProcess();
      finishConsole();
    }
  }
 private void closeCommunication() {
   if (!myProcessHandler.isProcessTerminated()) {
     myPydevConsoleCommunication.close();
   }
 }
  @Override
  public void runTestOn(final String sdkHome) throws Exception {
    final Project project = getProject();

    final Sdk sdk = createTempSdk(sdkHome, SdkCreationType.EMPTY_SDK);

    setProcessCanTerminate(false);

    PydevConsoleRunner consoleRunner =
        new PydevConsoleRunner(
            project,
            sdk,
            PyConsoleType.PYTHON,
            myFixture.getTempDirPath(),
            Maps.<String, String>newHashMap(),
            PyConsoleOptions.getInstance(project).getPythonConsoleSettings(),
            new String[] {}) {
          @Override
          protected void showConsole(
              Executor defaultExecutor, @NotNull RunContentDescriptor contentDescriptor) {
            myContentDescriptorRef.set(contentDescriptor);
            super.showConsole(defaultExecutor, contentDescriptor);
          }
        };

    before();

    myConsoleInitSemaphore = new Semaphore(0);

    consoleRunner.addConsoleListener(
        new PydevConsoleRunner.ConsoleListener() {
          @Override
          public void handleConsoleInitialized(LanguageConsoleView consoleView) {
            myConsoleInitSemaphore.release();
          }
        });

    consoleRunner.run();

    waitFor(myConsoleInitSemaphore);

    myCommandSemaphore = new Semaphore(1);

    myConsoleView = consoleRunner.getConsoleView();
    myProcessHandler = (PyConsoleProcessHandler) consoleRunner.getProcessHandler();

    myExecuteHandler =
        (PydevConsoleExecuteActionHandler) consoleRunner.getConsoleExecuteActionHandler();

    myCommunication = consoleRunner.getPydevConsoleCommunication();

    myCommunication.addCommunicationListener(
        new ConsoleCommunicationListener() {
          @Override
          public void commandExecuted(boolean more) {
            myCommandSemaphore.release();
          }

          @Override
          public void inputRequested() {}
        });

    myProcessHandler.addProcessListener(
        new ProcessAdapter() {
          @Override
          public void processTerminated(ProcessEvent event) {
            if (event.getExitCode() != 0 && !myProcessCanTerminate) {
              Assert.fail("Process terminated unexpectedly\n" + output());
            }
          }
        });

    OutputPrinter myOutputPrinter = null;
    if (shouldPrintOutput) {
      myOutputPrinter = new OutputPrinter();
      myOutputPrinter.start();
    }

    waitForOutput("PyDev console");

    try {
      testing();
      after();
    } finally {
      setProcessCanTerminate(true);

      if (myOutputPrinter != null) {
        myOutputPrinter.stop();
      }

      disposeConsole();
    }
  }