}
@Test
public void testCustomTailTrackLocation() throws Exception {
assertEquals(0, cappedTestCollection.count());
final MockEndpoint mock = getMockEndpoint("mock:test");
// get the custom tracking collection and drop it (tailTrackDb=einstein&tailTrackCollection=curie&tailTrackField=newton)
DBCollection trackingCol = mongo.getDB("einstein").getCollection("curie");
trackingCol.drop();
trackingCol = mongo.getDB("einstein").getCollection("curie");
// create a capped collection with max = 1000
cappedTestCollection = db.createCollection(cappedTestCollectionName,
BasicDBObjectBuilder.start().add("capped", true).add("size", 1000000000).add("max", 1000).get());
addTestRoutes();
context.startRoute("tailableCursorConsumer3");
mock.expectedMessageCount(300);
// pump 300 records
Thread t = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 1; i <= 300; i++) {
cappedTestCollection.insert(BasicDBObjectBuilder.start("increasing", i).add("string", "value" + i).get(), WriteConcern.SAFE);
}
}
});
// start the data pumping
t.start();
// before we continue wait for the data pump to end
t.join();
mock.assertIsSatisfied();
mock.reset();
// stop the route to ensure that our lastVal is persisted, and check it
context.stopRoute("tailableCursorConsumer3");
// ensure that the persisted lastVal is 300, newton is the name of the trackingField we are using
assertEquals(300, trackingCol.findOne(new BasicDBObject("persistentId", "darwin")).get("newton"));
context.startRoute("tailableCursorConsumer3");
// expect 300 messages and not 600
mock.expectedMessageCount(300);
// pump 300 records
t = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 301; i <= 600; i++) {
cappedTestCollection.insert(BasicDBObjectBuilder.start("increasing", i).add("string", "value" + i).get(), WriteConcern.SAFE);
}
}
});
// start the data pumping
t.start();
// before we continue wait for the data pump to end
t.join();
mock.assertIsSatisfied();
// check that the first received body contains increasing=301 and not increasing=1, i.e. it's not starting from the top
Object firstBody = mock.getExchanges().get(0).getIn().getBody();
assertTrue(firstBody instanceof DBObject);
assertEquals(301, ((DBObject) firstBody).get("increasing"));
// check that the persisted lastVal after stopping the route is 600, newton is the name of the trackingField we are using
context.stopRoute("tailableCursorConsumer3");
assertEquals(600, trackingCol.findOne(new BasicDBObject("persistentId", "darwin")).get("newton"));