Add support for looking up topics for matches if the topicPattern has wildcards in it

References #8
This commit is contained in:
R. Tyler Croy 2014-11-24 22:20:14 -08:00
parent 69ed3f3cad
commit 493deb7ddf
3 changed files with 81 additions and 3 deletions

View File

@ -23,7 +23,7 @@ class OfftopicClient {
}
public void createSubscribersFor(String topicsPattern) {
topicsPattern.split("\\+").each { topic ->
topicsFrom(topicsPattern).each { topic ->
if (topic.length() == 0) {
return
}
@ -57,4 +57,30 @@ class OfftopicClient {
subscriber.shutdown()
}
}
public ArrayList<String> topicsFrom(String topicsPattern) {
ArrayList<String> topics = new ArrayList<String>()
topicsPattern.split("\\+").each { topic ->
if (topic.length() == 0) {
return
}
if (topic.indexOf('*') >= 0) {
topics.addAll(lookupTopicsFor(topicsPattern))
}
else {
topics.add(topic)
}
}
return topics
}
private ArrayList<String> lookupTopicsFor(String topicPattern) {
ArrayList<String> topics = new ArrayList<String>()
KafkaService.fetchTopics().each { topic ->
if (topic =~ topicPattern) {
topics.add(topic)
}
}
return topics
}
}

View File

@ -18,7 +18,6 @@ class CuratorPool extends GenericObjectPool<CuratorClient>{
private CuratorPool(String zookeepers) {
super(new CuratorClientObjectFactory(zookeepers))
println "CREATING WITH ${zookeepers}"
}
public static void withCurator(Closure closure) {

View File

@ -1,6 +1,6 @@
package offtopic
import spock.lang.Specification
import spock.lang.*
class OfftopicClientSpec extends Specification {
def "initialization should create a clientId"() {
@ -43,3 +43,56 @@ class OfftopicClientCreateSubscribersSpec extends Specification {
this.client.subscribers.size() == 1
}
}
class OfftopicClientTopicParsingSpec extends Specification {
def client = null
def setup() {
this.client = new OfftopicClient()
}
def "a single topic"() {
when:
def topics = this.client.topicsFrom('topic')
then:
topics.size() == 1
}
def "plus-delimited topics"() {
when:
def topics = this.client.topicsFrom('topic+some.topic')
then:
topics.size() == 2
}
}
class OfftopicClientTopicLookupSpec extends Specification {
def client
def setup() {
// Mocking out KafkaService so we don't actually have to hit Zookeeper
GroovyMock(KafkaService, global: true)
this.client = new OfftopicClient()
}
def "looking up topics that don't exist"() {
when:
// Call fetchTopics() once and return an empty Array
1 * KafkaService.fetchTopics() >> []
def topics = this.client.lookupTopicsFor('spock.*')
then:
topics.size() == 0
}
def "looking up topics against topics that exist"() {
when:
// Call fetchTopics() once and return an empty Array
1 * KafkaService.fetchTopics() >> ['foo', 'bar', 'spock.rocks']
def topics = this.client.lookupTopicsFor('spock.*')
then:
topics.size() == 1
}
}