making SensorFixStoreTest more robust by using MongoDB ClientSessions

This commit is contained in:
Axel Uhl committed 2024-08-24 23:04:42 +02:00
1 parent 12e41557a5
commit d3cdbb3d4b
2 files changed
+20 -9

No files matched your search

@@ -73,6 +73,12 @@ public class MongoSensorFixStoreImpl extends MongoFixHandler implements MongoSen
*/
private final WriteConcern writeConcern;
/**
* The read concern to use for reading/loading fixes and metadata. Tests can use this, e.g., to work with
* {@link ReadConcern#MAJORITY} in order to ensure they can read their own writes.
*/
private final ReadConcern readConcern;
/**
* Lock object to be used when accessing {@link #listeners}.
*/
@@ -94,6 +100,7 @@ public class MongoSensorFixStoreImpl extends MongoFixHandler implements MongoSen
fixesCollection = mongoOF.getGPSFixCollection(clientSession);
metadataCollection = new MetadataCollection(mongoOF, fixServiceFinder, deviceServiceFinder, readConcern, writeConcern, metadataCollectionClientSession);
this.writeConcern = writeConcern;
this.readConcern = readConcern;
this.clientSession = clientSession;
}
@@ -118,16 +125,14 @@ public class MongoSensorFixStoreImpl extends MongoFixHandler implements MongoSen
@Override
public <FixT extends Timed> void loadFixes(Consumer<FixT> consumer, DeviceIdentifier device, TimePoint from,
TimePoint to, boolean inclusive) throws NoCorrespondingServiceRegisteredException, TransformationException {
loadFixes(consumer, device, from, to, inclusive, () -> false, (d) -> {
});
loadFixes(consumer, device, from, to, inclusive, () -> false, /* progress consumer */ d -> {});
}
@Override
public <FixT extends Timed> void loadFixes(Consumer<FixT> consumer, DeviceIdentifier device, TimePoint from,
TimePoint to, boolean inclusive, BooleanSupplier isPreemptiveStopped, Consumer<Double> progressConsumer)
throws NoCorrespondingServiceRegisteredException, TransformationException {
loadFixes(consumer, device, from, to, inclusive, isPreemptiveStopped, progressConsumer,
true, false);
loadFixes(consumer, device, from, to, inclusive, isPreemptiveStopped, progressConsumer, true, false);
}
private <FixT extends Timed> boolean loadFixes(Consumer<FixT> consumer, DeviceIdentifier device, TimePoint from,
@@ -147,7 +152,8 @@ public class MongoSensorFixStoreImpl extends MongoFixHandler implements MongoSen
filters.add(Filters.lt(FieldNames.TIME_AS_MILLIS.name(), loadFixesTo.asMillis()));
}
Bson query = Filters.and(filters);
FindIterable<Document> result = clientSession == null ? fixesCollection.find(query) : fixesCollection.find(clientSession, query);
final MongoCollection<Document> fixesCollectionWithReadConcern = fixesCollection.withReadConcern(readConcern);
FindIterable<Document> result = clientSession == null ? fixesCollectionWithReadConcern.find(query) : fixesCollectionWithReadConcern.find(clientSession, query);
result.batchSize(100000).sort(new Document(FieldNames.TIME_AS_MILLIS.name(), ascending ? 1 : -1));
if (onlyOneResult) {
result.limit(1);
@@ -16,6 +16,7 @@ import org.mockito.Mockito;
import com.mongodb.MongoException;
import com.mongodb.ReadConcern;
import com.mongodb.WriteConcern;
import com.mongodb.client.ClientSession;
import com.mongodb.client.MongoDatabase;
import com.sap.sailing.domain.common.DeviceIdentifier;
import com.sap.sailing.domain.common.sensordata.BravoSensorDataMetadata;
@@ -33,6 +34,7 @@ import com.sap.sse.common.Timed;
import com.sap.sse.common.TransformationException;
import com.sap.sse.common.impl.MillisecondsTimePoint;
import com.sap.sse.common.impl.TimeRangeImpl;
import com.sap.sse.mongodb.MongoDBService;
public class SensorFixStoreTest {
private static final long FIX_TIMESTAMP = 110;
@@ -43,9 +45,13 @@ public class SensorFixStoreTest {
protected final DeviceIdentifier device = new SmartphoneImeiIdentifier("a");
protected final DeviceIdentifier device2 = new SmartphoneImeiIdentifier("b");
protected SensorFixStore store;
private ClientSession clientSession;
private ClientSession metadataCollectionClientSession;
@Before
public void setUp() throws UnknownHostException, MongoException {
clientSession = MongoDBService.INSTANCE.startCausallyConsistentSession();
metadataCollectionClientSession = MongoDBService.INSTANCE.startCausallyConsistentSession();
dropPersistedData();
newStore();
}
@@ -53,7 +59,7 @@ public class SensorFixStoreTest {
private void newStore() {
store = new MongoSensorFixStoreImpl(PersistenceFactory.INSTANCE.getDefaultMongoObjectFactory(),
PersistenceFactory.INSTANCE.getDefaultDomainObjectFactory(), serviceFinderFactory, ReadConcern.MAJORITY,
WriteConcern.MAJORITY, /* clientSession */ null, /* metadataCollectionClientSession */ null);
WriteConcern.MAJORITY, clientSession, metadataCollectionClientSession);
}
@After
@@ -63,8 +69,8 @@ public class SensorFixStoreTest {
private void dropPersistedData() {
MongoDatabase db = PersistenceFactory.INSTANCE.getDefaultMongoObjectFactory().getDatabase();
db.getCollection(CollectionNames.GPS_FIXES.name()).drop();
db.getCollection(CollectionNames.GPS_FIXES_METADATA.name()).drop();
db.getCollection(CollectionNames.GPS_FIXES.name()).withWriteConcern(WriteConcern.MAJORITY).drop(clientSession);
db.getCollection(CollectionNames.GPS_FIXES_METADATA.name()).withWriteConcern(WriteConcern.MAJORITY).drop(metadataCollectionClientSession);
}
@Test
@@ -87,7 +93,6 @@ public class SensorFixStoreTest {
@Test
public void testFixDataIsPreservedOnStore() throws Exception {
DoubleVectorFix fix = addBravoFix(device, FIX_TIMESTAMP, FIX_RIDE_HEIGHT);
verifySingleFix(fix, 100, 200, device, true);
}