@Test
public void testWithBackup() throws Exception {

  boolean exception = false;
  try {
    manager.getFileSetViewInfo(FILE_SET);
  } catch (TerrapinGetException e) {
    assertEquals(TerrapinGetErrorCode.FILE_SET_NOT_FOUND, e.getErrorCode());
    exception = true;
  }
  assertTrue(exception);
  // Now, a fileset watch is triggered but the external view is not triggered.
  FileSetInfo fsInfo1 = new FileSetInfo(
      "file_set", "/terrapin/data/file_set/1", 1, (List)Lists.newArrayList(), new Options());
  triggerFileSetInfoWatch(FILE_SET, fsInfo1);
  exception = false;
  try {
    manager.getFileSetViewInfo(FILE_SET);
  } catch (TerrapinGetException e) {
    assertEquals(TerrapinGetErrorCode.INVALID_FILE_SET_VIEW, e.getErrorCode());
    exception = true;
  }
  assertTrue(exception);
  // Now the watch for the first view is triggered.
  ExternalView externalView1 = new ExternalView("$terrapin$data$file_set$1");
  externalView1.setStateMap("0", ImmutableMap.of("host1", "ONLINE"));
  ViewInfo viewInfo1 = new ViewInfo(externalView1);
  triggerViewInfoWatch(viewInfo1);
  Pair<FileSetInfo, ViewInfo> pair = manager.getFileSetViewInfo(FILE_SET);
  assertEquals(fsInfo1, pair.getLeft());
  assertEquals(viewInfo1, pair.getRight());
  // The fileset info alone gets updated but the other watch is not yet triggered.
  FileSetInfo fsInfo2 = new FileSetInfo(
      FILE_SET, "/terrapin/data/file_set/2", 1, (List)Lists.newArrayList(), new Options());
  triggerFileSetInfoWatch(FILE_SET, fsInfo2);
  pair = manager.getFileSetViewInfo(FILE_SET);
  // We should see the view from the backup only.
  assertEquals(fsInfo1, pair.getLeft());
  assertEquals(viewInfo1, pair.getRight());
  // Finally, a watch is triggered for the resource corresponding to the second fileset.
  ExternalView externalView2 = new ExternalView("$terrapin$data$file_set$2");
  externalView2.setStateMap("0", ImmutableMap.of("host2", "ONLINE"));
  ViewInfo viewInfo2 = new ViewInfo(externalView2);
  triggerViewInfoWatch(viewInfo2);
  pair = manager.getFileSetViewInfo(FILE_SET);
  assertEquals(fsInfo2, pair.getLeft());
  assertEquals(viewInfo2, pair.getRight());
  // Watches execute in the correct order. Firs the view is propagated.
  ExternalView externalView3 = new ExternalView("$terrapin$data$file_set$3");
  externalView3.setStateMap("0", ImmutableMap.of("host3", "ONLINE"));
  ViewInfo viewInfo3 = new ViewInfo(externalView3);
  triggerViewInfoWatch(viewInfo3);
  // We should still continue to serve previous view.
  pair = manager.getFileSetViewInfo(FILE_SET);
  assertEquals(fsInfo2, pair.getLeft());
  assertEquals(viewInfo2, pair.getRight());
  // Finally, the fileset version update watch is propagated.
  FileSetInfo fsInfo3 = new FileSetInfo(
      FILE_SET, "/terrapin/data/file_set/3", 1, (List)Lists.newArrayList(), new Options());
  triggerFileSetInfoWatch(FILE_SET, fsInfo3);
  pair = manager.getFileSetViewInfo(FILE_SET);
  assertEquals(fsInfo3, pair.getLeft());
  assertEquals(viewInfo3, pair.getRight());
}