private boolean handshake() {
   boolean res;
   long started = System.currentTimeMillis();
   do {
     try {
       res = myPydevConsoleCommunication.handshake();
     } catch (XmlRpcException ignored) {
       res = false;
     }
     if (res) {
       break;
     } else {
       long now = System.currentTimeMillis();
       if (now - started > APPROPRIATE_TO_WAIT) {
         break;
       } else {
         try {
           Thread.sleep(100);
         } catch (InterruptedException ignored) {
         }
       }
     }
   } while (true);
   return res;
 }
 public void dispose() {
   LOG.assertTrue(EventQueue.isDispatchThread(), Thread.currentThread().getName());
   myAbstractTreeBuilder = null;
   // this will also dispose wrapped TreeModel
   myTreeModelWrapper.dispose();
   myFileEditor = null;
 }
 public void drainQueuedUsageNodes() {
   assert !ApplicationManager.getApplication().isDispatchThread() : Thread.currentThread();
   UIUtil.invokeAndWaitIfNeeded(
       new Runnable() {
         @Override
         public void run() {
           myTransferToEDTQueue.drain();
         }
       });
 }
 @TestOnly
 public static void dispatchAllInvocationEventsInIdeEventQueue() throws InterruptedException {
   assert SwingUtilities.isEventDispatchThread() : Thread.currentThread();
   final EventQueue eventQueue = Toolkit.getDefaultToolkit().getSystemEventQueue();
   while (true) {
     AWTEvent event = eventQueue.peekEvent();
     if (event == null) break;
     AWTEvent event1 = eventQueue.getNextEvent();
     if (event1 instanceof InvocationEvent) {
       IdeEventQueue.getInstance().dispatchEvent(event1);
     }
   }
 }
  @TestOnly
  public static void waitForAlarm(final int delay) throws InterruptedException {
    assert !ApplicationManager.getApplication().isWriteAccessAllowed()
        : "It's a bad idea to wait for an alarm under the write action. Somebody creates an alarm which requires read action and you are deadlocked.";
    assert ApplicationManager.getApplication().isDispatchThread();

    final AtomicBoolean invoked = new AtomicBoolean();
    final Alarm alarm = new Alarm(Alarm.ThreadToUse.SWING_THREAD);
    alarm.addRequest(
        new Runnable() {
          @Override
          public void run() {
            ApplicationManager.getApplication()
                .invokeLater(
                    new Runnable() {
                      @Override
                      public void run() {
                        alarm.addRequest(
                            new Runnable() {
                              @Override
                              public void run() {
                                invoked.set(true);
                              }
                            },
                            delay);
                      }
                    });
          }
        },
        delay);

    UIUtil.dispatchAllInvocationEvents();

    boolean sleptAlready = false;
    while (!invoked.get()) {
      UIUtil.dispatchAllInvocationEvents();
      //noinspection BusyWait
      Thread.sleep(sleptAlready ? 10 : delay);
      sleptAlready = true;
    }
    UIUtil.dispatchAllInvocationEvents();
  }
 private AnActionEvent stopConsole(AnActionEvent e) {
   if (myPydevConsoleCommunication != null) {
     e =
         new AnActionEvent(
             e.getInputEvent(),
             e.getDataContext(),
             e.getPlace(),
             e.getPresentation(),
             e.getActionManager(),
             e.getModifiers());
     try {
       closeCommunication();
       // waiting for REPL communication before destroying process handler
       Thread.sleep(300);
     } catch (Exception ignored) {
       // Ignore
     }
   }
   return e;
 }
  private static int readInt(Scanner s, Process process) throws ExecutionException {
    long started = System.currentTimeMillis();

    while (System.currentTimeMillis() - started < PORTS_WAITING_TIMEOUT) {
      if (s.hasNextLine()) {
        String line = s.nextLine();
        try {
          return Integer.parseInt(line);
        } catch (NumberFormatException ignored) {
          continue;
        }
      }

      try {

        Thread.sleep(200);
      } catch (InterruptedException ignored) {
      }

      if (process.exitValue() != 0) {
        String error;
        try {
          error =
              "Console process terminated with error:\n"
                  + StreamUtil.readText(process.getErrorStream());
        } catch (Exception ignored) {
          error = "Console process terminated with exit code " + process.exitValue();
        }
        throw new ExecutionException(error);
      } else {
        break;
      }
    }

    throw new ExecutionException("Couldn't read integer value from stream");
  }