Skip to content

Commit

Permalink
Development: Tune data export test to prevent flakyness (#9155)
Browse files Browse the repository at this point in the history
  • Loading branch information
N0W0RK authored Aug 23, 2024
1 parent 4dc60a8 commit d5d999f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ public class ScienceUtilService {
* @param identity The login of the user associated with the event.
* @param type The type of the event.
* @param resourceId The id of the resource associated with the event.
* @param timestamp The timestamp of the event.
*/
public ScienceEvent createScienceEvent(String identity, ScienceEventType type, Long resourceId) {
public ScienceEvent createScienceEvent(String identity, ScienceEventType type, Long resourceId, ZonedDateTime timestamp) {
ScienceEvent event = new ScienceEvent();
event.setIdentity(identity);
event.setTimestamp(ZonedDateTime.now());
event.setTimestamp(timestamp);
event.setType(type);
event.setResourceId(resourceId);
return scienceEventRepository.save(event);
Expand All @@ -39,14 +40,7 @@ public ScienceEvent createScienceEvent(String identity, ScienceEventType type, L
*/
public static Comparator<ScienceEvent> scienceEventComparator = Comparator.comparing(ScienceEvent::getResourceId).thenComparing(ScienceEvent::getType)
.thenComparing((ScienceEvent e1, ScienceEvent e2) -> {

Duration d = Duration.between(e1.getTimestamp(), e2.getTimestamp());
if (d.toNanos() > 500) {
return 1;
}
else if (d.toNanos() < -500) {
return -1;
}
return 0;
});
Duration duration = Duration.between(e1.getTimestamp(), e2.getTimestamp());
return Math.abs(duration.toNanos()) < 1e5 ? 0 : duration.isNegative() ? -1 : 1;
}).thenComparing(ScienceEvent::getIdentity);
}
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ void initTestCase() throws IOException {

apollonRequestMockProvider.enableMockingOfRequests();

// mock apollon conversion 8 times, because the last test includes 8 modeling exercises, because each test adds modeling exercises
// mock apollon conversion 8 times, because the last test includes 8 modeling
// exercises, because each test adds modeling exercises
for (int i = 0; i < 8; i++) {
mockApollonConversion();
}
Expand Down Expand Up @@ -230,7 +231,6 @@ private void assertCommunicationDataCsvFile(Path courseDirPath) {
/**
* Asserts the content of the science events CSV file.
* Allows for a 500ns difference between the timestamps due to the reimport from the csv export.
* Might cause the test to be flaky if multiple events are created overlapping and matched wrongly.
*
* @param extractedZipDirPath The path to the extracted zip directory
* @param events The set of science events to compare with the content of the CSV file
Expand All @@ -249,13 +249,13 @@ private void assertScienceEventsCSVFile(Path extractedZipDirPath, Set<ScienceEve
scienceEvent.setTimestamp(ZonedDateTime.parse(record.get("timestamp")));
scienceEvent.setType(ScienceEventType.valueOf(record.get("event_type")));
scienceEvent.setResourceId(Long.parseLong(record.get("resource_id")));
scienceEvent.setIdentity(TEST_PREFIX + "student1");
actual.add(scienceEvent);
}
}
catch (IOException e) {
fail("Failed while reading science events CSV file");
}

assertThat(actual).usingElementComparator(ScienceUtilService.scienceEventComparator).containsExactlyInAnyOrderElementsOf(events);
}

Expand Down Expand Up @@ -332,9 +332,13 @@ private void createPlagiarismData(String userLogin, ProgrammingExercise programm
}

private Set<ScienceEvent> createScienceEvents(String userLogin) {
return Set.of(scienceUtilService.createScienceEvent(userLogin, ScienceEventType.EXERCISE__OPEN, 1L),
scienceUtilService.createScienceEvent(userLogin, ScienceEventType.LECTURE__OPEN, 2L),
scienceUtilService.createScienceEvent(userLogin, ScienceEventType.LECTURE__OPEN_UNIT, 3L));

ZonedDateTime timestamp = ZonedDateTime.now();
// Rounding timestamp due to rounding during export
timestamp = timestamp.withNano(timestamp.getNano() - timestamp.getNano() % 10000);
return Set.of(scienceUtilService.createScienceEvent(userLogin, ScienceEventType.EXERCISE__OPEN, 1L, timestamp),
scienceUtilService.createScienceEvent(userLogin, ScienceEventType.LECTURE__OPEN, 2L, timestamp.plusMinutes(1)),
scienceUtilService.createScienceEvent(userLogin, ScienceEventType.LECTURE__OPEN_UNIT, 3L, timestamp.plusSeconds(30)));

}

Expand Down Expand Up @@ -635,7 +639,8 @@ void testDataExportContainsDataAboutCourseStudentUnenrolled() throws Exception {
var course = prepareCourseDataForDataExportCreation(assessmentDueDateInTheFuture, courseShortName);
conversationUtilService.addOneMessageForUserInCourse(TEST_PREFIX + "student1", course, "only one post");
var dataExport = initDataExport();
// by setting the course groups to a different value we simulate unenrollment because the user is no longer part of the user group and hence, the course.
// by setting the course groups to a different value, we simulate unenrollment
// because the user is no longer part of the user group and hence, the course.
courseUtilService.updateCourseGroups("abc", course, "");
dataExportCreationService.createDataExport(dataExport);
var dataExportFromDb = dataExportRepository.findByIdElseThrow(dataExport.getId());
Expand Down

0 comments on commit d5d999f

Please sign in to comment.