         site.guid = "abc\(i)def"
 
         history.insertOrUpdatePlace(site.asPlace(), modified: 1234567890).value
-
         for j in 0...20 {
-            let local = SiteVisit(site: site, date: Timestamp(1000 * (1437088398461 + (1000 * i) + j)), type: VisitType.Link)
-            XCTAssertTrue(history.addLocalVisit(local).value.isSuccess)
-        }
-
-        var remotes = [Visit]()
-        for j in 0...20 {
-            remotes.append(SiteVisit(site: site, date: Timestamp(1000 * (1437088399461 + (1000 * i) + j)), type: VisitType.Link))
-        }
-        history.storeRemoteVisits(remotes, forGUID: site.guid!).value
+            let visitTime = Timestamp(1000 * (1437088398461 + (1000 * i) + j))
+            addVisitForSite(site, intoHistory: history, from: .Local, atTime: visitTime)
+            addVisitForSite(site, intoHistory: history, from: .Remote, atTime: visitTime)
+        }
+    }
+}
+
+private func addVisitForSite(site: Site, intoHistory history: SQLiteHistory, from: VisitOrigin, atTime: Timestamp) {
+    let visit = SiteVisit(site: site, date: atTime, type: VisitType.Link)
+    switch from {
+    case .Local:
+            history.addLocalVisit(visit).value
+    case .Remote:
+        history.storeRemoteVisits([visit], forGUID: site.guid!).value
     }
 }
        let prefs = MockProfilePrefs()
        let history = SQLiteHistory(db: db, prefs: prefs)!

        // Make sure that we get back the top sites
        XCTAssertNil(prefs.timestampForKey(PrefsKeys.KeyLastFrecencyCacheTime))
        populateHistoryForFrecencyCalcuations(history, siteCount: 100)

        let expectation = self.expectationWithDescription("First.")
        func done() -> Success {

            return succeed()
        }

        func loadCache() -> Success {
            return history.invalidateTopSitesIfNeeded().bind { result in
                XCTAssertTrue(result, "Invalidate should be successful")
                return succeed()
            }
        }

        func checkTopSitesReturnsResults() -> Success {
            return history.getTopSitesWithLimit(20) >>== { topSites in
                XCTAssertEqual(topSites.count, 20)
                XCTAssertEqual(topSites[0]!.guid, "abc\(5)def")
                return succeed()
            }
        }

        func invalidateIfNeededDoesntChangeResults() -> Success {
            return history.invalidateTopSitesIfNeeded().bind { result in
                return history.getTopSitesWithLimit(20) >>== { topSites in
                    XCTAssertEqual(topSites.count, 20)
                    XCTAssertEqual(topSites[0]!.guid, "abc\(5)def")
                    return succeed()
                }
            }
        }

        func addVisitsToZerothSite() -> Success {
            let site = Site(url: "http://s\(0)ite\(0)/foo", title: "A \(0)")
            site.guid = "abc\(0)def"
            for i in 0...20 {
                addVisitForSite(site, intoHistory: history, from: .Local, atTime: Timestamp(1439088398461 + (1000 * i)))
            }
            return succeed()
        }

        func markInvalidation() -> Success {
            history.setTopSitesNeedsInvalidation()
            return succeed()
        }

        func checkSitesInvalidate() -> Success {
            history.invalidateTopSitesIfNeeded().value

            return history.getTopSitesWithLimit(20) >>== { topSites in
                XCTAssertEqual(topSites.count, 20)
                XCTAssertEqual(topSites[0]!.guid, "abc\(0)def")
                return succeed()
            }
        }

        loadCache()
            >>> checkTopSitesReturnsResults
            >>> invalidateIfNeededDoesntChangeResults
            >>> markInvalidation
            >>> addVisitsToZerothSite
            >>> checkSitesInvalidate
            >>> done

        waitForExpectationsWithTimeout(10.0) { error in
