-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* [feature] add back-pressure to simple buffer trigger.
- Loading branch information
1 parent
c9656ff
commit ba14a08
Showing
8 changed files
with
264 additions
and
14 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
19 changes: 19 additions & 0 deletions
19
src/main/java/com/github/phantomthief/collection/impl/BackPressureHandler.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,19 @@ | ||
package com.github.phantomthief.collection.impl; | ||
|
||
import java.util.concurrent.locks.Condition; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
/** | ||
* @author w.vela | ||
* Created on 2019-07-30. | ||
*/ | ||
class BackPressureHandler<T> implements RejectHandler<T> { | ||
|
||
@Override | ||
public boolean onReject(T element, @Nullable Condition condition) throws Throwable { | ||
assert condition != null; | ||
condition.awaitUninterruptibly(); | ||
return true; | ||
} | ||
} |
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
18 changes: 18 additions & 0 deletions
18
src/main/java/com/github/phantomthief/collection/impl/RejectHandler.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,18 @@ | ||
package com.github.phantomthief.collection.impl; | ||
|
||
import java.util.concurrent.locks.Condition; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
/** | ||
* @author w.vela | ||
* Created on 2019-07-30. | ||
*/ | ||
interface RejectHandler<T> { | ||
|
||
/** | ||
* execute on caller thread | ||
* @return {@code true} 继续执行, {@code false} 阻止执行 | ||
*/ | ||
boolean onReject(T element, @Nullable Condition condition) throws Throwable; | ||
} |
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
89 changes: 89 additions & 0 deletions
89
src/test/java/com/github/phantomthief/collection/impl/BackPressureTest.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,89 @@ | ||
package com.github.phantomthief.collection.impl; | ||
|
||
import static com.google.common.util.concurrent.MoreExecutors.shutdownAndAwaitTermination; | ||
import static com.google.common.util.concurrent.Uninterruptibles.sleepUninterruptibly; | ||
import static java.util.Collections.synchronizedList; | ||
import static java.util.concurrent.Executors.newFixedThreadPool; | ||
import static java.util.concurrent.TimeUnit.DAYS; | ||
import static java.util.concurrent.TimeUnit.SECONDS; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.ExecutorService; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.github.phantomthief.collection.BufferTrigger; | ||
|
||
/** | ||
* @author w.vela | ||
* Created on 2019-07-30. | ||
*/ | ||
class BackPressureTest { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(BackPressureTest.class); | ||
|
||
@Test | ||
void test() { | ||
List<String> consumed = new ArrayList<>(); | ||
BufferTrigger<String> buffer = BufferTrigger.<String, List<String>> simple() | ||
.enableBackPressure() | ||
.maxBufferCount(10) | ||
.interval(1, SECONDS) | ||
.setContainer(() -> synchronizedList(new ArrayList<>()), List::add) | ||
.consumer(it -> { | ||
logger.info("do consuming...{}", it); | ||
sleepUninterruptibly(1, SECONDS); | ||
consumed.addAll(it); | ||
logger.info("consumer done.{}", it); | ||
}) | ||
.build(); | ||
long cost = System.currentTimeMillis(); | ||
ExecutorService executor = newFixedThreadPool(10); | ||
for (int i = 0; i < 30; i++) { | ||
int j = i; | ||
executor.execute(() -> { | ||
buffer.enqueue("" + j); | ||
logger.info("enqueued:{}", j); | ||
}); | ||
} | ||
shutdownAndAwaitTermination(executor, 1, DAYS); | ||
buffer.manuallyDoTrigger(); | ||
assertEquals(30, consumed.size()); | ||
cost = System.currentTimeMillis() - cost; | ||
assertTrue(cost >= SECONDS.toMillis(3)); | ||
} | ||
|
||
@Test | ||
void testNoBlock() { | ||
List<String> consumed = new ArrayList<>(); | ||
BufferTrigger<String> buffer = BufferTrigger.<String, List<String>> simple() | ||
.maxBufferCount(10) | ||
.interval(1, SECONDS) | ||
.setContainer(() -> synchronizedList(new ArrayList<>()), List::add) | ||
.consumer(it -> { | ||
logger.info("do consuming...{}", it); | ||
sleepUninterruptibly(1, SECONDS); | ||
consumed.addAll(it); | ||
logger.info("consumer done.{}", it); | ||
}) | ||
.build(); | ||
long cost = System.currentTimeMillis(); | ||
ExecutorService executor = newFixedThreadPool(10); | ||
for (int i = 0; i < 30; i++) { | ||
int j = i; | ||
executor.execute(() -> { | ||
buffer.enqueue("" + j); | ||
logger.info("enqueued:{}", j); | ||
}); | ||
} | ||
shutdownAndAwaitTermination(executor, 1, DAYS); | ||
buffer.manuallyDoTrigger(); | ||
cost = System.currentTimeMillis() - cost; | ||
assertTrue(cost <= 1200); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/test/java/com/github/phantomthief/collection/impl/BuilderTest.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,44 @@ | ||
package com.github.phantomthief.collection.impl; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import com.github.phantomthief.collection.BufferTrigger; | ||
|
||
/** | ||
* @author w.vela | ||
* Created on 2019-07-30. | ||
*/ | ||
class BuilderTest { | ||
|
||
@Test | ||
void testBuilder() { | ||
assertThrows(NullPointerException.class, () -> | ||
BufferTrigger.simple() | ||
.build()); | ||
assertThrows(IllegalStateException.class, () -> | ||
BufferTrigger.simple() | ||
.consumer(it -> {}) | ||
.enableBackPressure() | ||
.disableSwitchLock() | ||
.build()); | ||
assertThrows(IllegalStateException.class, () -> | ||
BufferTrigger.simple() | ||
.consumer(it -> {}) | ||
.enableBackPressure() | ||
.rejectHandler(it -> {}) | ||
.build()); | ||
assertThrows(IllegalStateException.class, () -> | ||
BufferTrigger.simple() | ||
.consumer(it -> {}) | ||
.rejectHandler(it -> {}) | ||
.enableBackPressure() | ||
.build()); | ||
assertThrows(IllegalStateException.class, () -> | ||
BufferTrigger.simple() | ||
.consumer(it -> {}) | ||
.enableBackPressure() | ||
.build()); | ||
} | ||
} |