Skip to content
This repository was archived by the owner on Jan 31, 2022. It is now read-only.

Commit 297f805

Browse files
authored
Tests: Refactor to expose inner AssertionErrors (#122)
* Tests: Refactor to expose inner AssertionErrors * Tests: Print details when a test fails * Tests: Disable connectTimeout/DNS/KeepAlive testing on Travis as it is currently unreliable
1 parent a070e79 commit 297f805

File tree

4 files changed

+358
-283
lines changed

4 files changed

+358
-283
lines changed

algoliasearch/common.gradle

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,17 @@ android {
4848
// WARNING: Uncomment this line if you want to use the Debug configuration when developing against this module.
4949
// Otherwise, **leave it commented out** as we do **not** want to publish the Debug configuration.
5050
// publishNonDefault true
51+
52+
testOptions {
53+
unitTests.all {
54+
testLogging {
55+
events "skipped", "failed", "standardOut", "standardError"
56+
exceptionFormat "full"
57+
outputs.upToDateWhen { false }
58+
}
59+
}
5160
}
61+
}
5262
configurations {
5363
// Configuration solely created to make the Android classpath available when compiling Javadocs.
5464
// Taken from: <http://stackoverflow.com/questions/29663918/android-gradle-javadoc-annotation-does-not-exists>.
@@ -126,15 +136,15 @@ uploadArchives {
126136
// Maven Central repository.
127137
repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") {
128138
authentication(
129-
userName: project.hasProperty('nexusUsername') ? project['nexusUsername'] : 'FIXME',
130-
password: project.hasProperty('nexusPassword') ? project['nexusPassword'] : 'FIXME'
139+
userName: project.hasProperty('nexusUsername') ? project['nexusUsername'] : 'FIXME',
140+
password: project.hasProperty('nexusPassword') ? project['nexusPassword'] : 'FIXME'
131141
)
132142
}
133143
// Maven Central snapshot repository.
134144
snapshotRepository(url: "https://oss.sonatype.org/content/repositories/snapshots/") {
135145
authentication(
136-
userName: project.hasProperty('nexusUsername') ? project['nexusUsername'] : 'FIXME',
137-
password: project.hasProperty('nexusPassword') ? project['nexusPassword'] : 'FIXME'
146+
userName: project.hasProperty('nexusUsername') ? project['nexusUsername'] : 'FIXME',
147+
password: project.hasProperty('nexusPassword') ? project['nexusPassword'] : 'FIXME'
138148
)
139149
}
140150
}
@@ -189,4 +199,4 @@ def editPom(pom) {
189199
}
190200
}
191201
}
192-
}
202+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.algolia.search.saas;
2+
3+
import org.json.JSONObject;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
import static junit.framework.Assert.fail;
9+
10+
/**
11+
* This class helps coping with assertions in callbacks.
12+
* As AssertionErrors are thrown silently when an assertion fails in a callback,
13+
* this class wraps a CompletionHandler to check if an assert failed and expose the error through {@link AssertCompletionHandler#checkAssertions()}.
14+
*/
15+
abstract class AssertCompletionHandler implements CompletionHandler {
16+
private AssertionError error;
17+
private final CompletionHandler handler;
18+
private final List<AssertCompletionHandler> innerHandlers = new ArrayList<>();
19+
20+
public AssertCompletionHandler() {
21+
this.handler = new CompletionHandler() {
22+
@Override public void requestCompleted(JSONObject content, AlgoliaException error) {
23+
doRequestCompleted(content, error);
24+
}
25+
};
26+
}
27+
28+
abstract public void doRequestCompleted(JSONObject content, AlgoliaException error);
29+
30+
/**
31+
* Add an inner handler to be checked as well in {@link AssertCompletionHandler#checkAssertions()}.
32+
*/
33+
public void addInnerHandler(AssertCompletionHandler handler) {
34+
innerHandlers.add(handler);
35+
}
36+
37+
/**
38+
* Fail if the handler encountered at least one AssertionError.
39+
*/
40+
public void checkAssertions() {
41+
if (error != null) {
42+
fail("At least one assertion failed: " + error);
43+
}
44+
for (AssertCompletionHandler h : innerHandlers) {
45+
h.checkAssertions();
46+
}
47+
}
48+
49+
@Override final public void requestCompleted(JSONObject content, AlgoliaException error) {
50+
try {
51+
handler.requestCompleted(content, error);
52+
} catch (AssertionError e) {
53+
this.error = e;
54+
}
55+
}
56+
}

algoliasearch/src/test/java/com/algolia/search/saas/BrowseIteratorTest.java

Lines changed: 56 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,7 @@
3535

3636
import java.util.ArrayList;
3737
import java.util.List;
38-
import java.util.concurrent.CountDownLatch;
39-
import java.util.concurrent.TimeUnit;
4038

41-
import static junit.framework.Assert.assertTrue;
4239
import static junit.framework.Assert.fail;
4340

4441
public class BrowseIteratorTest extends PowerMockTestCase {
@@ -80,48 +77,81 @@ public void tearDown() throws Exception {
8077

8178
@Test
8279
public void testNominal() throws Exception {
83-
final CountDownLatch signal = new CountDownLatch(2);
84-
8580
Query query = new Query();
86-
BrowseIterator iterator = new BrowseIterator(index, query, new BrowseIterator.BrowseIteratorHandler() {
81+
AssertBrowseHandler handler = new AssertBrowseHandler() {
8782
@Override
88-
public void handleBatch(@NonNull BrowseIterator iterator, JSONObject result, AlgoliaException error) {
89-
if (error == null) {
90-
signal.countDown();
91-
if (signal.getCount() > 2) {
92-
fail("Should not reach this point");
93-
}
94-
} else {
83+
void doHandleBatch(BrowseIterator iterator, JSONObject result, AlgoliaException error) {
84+
if (error != null) {
9585
fail(error.getMessage());
9686
}
9787
}
98-
});
88+
};
89+
BrowseIterator iterator = new BrowseIterator(index, query, handler);
9990
iterator.start();
100-
assertTrue("No callback was called", signal.await(Helpers.wait, TimeUnit.SECONDS));
91+
handler.checkAssertions();
92+
handler.checkCalledMax(2);
10193
}
10294

10395
@Test
10496
public void testCancel() throws Exception {
105-
final CountDownLatch signal = new CountDownLatch(1);
106-
10797
Query query = new Query();
108-
BrowseIterator iterator = new BrowseIterator(index, query, new BrowseIterator.BrowseIteratorHandler() {
98+
AssertBrowseHandler handler = new AssertBrowseHandler() {
10999
@Override
110-
public void handleBatch(@NonNull BrowseIterator iterator, JSONObject result, AlgoliaException error) {
100+
void doHandleBatch(BrowseIterator iterator, JSONObject result, AlgoliaException error) {
111101
if (error == null) {
112-
signal.countDown();
113-
if (signal.getCount() == 1) {
102+
if (getCount() == 1) {
114103
iterator.cancel();
115104
}
116-
else if (signal.getCount() > 1) {
117-
fail("Should not reach this point");
118-
}
119105
} else {
120106
fail(error.getMessage());
121107
}
122108
}
123-
});
109+
};
110+
BrowseIterator iterator = new BrowseIterator(index, query, handler);
124111
iterator.start();
125-
assertTrue("No callback was called", signal.await(Helpers.wait, TimeUnit.SECONDS));
112+
handler.checkAssertions();
113+
handler.checkCalledMax(1);
114+
}
115+
116+
private abstract class AssertBrowseHandler implements BrowseIterator.BrowseIteratorHandler {
117+
118+
private AssertionError error;
119+
private int count;
120+
121+
abstract void doHandleBatch(BrowseIterator iterator, JSONObject result, AlgoliaException error);
122+
123+
/**
124+
* Fail if the handler encountered at least one AssertionError.
125+
*/
126+
public void checkAssertions() {
127+
if (error != null) {
128+
fail(error.getMessage());
129+
}
130+
}
131+
132+
/**
133+
* Fail if the handler was called more than a certain amount of time.
134+
* @param times the maximum allowed before failing.
135+
*/
136+
public void checkCalledMax(int times) {
137+
if (count > times) {
138+
fail("The BrowseIterator was called more than " + times + " times.");
139+
}
140+
}
141+
142+
@Override
143+
public void handleBatch(@NonNull BrowseIterator iterator, JSONObject result, AlgoliaException error) {
144+
count++;
145+
try {
146+
147+
doHandleBatch(iterator, result, error);
148+
} catch (AssertionError e) {
149+
this.error = e;
150+
}
151+
}
152+
153+
public int getCount() {
154+
return count;
155+
}
126156
}
127157
}

0 commit comments

Comments
 (0)