Skip to content

Commit

Permalink
#130 - Harden test.
Browse files Browse the repository at this point in the history
  • Loading branch information
markrmiller committed Jul 14, 2020
1 parent 472aafa commit c968122
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.solr.client.solrj.routing;

import java.io.Closeable;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
Expand All @@ -30,7 +31,7 @@
* either explicitly configured hash routing parameter, or the hash of a query parameter (configurable, usually related
* to the main query).
*/
public class AffinityReplicaListTransformer implements ReplicaListTransformer {
public class AffinityReplicaListTransformer implements ReplicaListTransformer, Closeable {

private final int routingDividend;

Expand Down Expand Up @@ -83,6 +84,11 @@ public void transform(List<?> choices) {
}
}

@Override
public void close() {

}

private static final class SortableChoice {

private final Object choice;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,7 @@ public interface ReplicaListTransformer {
*/
public void transform(List<?> choices);

default void close() {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.solr.client.solrj.routing;

import java.io.Closeable;
import java.lang.invoke.MethodHandles;
import java.util.Arrays;
import java.util.Iterator;
Expand Down Expand Up @@ -124,7 +125,7 @@ public ReplicaListTransformer getReplicaListTransformer(final SolrParams request
* and subsequently reordering any equivalent replica sets according to behavior specified
* by the baseReplicaListTransformer.
*/
private static final class TopLevelReplicaListTransformer implements ReplicaListTransformer {
private static final class TopLevelReplicaListTransformer implements ReplicaListTransformer, Closeable {

private final NodePreferenceRulesComparator replicaComp;
private final ReplicaListTransformer baseReplicaListTransformer;
Expand Down Expand Up @@ -185,6 +186,11 @@ public void transform(List<?> choices) {
}
}
}

@Override
public void close() {

}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
*/
package org.apache.solr.client.solrj.routing;

import java.io.Closeable;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class ShufflingReplicaListTransformer implements ReplicaListTransformer {
public class ShufflingReplicaListTransformer implements ReplicaListTransformer, Closeable {

private final Random r;

Expand All @@ -36,4 +37,9 @@ public void transform(List<?> choices)
}
}

@Override
public void close() {

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.solr.client.solrj.routing;

import java.io.Closeable;
import java.lang.invoke.MethodHandles;

import java.util.ArrayList;
Expand All @@ -41,7 +42,7 @@ public class ReplicaListTransformerTest extends SolrTestCase {
private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

// A transformer that keeps only matching choices
private static class ToyMatchingReplicaListTransformer implements ReplicaListTransformer {
private static class ToyMatchingReplicaListTransformer implements ReplicaListTransformer, Closeable {

private final String regex;

Expand Down Expand Up @@ -73,10 +74,15 @@ else if (choice instanceof Replica) {
}
}

@Override
public void close() {

}

}

// A transformer that makes no transformation
private static class ToyNoOpReplicaListTransformer implements ReplicaListTransformer {
private static class ToyNoOpReplicaListTransformer implements ReplicaListTransformer, Closeable {

public ToyNoOpReplicaListTransformer()
{
Expand All @@ -88,79 +94,86 @@ public void transform(List<?> choices)
log.info("No-Op transform ignoring input: {}", choices);
}

@Override
public void close() {

}

}

@Test
public void testTransform() throws Exception {

final String regex = ".*" + random().nextInt(10) + ".*";

final ReplicaListTransformer transformer;
if (random().nextBoolean()) {
log.info("Using ToyMatching Transfomer");
transformer = new ToyMatchingReplicaListTransformer(regex);
ReplicaListTransformer transformer = null;
try {
if (random().nextBoolean()) {
log.info("Using ToyMatching Transfomer");
transformer = new ToyMatchingReplicaListTransformer(regex);

} else {
log.info("Using conditional Transfomer");
transformer = new HttpShardHandlerFactory() {
} else {
log.info("Using conditional Transfomer");
transformer = new HttpShardHandlerFactory() {

@Override
protected ReplicaListTransformer getReplicaListTransformer(final SolrQueryRequest req)
{
final SolrParams params = req.getParams();
@Override
protected ReplicaListTransformer getReplicaListTransformer(final SolrQueryRequest req) {
final SolrParams params = req.getParams();

if (params.getBool("toyNoTransform", false)) {
return new ToyNoOpReplicaListTransformer();
}
if (params.getBool("toyNoTransform", false)) {
return new ToyNoOpReplicaListTransformer();
}

final String regex = params.get("toyRegEx");
if (regex != null) {
return new ToyMatchingReplicaListTransformer(regex);
final String regex = params.get("toyRegEx");
if (regex != null) {
return new ToyMatchingReplicaListTransformer(regex);
}

return super.getReplicaListTransformer(req);
}

return super.getReplicaListTransformer(req);
}
}.getReplicaListTransformer(
new LocalSolrQueryRequest(null,
new ModifiableSolrParams().add("toyRegEx", regex)));
}

}.getReplicaListTransformer(
new LocalSolrQueryRequest(null,
new ModifiableSolrParams().add("toyRegEx", regex)));
}
final List<Replica> inputs = new ArrayList<>();
final List<Replica> expectedTransformed = new ArrayList<>();

final List<String> urls = createRandomUrls();
for (int ii = 0; ii < urls.size(); ++ii) {

final String name = "replica" + (ii + 1);
final String url = urls.get(ii);
final Map<String, Object> propMap = new HashMap<String, Object>();
propMap.put("base_url", url);
propMap.put("core", "test_core");
propMap.put("node_name", "test_node");
propMap.put("type", "NRT");
// a skeleton replica, good enough for this test's purposes
final Replica replica = new Replica(name, propMap, "c1", "s1");

inputs.add(replica);
final String coreUrl = replica.getCoreUrl();
if (coreUrl.matches(regex)) {
log.info("adding replica=[{}] to expected due to core url ({}) regex match on {} ",
replica, coreUrl, regex);
expectedTransformed.add(replica);
} else {
log.info("NOT expecting replica=[{}] due to core url ({}) regex mismatch ({})",
replica, coreUrl, regex);
}

final List<Replica> inputs = new ArrayList<>();
final List<Replica> expectedTransformed = new ArrayList<>();

final List<String> urls = createRandomUrls();
for (int ii=0; ii<urls.size(); ++ii) {

final String name = "replica"+(ii+1);
final String url = urls.get(ii);
final Map<String,Object> propMap = new HashMap<String,Object>();
propMap.put("base_url", url);
propMap.put("core", "test_core");
propMap.put("node_name", "test_node");
propMap.put("type", "NRT");
// a skeleton replica, good enough for this test's purposes
final Replica replica = new Replica(name, propMap,"c1","s1");

inputs.add(replica);
final String coreUrl = replica.getCoreUrl();
if (coreUrl.matches(regex)) {
log.info("adding replica=[{}] to expected due to core url ({}) regex match on {} ",
replica, coreUrl, regex);
expectedTransformed.add(replica);
} else {
log.info("NOT expecting replica=[{}] due to core url ({}) regex mismatch ({})",
replica, coreUrl, regex);
}

}

final List<Replica> actualTransformed = new ArrayList<>(inputs);
transformer.transform(actualTransformed);
final List<Replica> actualTransformed = new ArrayList<>(inputs);
transformer.transform(actualTransformed);

assertEquals(expectedTransformed, actualTransformed);
if (transformer instanceof HttpShardHandlerFactory) {
((HttpShardHandlerFactory) transformer).close();
assertEquals(expectedTransformed, actualTransformed);
} finally {
if (transformer != null) {
transformer.close();
}
}
}

Expand Down

0 comments on commit c968122

Please sign in to comment.