}
@SuppressWarnings("unchecked")
public void testOnMessageListFetchFailed() {
WoTOwnIdentity author = mOwnIdentities[0];
Query q;
ObjectSet<FetchFailedMarker> markers;
ObjectSet<MessageList> messageLists;
MessageListFetchFailedMarker marker;
q = db.query();
q.constrain(FetchFailedMarker.class);
assertEquals(0, q.execute().size());
q = db.query();
q.constrain(MessageList.class);
assertEquals(0, q.execute().size());
mMessageManager.onMessageListFetchFailed(author, WoTMessageList.assembleURI(author.getRequestURI(), 1), FetchFailedMarker.Reason.DataNotFound);
q = db.query();
q.constrain(FetchFailedMarker.class);
assertEquals(1, q.execute().size());
mMessageManager.clearExpiredFetchFailedMarkers();
q = db.query();
q.constrain(FetchFailedMarker.class);
markers = q.execute();
assertEquals(1, markers.size());
marker = (MessageListFetchFailedMarker)markers.next();
assertTrue((CurrentTimeUTC.getInMillis() - marker.getDate().getTime()) < 10 * 1000);
assertEquals(marker.getDate().getTime() + MessageManager.MINIMAL_MESSAGELIST_FETCH_RETRY_DELAY, marker.getDateOfNextRetry().getTime());
assertEquals(false, marker.isRetryAllowedNow());
q = db.query();
q.constrain(MessageList.class);
messageLists = q.execute();
assertEquals(1, messageLists.size());
assertEquals(messageLists.next().getID(), marker.getMessageListID());
// Now we simulate a retry of the message list fetch
marker.setDateOfNextRetry(marker.getDate());
marker.setAllowRetryNow(false); // Needed for clearExpiredFetchFailedMarkers to process the marker
marker.storeWithoutCommit();
Persistent.checkedCommit(db, this);
mMessageManager.clearExpiredFetchFailedMarkers();
q = db.query();
q.constrain(FetchFailedMarker.class);
markers = q.execute();
assertEquals(1, markers.size());
assertEquals(marker, markers.next());
q = db.query();
q.constrain(MessageList.class);
messageLists = q.execute();
assertEquals(0, messageLists.size());
mMessageManager.onMessageListFetchFailed(author, WoTMessageList.assembleURI(author.getRequestURI(), 1), FetchFailedMarker.Reason.DataNotFound);
q = db.query();
q.constrain(FetchFailedMarker.class);
markers = q.execute();
assertEquals(1, markers.size());
assertEquals(marker, markers.next());
assertTrue((CurrentTimeUTC.getInMillis() - marker.getDate().getTime()) < 10 * 1000);
assertEquals(marker.getDate().getTime() + Math.min(MessageManager.MINIMAL_MESSAGELIST_FETCH_RETRY_DELAY*2, MessageManager.MAXIMAL_MESSAGELIST_FETCH_RETRY_DELAY),
marker.getDateOfNextRetry().getTime());
assertEquals(1, marker.getNumberOfRetries());
assertEquals(false, marker.isRetryAllowedNow());
q = db.query();
q.constrain(MessageList.class);
messageLists = q.execute();
assertEquals(1, messageLists.size());
assertEquals(messageLists.next().getID(), marker.getMessageListID());
// Simulate failure with existing marker and existing ghost message list, i.e. the message list fetcher tried to fetch even though it shouldn't.
mMessageManager.onMessageListFetchFailed(author, WoTMessageList.assembleURI(author.getRequestURI(), 1), FetchFailedMarker.Reason.DataNotFound);
q = db.query();
q.constrain(FetchFailedMarker.class);
markers = q.execute();
assertEquals(1, markers.size());
assertEquals(marker, markers.next());
q = db.query();
q.constrain(MessageList.class);
messageLists = q.execute();
assertEquals(1, messageLists.size());
assertEquals(messageLists.next().getID(), marker.getMessageListID());
}