forked from ReactiveX/RxJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue ReactiveX#42: Added a new EventConsumerRegistry which can be us…
…ed to create and manage instances of EventConsumers.
- Loading branch information
Robert Winkler
committed
Jan 11, 2017
1 parent
1170867
commit dc756b4
Showing
6 changed files
with
191 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/main/java/io/github/robwin/consumer/DefaultEventConsumerRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright 2017 Robert Winkler | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.github.robwin.consumer; | ||
|
||
import javaslang.collection.Array; | ||
import javaslang.collection.Seq; | ||
|
||
|
||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ConcurrentMap; | ||
|
||
public class DefaultEventConsumerRegistry<T> implements EventConsumerRegistry<T>{ | ||
|
||
/** | ||
* The CircularEventConsumers, indexed by name of the backend. | ||
*/ | ||
private final ConcurrentMap<String, EventConsumer<T>> registry; | ||
|
||
/** | ||
* The constructor with default circuitBreaker properties. | ||
*/ | ||
public DefaultEventConsumerRegistry() { | ||
this.registry = new ConcurrentHashMap<>(); | ||
} | ||
|
||
@Override | ||
public EventConsumer<T> createEventConsumer(String id, int bufferSize) { | ||
EventConsumer<T> eventConsumer = new CircularEventConsumer<>(bufferSize); | ||
registry.put(id, eventConsumer); | ||
return eventConsumer; | ||
} | ||
|
||
@Override | ||
public EventConsumer<T> getEventConsumer(String id){ | ||
return registry.get(id); | ||
} | ||
|
||
@Override | ||
public Seq<EventConsumer<T>> getAllEventConsumer(){ | ||
return Array.ofAll(registry.values()); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/io/github/robwin/consumer/EventConsumer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* | ||
* Copyright 2016 Robert Winkler | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* | ||
*/ | ||
package io.github.robwin.consumer; | ||
|
||
import io.reactivex.functions.Consumer; | ||
import javaslang.collection.List; | ||
|
||
public interface EventConsumer<T> extends Consumer<T> { | ||
|
||
/** | ||
* Returns a list containing all of the buffered events. | ||
* | ||
* @return a list containing all of the buffered events. | ||
*/ | ||
List<T> getBufferedEvents(); | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/io/github/robwin/consumer/EventConsumerRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright 2017 Robert Winkler | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.github.robwin.consumer; | ||
|
||
import javaslang.collection.Seq; | ||
|
||
|
||
public interface EventConsumerRegistry<T> { | ||
|
||
/** | ||
* Creates a new EventConsumer and stores the instance in the registry. | ||
* | ||
* @param id the id of the EventConsumer | ||
* @param bufferSize the size of the EventConsumer | ||
* @return a new EventConsumer | ||
*/ | ||
EventConsumer<T> createEventConsumer(String id, int bufferSize); | ||
|
||
/** | ||
* Returns the EventConsumer to which the specified id is mapped. | ||
* | ||
* @param id the id of the EventConsumer | ||
* @return the EventConsumer to which the specified id is mapped | ||
*/ | ||
EventConsumer<T> getEventConsumer(String id); | ||
|
||
/** | ||
* Returns all EventConsumer instances. | ||
* | ||
* @return all EventConsumer instances | ||
*/ | ||
Seq<EventConsumer<T>> getAllEventConsumer(); | ||
} |
54 changes: 54 additions & 0 deletions
54
src/test/java/io/github/robwin/consumer/EventConsumerRegistryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* | ||
* Copyright 2016 Robert Winkler | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* | ||
*/ | ||
package io.github.robwin.consumer; | ||
|
||
import io.github.robwin.circuitbreaker.event.CircuitBreakerEvent; | ||
import org.junit.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class EventConsumerRegistryTest { | ||
|
||
@Test | ||
public void shouldCreateAnEventConsumer() { | ||
EventConsumerRegistry<CircuitBreakerEvent> registry = new DefaultEventConsumerRegistry<>(); | ||
EventConsumer<CircuitBreakerEvent> eventEventConsumer = registry.createEventConsumer("testName", 5); | ||
|
||
assertThat(eventEventConsumer).isNotNull(); | ||
assertThat(eventEventConsumer.getBufferedEvents()).hasSize(0); | ||
} | ||
|
||
@Test | ||
public void shouldReturnTheSameEventConsumer() { | ||
EventConsumerRegistry<CircuitBreakerEvent> registry = new DefaultEventConsumerRegistry<>(); | ||
EventConsumer<CircuitBreakerEvent> eventEventConsumer1 = registry.createEventConsumer("testName", 5); | ||
EventConsumer<CircuitBreakerEvent> eventEventConsumer2 = registry.getEventConsumer("testName"); | ||
|
||
assertThat(eventEventConsumer1).isEqualTo(eventEventConsumer2); | ||
} | ||
|
||
@Test | ||
public void shouldReturnAllEventConsumer() { | ||
EventConsumerRegistry<CircuitBreakerEvent> registry = new DefaultEventConsumerRegistry<>(); | ||
registry.createEventConsumer("testName1", 5); | ||
registry.createEventConsumer("testName2", 2); | ||
|
||
assertThat(registry.getAllEventConsumer()).hasSize(2); | ||
} | ||
} |