Skip to content

Commit

Permalink
Allow subscription builder to be called with the same route (#805)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
johnewart authored and artursouza committed Nov 15, 2022
1 parent d425377 commit 576d89a
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@ class DaprSubscriptionBuilder {
*/
DaprSubscriptionBuilder setDefaultPath(String path) {
if (defaultPath != null) {
throw new RuntimeException(
String.format(
"a default route is already set for topic %s on pubsub %s",
this.topic, this.pubsubName));
if (!defaultPath.equals(path)) {
throw new RuntimeException(
String.format(
"a default route is already set for topic %s on pubsub %s (current: '%s', supplied: '%s')",
this.topic, this.pubsubName, this.defaultPath, path));
}
}
defaultPath = path;
return this;
Expand Down
Original file line number Diff line number Diff line change
@@ -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);

}

}

0 comments on commit 576d89a

Please sign in to comment.