Allow subscription builder to be called with the same route (#805)

If the route being provided for a topic is the same as it was previously,
do not raise an exception.

Fixes parallel test execution without `forkMode=always` (#766)

Signed-off-by: John Ewart <johnewart@microsoft.com>

Signed-off-by: John Ewart <johnewart@microsoft.com>
This commit is contained in:
John Ewart 2022-11-07 11:00:23 -08:00 committed by GitHub
parent 5dd45adcf2
commit aeb42d833f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 87 additions and 4 deletions

View File

@ -47,10 +47,12 @@ class DaprSubscriptionBuilder {
*/ */
DaprSubscriptionBuilder setDefaultPath(String path) { DaprSubscriptionBuilder setDefaultPath(String path) {
if (defaultPath != null) { if (defaultPath != null) {
if (!defaultPath.equals(path)) {
throw new RuntimeException( throw new RuntimeException(
String.format( String.format(
"a default route is already set for topic %s on pubsub %s", "a default route is already set for topic %s on pubsub %s (current: '%s', supplied: '%s')",
this.topic, this.pubsubName)); this.topic, this.pubsubName, this.defaultPath, path));
}
} }
defaultPath = path; defaultPath = path;
return this; return this;

View File

@ -0,0 +1,81 @@
package io.dapr.springboot;
import io.dapr.Rule;
import org.junit.Assert;
import org.junit.Test;
import java.lang.annotation.Annotation;
import java.util.HashMap;
public class DaprRuntimeTest {
@Test
public void testPubsubDefaultPathDuplicateRegistration() {
String pubSubName = "pubsub";
String topicName = "topic";
String match = "";
String route = String.format("%s/%s", pubSubName, topicName);
HashMap<String, String> metadata = new HashMap<String, String>();
Rule rule = new Rule(){
@Override
public Class<? extends Annotation> annotationType() {
return Rule.class;
}
public String match() {
return match;
}
public int priority() {
return 0;
}
};
DaprRuntime runtime = DaprRuntime.getInstance();
Assert.assertNotNull(runtime);
// We should be able to register the same route multiple times
runtime.addSubscribedTopic(
pubSubName, topicName, match, rule.priority(), route, metadata);
runtime.addSubscribedTopic(
pubSubName, topicName, match, rule.priority(), route, metadata);
}
@Test(expected = RuntimeException.class)
public void testPubsubDefaultPathDifferentRegistration() {
String pubSubName = "pubsub";
String topicName = "topic";
String match = "";
String firstRoute = String.format("%s/%s", pubSubName, topicName);
String secondRoute = String.format("%s/%s/subscribe", pubSubName, topicName);
HashMap<String, String> metadata = new HashMap<String, String>();
Rule rule = new Rule(){
@Override
public Class<? extends Annotation> annotationType() {
return Rule.class;
}
public String match() {
return match;
}
public int priority() {
return 0;
}
};
DaprRuntime runtime = DaprRuntime.getInstance();
Assert.assertNotNull(runtime);
runtime.addSubscribedTopic(
pubSubName, topicName, match, rule.priority(), firstRoute, metadata);
// Supplying the same pubsub bits but a different route should fail
runtime.addSubscribedTopic(
pubSubName, topicName, match, rule.priority(), secondRoute, metadata);
}
}