Skip to content

Commit

Permalink
Automate testing too, and fix flaky and slow tests
Browse files Browse the repository at this point in the history
  • Loading branch information
steventrouble committed Oct 26, 2023
1 parent ba4e19f commit 8250ce4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ jobs:
distribution: 'temurin'
cache: maven
- name: Build with Maven
run: mvn --no-transfer-progress clean compile package
run: mvn --no-transfer-progress clean compile test package
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:test-db-context.xml")
public class DBFilterPolicyDAOTest {
/**
* Amount of time for tests to sleep waiting for DB operations.
*
* Set as low as possible.
*/
private static int SLEEP_DELAY_MS = 10;

private final Logger log = LoggerFactory.getLogger(getClass());

@Autowired
Expand Down Expand Up @@ -201,7 +208,7 @@ public void testCreateFilterPolicy() throws Exception {
// check that there's nothing in there already
Assert.assertEquals(0, qResults.size());
Timestamp preUpdateTime = new Timestamp(new Date().getTime());
Thread.sleep(500);
Thread.sleep(SLEEP_DELAY_MS);
attributeFilterPolicy.setUuid(genUUID());
dao.createFilterPolicy(filterPolicyGroup, attributeFilterPolicy, remoteUser);

Expand Down Expand Up @@ -229,7 +236,7 @@ public void testCreateFilterPolicyPreviouslyDeleted() throws Exception {
, Timestamp.class);
Assert.assertEquals(0, qResults.size());
Timestamp preUpdateTime = new Timestamp(new Date().getTime());
Thread.sleep(500);
Thread.sleep(SLEEP_DELAY_MS);
dao.updateFilterPolicies(filterPolicyGroup, Arrays.asList(attributeFilterPolicy), remoteUser);

qResults = template.queryForList("select start_time from filter where entity_id = ? and end_time is null"
Expand All @@ -256,7 +263,7 @@ public void testUpdateFilterPolicy() throws Exception {
Assert.assertEquals(1, qResults.size());
Timestamp preUpdateTime = qResults.get(0);
//it's too fast!
Thread.sleep(500);
Thread.sleep(SLEEP_DELAY_MS);
dao.updateFilterPolicy(filterPolicyGroup, attributeFilterPolicy, remoteUser);

qResults = template.queryForList("select start_time from filter where entity_id = ? and end_time is null"
Expand Down Expand Up @@ -284,25 +291,21 @@ public void testUpdateFilterPolicies() throws Exception {
, new MapSqlParameterSource().addValue("ids", entityIds)
, Timestamp.class);
Assert.assertEquals(2, qResults.size());
Timestamp preUpdateTime = new Timestamp(new Date().getTime());
Thread.sleep(500);
Thread.sleep(SLEEP_DELAY_MS);

List<AttributeFilterPolicy> updatePolicies = new ArrayList<>();
updatePolicies.add(fakeAttributeFilterPolicy(filterPolicyGroup, updateEntityId));
updatePolicies.add(fakeAttributeFilterPolicy(filterPolicyGroup, createEntityId));

dao.updateFilterPolicies(filterPolicyGroup, updatePolicies, remoteUser);

qResults = namedTemplate.queryForList("select start_time from filter where end_time is null and entity_id in (:ids)"
// The two updated policies should have later start_times than other one
qResults = namedTemplate.queryForList(
"select start_time from filter where end_time is null and entity_id in (:ids)" +
" and start_time > (select min(start_time) from filter where end_time is null)"
, new MapSqlParameterSource().addValue("ids", entityIds)
, Timestamp.class);
Assert.assertEquals(3, qResults.size());
int updateCount = 0;
for(Timestamp result : qResults){
if(result.after(preUpdateTime))
updateCount++;
}
Assert.assertEquals(2, updateCount);
Assert.assertEquals(2, qResults.size());

//clean up
namedTemplate.update("delete from filter where entity_id in (:ids)",
Expand Down

0 comments on commit 8250ce4

Please sign in to comment.