Skip to content

Instantly share code, notes, and snippets.

@lwoodson
Last active November 20, 2015 01:19
Show Gist options
  • Select an option

  • Save lwoodson/8f22be5e720ea3e946c5 to your computer and use it in GitHub Desktop.

Select an option

Save lwoodson/8f22be5e720ea3e946c5 to your computer and use it in GitHub Desktop.
public class AttemptAssertion {
public static void attempt(timeout, wait, Callable testBody) {
int elapsed = 0;
Exception exception = null;
while (true) {
Thread.sleep(wait * 1000);
elapsed++;
try {
return testBody.call();
} catch (Exception e) {
exception = e;
}
if (elapsed > timeout) {
if (exception != null) {
throw exception;
} else {
throw new TimeoutException("Exceeded " + timeout + " seconds");
}
}
}
}
}
import static AttemptAssertion.attempt;
public class CountTest {
private DBI dbi;
private CountDAO countDao;
@BeforeMethod
public void setUp() {
// setup dbi
dao = dbi.onDemand(CountDAO.class);
}
@Test
public void testCounts() {
// make request to kick off process
attempt(60, 1, () -> {
int count = dao.getCountForRelationAndType(
JobStatusItem.class.simpleName(), id, "products");
assertEquals(count, 12345);
});
}
}
import static AttemptAssertion.attempt;
public class CountTest {
private CatalogProcessConfigFetcher configFetcher;
private Poller poller;
private CatalogProcessConfig syntheticConfig;
@BeforeMethod
public void setUp() {
configFetcher = // code to create our DAL layer config fetcher
}
@Test
public void testCounts() {
// make request to generate a config
attempt(60, 1, () -> {
syntheticConfig = configFetcher.fetchCatalogProcessConfig(
"higdonoutdoors", CatalogProcessType.SYNTHETIC_CATALOG);
assertEquals(syntheticConfig.getClientName(), "higdonoutdoors");
// other assertions...
});
callSomethingElse(syntheticConfig);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment