Skip to content

Commit

Permalink
Fix flaky test (caelum#302)
Browse files Browse the repository at this point in the history
* Improve ordering of tags to make querying more predictable.
* The old ordering was causing our tests to randomly fail.

* Log that we're initializing the database, as this step takes a long time.

* Make error message a little more friendly.
  • Loading branch information
Cosmin Stroe authored and felipeweb committed Apr 6, 2018
1 parent e14f1ec commit a42dfeb
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/mamute/dao/TagDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public List<Tag> search(String term){
public List<TagUsage> getRecentTagsSince(DateTime since, int maxResult) {
Query query = session.createQuery("select new org.mamute.model.TagUsage(tag, count(question)) from Question question " +
"join question.information.tags tag " +
"where question.lastUpdatedAt > :since group by tag order by count(question) desc");
"where question.lastUpdatedAt > :since group by tag order by count(question) desc, name");
query.setParameter("since", since);
return query.setMaxResults(maxResult).list();
}
Expand Down
4 changes: 4 additions & 0 deletions src/test/java/org/mamute/dao/DatabaseTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import javax.validation.ValidatorFactory;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.After;
Expand All @@ -21,6 +22,7 @@

@SuppressWarnings("unchecked")
public abstract class DatabaseTestCase extends CDITestCase{
private static Logger LOG = Logger.getLogger(DatabaseTestCase.class);
protected static final SessionFactory factory;
private static final SessionFactoryCreator creator;
protected Session session;
Expand All @@ -33,8 +35,10 @@ public abstract class DatabaseTestCase extends CDITestCase{
MamuteDatabaseConfiguration configuration = new MamuteDatabaseConfiguration(testing, vf, null);
configuration.init();
creator = new SessionFactoryCreator(configuration);
LOG.info("Initializing database (SessionFactoryCreator) ...");
creator.init();
factory = creator.getInstance();
LOG.info("Database initialized.");
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/mamute/dao/TagDAOTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public void should_load_recent_tags_used() {
assertEquals(2, recentTagsUsage.size());
assertEquals(2L, recentTagsUsage.get(0).getUsage().longValue());
assertEquals(1L, recentTagsUsage.get(1).getUsage().longValue());
assertEquals(java.getId(), recentTagsUsage.get(0).getTag().getId());
assertEquals(ruby.getId(), recentTagsUsage.get(1).getTag().getId());
assertEquals("java tag should be the most used", java.getId(), recentTagsUsage.get(0).getTag().getId());
assertEquals("ruby tag should be the second-most used", ruby.getId(), recentTagsUsage.get(1).getTag().getId());
}

@Test
Expand Down

0 comments on commit a42dfeb

Please sign in to comment.