Exemple #1
0
  private void beforeFirstTest() {
    if ((ourMode & START_GUARD) != 0) {
      Thread timeAndMemoryGuard =
          new Thread() {
            @Override
            public void run() {
              log("Starting Time and Memory Guard");
              while (true) {
                try {
                  try {
                    Thread.sleep(10000);
                  } catch (InterruptedException e) {
                    e.printStackTrace();
                  }
                  // check for time spent on current test
                  if (myLastTestStartTime != 0) {
                    long currTime = System.currentTimeMillis();
                    long secondsSpent = (currTime - myLastTestStartTime) / 1000L;
                    Thread currentThread = getCurrentThread();
                    if (!mySavingMemorySnapshot) {
                      if (secondsSpent > PlatformTestCase.ourTestTime * myLastTestTestMethodCount) {
                        UsefulTestCase.printThreadDump();
                        log(
                            "Interrupting current Test (out of time)! Test class: "
                                + myLastTestClass
                                + " Seconds spent = "
                                + secondsSpent);
                        myInterruptedByOutOfTime = true;
                        if (currentThread != null) {
                          currentThread.interrupt();
                          if (!currentThread.isInterrupted()) {
                            //noinspection deprecation
                            currentThread.stop(
                                new RuntimeException("Current Test Interrupted: OUT OF TIME!"));
                          }

                          break;
                        }
                      }
                    }
                  }
                } catch (Exception e) {
                  e.printStackTrace();
                }
              }
              log("Time and Memory Guard finished.");
            }
          };
      timeAndMemoryGuard.setDaemon(true);
      timeAndMemoryGuard.start();
    }
    myStartTime = System.currentTimeMillis();
  }
Exemple #2
0
  private void runNextTest(final TestResult testResult, int totalTests, Class testCaseClass) {
    myRunTests++;
    if (!checkAvaliableMemory(35, testResult)) {
      testResult.stop();
      return;
    }
    if (testResult.errorCount() + testResult.failureCount() > MAX_FAILURE_TEST_COUNT) {
      addErrorMessage(
          testResult,
          "Too many errors. Tests stopped. Total "
              + myRunTests
              + " of "
              + totalTests
              + " tests run");
      testResult.stop();
      return;
    }
    if (myStartTime == 0) {
      boolean ourClassLoader =
          getClass().getClassLoader().getClass().getName().startsWith("com.intellij.");
      if (!ourClassLoader) {
        beforeFirstTest();
      }
    } else {
      if (myInterruptedByOutOfTime) {
        addErrorMessage(
            testResult,
            "Current Test Interrupted: OUT OF TIME! Class = "
                + myLastTestClass
                + " Total "
                + myRunTests
                + " of "
                + totalTests
                + " tests run");
        testResult.stop();
        return;
      }
    }

    log("\nRunning " + testCaseClass.getName());
    final Test test = getTest(testCaseClass);

    if (test == null) return;

    myLastTestClass = null;

    myLastTestClass = testCaseClass.getName();
    myLastTestStartTime = System.currentTimeMillis();
    myLastTestTestMethodCount = test.countTestCases();

    try {
      test.run(testResult);
    } catch (Throwable t) {
      if (t instanceof OutOfMemoryError) {
        if ((ourMode & SAVE_MEMORY_SNAPSHOT) != 0) {
          try {
            mySavingMemorySnapshot = true;
            log("OutOfMemoryError detected. Saving memory snapshot started");
          } finally {
            log("Saving memory snapshot finished");
            mySavingMemorySnapshot = false;
          }
        }
      }
      testResult.addError(test, t);
    }
  }