/* Start these two processes. */
 private void startThreads(
     JUnitProcessThread thread1, JUnitProcessThread thread2, long sleepTime) {
   thread1.start();
   try {
     Thread.sleep(sleepTime);
   } catch (InterruptedException e) {
     e.printStackTrace();
   }
   thread2.start();
 }
  /* Check the exit value of processes. */
  private void checkExitVals(
      JUnitProcessThread thread1, int exitVal1, JUnitProcessThread thread2, int exitVal2) {
    /* End these threads. */
    try {
      thread1.finishTest();
      thread2.finishTest();
    } catch (Throwable t) {
      System.err.println(t.toString());
    }

    /* Check whether the processes exit with expected values. */
    assertEquals(thread1.getExitVal(), exitVal1);
    assertEquals(thread2.getExitVal(), exitVal2);
  }
  @Test
  public void testMultiEnvWrite() {
    /* Initiate the environment. */
    MainWrite.main(
        new String[] {
          "-envHome", SharedTestUtils.getTestDir().getAbsolutePath(), "-init", "-initSize", "1000"
        });

    /* Command for process 1. */
    String[] command1 = new String[8];
    command1[0] = "com.sleepycat.je.MultiProcessWriteTest$MainWrite";
    command1[1] = "-envHome";
    command1[2] = SharedTestUtils.getTestDir().getPath();
    command1[3] = "-write";
    command1[4] = "-numOps";
    command1[5] = "100000";
    command1[6] = "-procNum";
    command1[7] = "1";

    /* Command for process 2. */
    String[] jvmParams = {"-Xmx40m"};
    String[] command2 = new String[8];
    command2[0] = command1[0];
    command2[1] = command1[1];
    command2[2] = command1[2];
    command2[3] = command1[3];
    command2[4] = command1[4];
    command2[5] = "200000";
    command2[6] = command1[6];
    command2[7] = "2";

    /* Create and start the two threads. */
    JUnitProcessThread thread1 = new JUnitProcessThread("process1", command1);
    JUnitProcessThread thread2 = new JUnitProcessThread("process2", jvmParams, command2);

    thread1.start();

    try {
      Thread.sleep(20);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }

    thread2.start();

    /* End these two threads. */
    try {
      thread1.finishTest();
      thread2.finishTest();
    } catch (Throwable t) {
      System.err.println(t.toString());
    }

    /* Check whether the process throws out unexpected exceptions. */
    assertEquals(thread1.getExitVal(), 0);
    assertEquals(thread2.getExitVal(), 0);
  }
  /*
   * Test the following case:
   *   1. Start a process, p1, which opens a read only standalone Environment
   *      on envHome, then sleeps.
   *   2. Start a new process, p2, which opens a read only standalone
   *      Environment on the same envHome.
   *   3. Both p1 and p2 should exit with value 0.
   */
  @Test
  public void testEnvReadOnEnvRead() throws Throwable {

    if (readPreserveRecordVersionProperty()) {
      return;
    }

    testEnvWriteOnEnvRead();

    /* Write some data. */
    JUnitProcessThread writeThread = getEnvProcess(false, true, "-1");
    writeThread.start();
    writeThread.finishTest();
    assertTrue(writeThread.getExitVal() == 0);

    JUnitProcessThread envThread1 = getEnvProcess(true, true);
    JUnitProcessThread envThread2 = getEnvProcess(true, false);

    startThreads(envThread1, envThread2, 500);

    checkExitVals(envThread1, 0, envThread2, 0);
  }