Program: 160299
Project: 160299
Buggy source:
/** 
 * Simulate the following: - Add a few docs and do a pull replication - One doc on sync gateway is now updated - Do a second pull replication - Assert we get the updated doc and save it locally
 */
public Map<String,Object> mockMultiplePull(boolean shutdownMockWebserver,MockDispatcher.ServerType serverType) throws Exception {
  String doc1Id="String_Node_Str";
  boolean addAttachments=false;
  Map<String,Object> serverAndDispatcher=mockSinglePull(false,serverType,addAttachments);
  MockWebServer server=(MockWebServer)serverAndDispatcher.get("String_Node_Str");
  MockDispatcher dispatcher=(MockDispatcher)serverAndDispatcher.get("String_Node_Str");
  String doc1Rev="String_Node_Str";
  int doc1Seq=3;
  String checkpointRev="String_Node_Str";
  String checkpointLastSequence="String_Node_Str";
  MockCheckpointGet mockCheckpointGet=new MockCheckpointGet();
  mockCheckpointGet.setOk("String_Node_Str");
  mockCheckpointGet.setRev(checkpointRev);
  mockCheckpointGet.setLastSequence(checkpointLastSequence);
  dispatcher.enqueueResponse(MockHelper.PATH_REGEX_CHECKPOINT,mockCheckpointGet);
  MockChangesFeed mockChangesFeed=new MockChangesFeed();
  MockChangedDoc mockChangedDoc1=new MockChangedDoc().setSeq(doc1Seq).setDocId(doc1Id).setChangedRevIds(Arrays.asList(doc1Rev));
  mockChangesFeed.add(mockChangedDoc1);
  MockResponse fakeChangesResponse=mockChangesFeed.generateMockResponse();
  dispatcher.enqueueResponse(MockHelper.PATH_REGEX_CHANGES,fakeChangesResponse);
  Map<String,Object> doc1JsonMap=MockHelper.generateRandomJsonMap();
  MockDocumentGet mockDocumentGet=new MockDocumentGet().setDocId(doc1Id).setRev(doc1Rev).setJsonMap(doc1JsonMap);
  dispatcher.enqueueResponse("String_Node_Str",mockDocumentGet.generateMockResponse());
  MockCheckpointPut mockCheckpointPut=new MockCheckpointPut();
  dispatcher.enqueueResponse(MockHelper.PATH_REGEX_CHECKPOINT,mockCheckpointPut);
  doPullReplication(server.getUrl("String_Node_Str"));
  assertNotNull(database);
  Document doc1=database.getDocument(doc1Id);
  assertNotNull(doc1);
  assertNotNull(doc1.getCurrentRevisionId());
  assertTrue(doc1.getCurrentRevisionId().startsWith("String_Node_Str"));
  assertEquals(doc1JsonMap,doc1.getUserProperties());
  RecordedRequest getCheckpointRequest=server.takeRequest();
  assertTrue(getCheckpointRequest.getMethod().equals("String_Node_Str"));
  assertTrue(getCheckpointRequest.getPath().matches(MockHelper.PATH_REGEX_CHECKPOINT));
  RecordedRequest getChangesFeedRequest=server.takeRequest();
  if (serverType == MockDispatcher.ServerType.SYNC_GW) {
    assertTrue(getChangesFeedRequest.getMethod().equals("String_Node_Str"));
  }
 else {
    assertTrue(getChangesFeedRequest.getMethod().equals("String_Node_Str"));
  }
  assertTrue(getChangesFeedRequest.getPath().matches(MockHelper.PATH_REGEX_CHANGES));
  Log.d(TAG,"String_Node_Str",getChangesFeedRequest.getPath());
  RecordedRequest doc1Request=server.takeRequest();
  assertTrue(doc1Request.getMethod().equals("String_Node_Str"));
  assertTrue(doc1Request.getPath().matches("String_Node_Str"));
  RecordedRequest putCheckpointRequest=server.takeRequest();
  assertTrue(putCheckpointRequest.getMethod().equals("String_Node_Str"));
  assertTrue(putCheckpointRequest.getPath().matches(MockHelper.PATH_REGEX_CHECKPOINT));
  String utf8Body=putCheckpointRequest.getUtf8Body();
  Map<String,Object> checkpointJson=Manager.getObjectMapper().readValue(utf8Body,Map.class);
  assertEquals("String_Node_Str",checkpointJson.get("String_Node_Str"));
  assertEquals("String_Node_Str",checkpointJson.get("String_Node_Str"));
  if (shutdownMockWebserver) {
    server.shutdown();
  }
  Map<String,Object> returnVal=new HashMap<String,Object>();
  returnVal.put("String_Node_Str",server);
  returnVal.put("String_Node_Str",dispatcher);
  return returnVal;
}

Fixed version (ground truth):
/** 
 * Simulate the following: - Add a few docs and do a pull replication - One doc on sync gateway is now updated - Do a second pull replication - Assert we get the updated doc and save it locally
 */
public Map<String,Object> mockMultiplePull(boolean shutdownMockWebserver,MockDispatcher.ServerType serverType) throws Exception {
  String doc1Id="String_Node_Str";
  boolean addAttachments=false;
  Map<String,Object> serverAndDispatcher=mockSinglePull(false,serverType,addAttachments);
  MockWebServer server=(MockWebServer)serverAndDispatcher.get("String_Node_Str");
  MockDispatcher dispatcher=(MockDispatcher)serverAndDispatcher.get("String_Node_Str");
  dispatcher.reset();
  String doc1Rev="String_Node_Str";
  int doc1Seq=3;
  String checkpointRev="String_Node_Str";
  String checkpointLastSequence="String_Node_Str";
  MockCheckpointGet mockCheckpointGet=new MockCheckpointGet();
  mockCheckpointGet.setOk("String_Node_Str");
  mockCheckpointGet.setRev(checkpointRev);
  mockCheckpointGet.setLastSequence(checkpointLastSequence);
  dispatcher.enqueueResponse(MockHelper.PATH_REGEX_CHECKPOINT,mockCheckpointGet);
  MockChangesFeed mockChangesFeed=new MockChangesFeed();
  MockChangedDoc mockChangedDoc1=new MockChangedDoc().setSeq(doc1Seq).setDocId(doc1Id).setChangedRevIds(Arrays.asList(doc1Rev));
  mockChangesFeed.add(mockChangedDoc1);
  MockResponse fakeChangesResponse=mockChangesFeed.generateMockResponse();
  dispatcher.enqueueResponse(MockHelper.PATH_REGEX_CHANGES,fakeChangesResponse);
  Map<String,Object> doc1JsonMap=MockHelper.generateRandomJsonMap();
  MockDocumentGet mockDocumentGet=new MockDocumentGet().setDocId(doc1Id).setRev(doc1Rev).setJsonMap(doc1JsonMap);
  String doc1PathRegex="String_Node_Str";
  dispatcher.enqueueResponse(doc1PathRegex,mockDocumentGet.generateMockResponse());
  MockCheckpointPut mockCheckpointPut=new MockCheckpointPut();
  dispatcher.enqueueResponse(MockHelper.PATH_REGEX_CHECKPOINT,mockCheckpointPut);
  doPullReplication(server.getUrl("String_Node_Str"));
  assertNotNull(database);
  Document doc1=database.getDocument(doc1Id);
  assertNotNull(doc1);
  assertNotNull(doc1.getCurrentRevisionId());
  assertTrue(doc1.getCurrentRevisionId().startsWith("String_Node_Str"));
  assertEquals(doc1JsonMap,doc1.getUserProperties());
  RecordedRequest getCheckpointRequest=dispatcher.takeRequest(MockHelper.PATH_REGEX_CHECKPOINT);
  assertNotNull(getCheckpointRequest);
  assertTrue(getCheckpointRequest.getMethod().equals("String_Node_Str"));
  assertTrue(getCheckpointRequest.getPath().matches(MockHelper.PATH_REGEX_CHECKPOINT));
  RecordedRequest getChangesFeedRequest=dispatcher.takeRequest(MockHelper.PATH_REGEX_CHANGES);
  if (serverType == MockDispatcher.ServerType.SYNC_GW) {
    assertTrue(getChangesFeedRequest.getMethod().equals("String_Node_Str"));
  }
 else {
    assertTrue(getChangesFeedRequest.getMethod().equals("String_Node_Str"));
  }
  assertTrue(getChangesFeedRequest.getPath().matches(MockHelper.PATH_REGEX_CHANGES));
  Log.d(TAG,"String_Node_Str",getChangesFeedRequest.getPath());
  RecordedRequest doc1Request=dispatcher.takeRequest(doc1PathRegex);
  assertTrue(doc1Request.getMethod().equals("String_Node_Str"));
  assertTrue(doc1Request.getPath().matches("String_Node_Str"));
  Thread.sleep(2000);
  RecordedRequest putCheckpointRequest=dispatcher.takeRequest(MockHelper.PATH_REGEX_CHECKPOINT);
  assertNotNull(putCheckpointRequest);
  assertTrue(putCheckpointRequest.getMethod().equals("String_Node_Str"));
  assertTrue(putCheckpointRequest.getPath().matches(MockHelper.PATH_REGEX_CHECKPOINT));
  String utf8Body=putCheckpointRequest.getUtf8Body();
  Map<String,Object> checkpointJson=Manager.getObjectMapper().readValue(utf8Body,Map.class);
  assertEquals("String_Node_Str",checkpointJson.get("String_Node_Str"));
  assertEquals("String_Node_Str",checkpointJson.get("String_Node_Str"));
  if (shutdownMockWebserver) {
    server.shutdown();
  }
  Map<String,Object> returnVal=new HashMap<String,Object>();
  returnVal.put("String_Node_Str",server);
  returnVal.put("String_Node_Str",dispatcher);
  return returnVal;
}