public void testReconnect() {
    List<DiscoveryNode> nodes = generateNodes();
    NodeConnectionsService service =
        new NodeConnectionsService(Settings.EMPTY, THREAD_POOL, transportService);

    ClusterState current = clusterStateFromNodes(Collections.emptyList());
    ClusterChangedEvent event =
        new ClusterChangedEvent("test", clusterStateFromNodes(randomSubsetOf(nodes)), current);

    transport.randomConnectionExceptions = true;

    service.connectToNodes(event.nodesDelta().addedNodes());

    for (int i = 0; i < 3; i++) {
      // simulate disconnects
      for (DiscoveryNode node : randomSubsetOf(nodes)) {
        transport.disconnectFromNode(node);
      }
      service.new ConnectionChecker().run();
    }

    // disable exceptions so things can be restored
    transport.randomConnectionExceptions = false;
    service.new ConnectionChecker().run();
    assertConnectedExactlyToNodes(event.state());
  }
  @Test
  public void testPrepareReachesBothTransports() throws JournalException, ServerException {
    createJournalWriterAndTransports();
    Date currentDate = parseDateString("20050316.144555.123");
    journalWriter.setCurrentDate(currentDate);

    journalWriter.prepareToWriteJournalEntry();
    assertCorrectNumberOfRequests(1, 0, 0, 0);
    assertCorrectCurrentDate(currentDate);
    assertEquals(
        "transport1 repository hash", server.getRepositoryHash(), transport1.getRepositoryHash());
    assertCorrectFilenames("fedoraJournal20050316.194555.123Z");
    assertEquals(
        "transport2 repository hash", server.getRepositoryHash(), transport2.getRepositoryHash());
    assertExpectedLogMessages("");
  }
  @Test
  public void testWriteReachesBothTransports() throws JournalException, ServerException {
    createJournalWriterAndTransports();
    journalWriter.setCurrentDate(parseDateString("20070218.085507.951"));

    journalWriter.prepareToWriteJournalEntry();
    assertCorrectNumberOfRequests(1, 0, 0, 0);

    journalWriter.writeJournalEntry(SampleJournalEntries.ENTRY_1);
    assertCorrectNumberOfRequests(1, 1, 0, 0);
    assertEquals(
        "transport1 journal file contents",
        SampleJournalFile1.FILE_CONTENTS,
        transport1.getFileContents());
    assertEquals(
        "transport2 journal file contents",
        SampleJournalFile1.FILE_CONTENTS,
        transport2.getFileContents());
  }
 /** Asserts that the expected retransmission requests match the actual ones */
 protected void assertXmitRequests(long... expected_seqnos) {
   List<Long> actual_xmit_reqs = transport.getXmitRequests();
   assert actual_xmit_reqs.size() == expected_seqnos.length
       : "size mismatch: expected="
           + Arrays.toString(expected_seqnos)
           + ", received="
           + actual_xmit_reqs;
   for (int i = 0; i < expected_seqnos.length; i++) {
     assert expected_seqnos[i] == actual_xmit_reqs.get(i)
         : "expected=" + Arrays.toString(expected_seqnos) + ", received=" + actual_xmit_reqs;
   }
   actual_xmit_reqs.clear();
 }
 private void assertCorrectNumberOfRequests(
     String name,
     MockTransport transport,
     int openFileRequests,
     int getWriterRequests,
     int closeFileRequests,
     int shutdownRequests) {
   assertEquals(
       name + " should get " + openFileRequests + " openFile() request(s)",
       openFileRequests,
       transport1.getHowManyOpenFileRequests());
   assertEquals(
       name + " should get " + getWriterRequests + " getWriter() request(s)",
       getWriterRequests,
       transport1.getHowManyGetWriterRequests());
   assertEquals(
       name + " should get " + closeFileRequests + " closeFile() request(s)",
       closeFileRequests,
       transport1.getHowManyCloseFileRequests());
   assertEquals(
       name + " should get " + shutdownRequests + " shutdown() request(s)",
       shutdownRequests,
       transport1.getHowManyShutdownRequests());
 }
  @Test
  public void testExceptionFromCriticalTransport() throws JournalException {
    createJournalWriterAndTransports();
    transport2.setThrowExceptionOnGetWriter(true);

    // Writing a journal entry should cause a high-level log message, and a
    // mode change.
    journalWriter.prepareToWriteJournalEntry();
    journalWriter.writeJournalEntry(SampleJournalEntries.ENTRY_1);
    assertCorrectNumberOfRequests(1, 1, 0, 0);
    assertEquals(
        "should be a mode change.", JournalOperatingMode.READ_ONLY, JournalOperatingMode.getMode());

    assertExpectedLogMessages(
        "FATAL - Exception thrown from " + "crucial Journal Transport: 'two'");
  }
  @Test
  public void testExceptionFromNonCriticalTransport() throws JournalException {
    createJournalWriterAndTransports();
    transport1.setThrowExceptionOnGetWriter(true);

    // Writing a journal entry should cause a high-level log message, but no
    // mode change.
    journalWriter.prepareToWriteJournalEntry();
    journalWriter.writeJournalEntry(SampleJournalEntries.ENTRY_1);
    assertCorrectNumberOfRequests(1, 1, 0, 0);
    assertEquals(
        "should be no mode change.", JournalOperatingMode.NORMAL, JournalOperatingMode.getMode());

    assertExpectedLogMessages(
        "ERROR - Exception thrown from " + "non-crucial Journal Transport: 'one'");
  }
 /** Confirm that both of the transports have the expected current date. */
 private void assertCorrectCurrentDate(Date currentDate) {
   assertEquals("transport1 date", currentDate, transport1.getCurrentDate());
   assertEquals("transport2 date", currentDate, transport2.getCurrentDate());
 }
 /** Confirm that both of the transports are using the expected filename. */
 private void assertCorrectFilenames(String filename) {
   assertEquals("transport1 filename", filename, transport1.getFilename());
   assertEquals("transport2 filename", filename, transport2.getFilename());
 }