Skip to content

Commit

Permalink
edit
Browse files Browse the repository at this point in the history
  • Loading branch information
usfalami committed Jul 15, 2024
1 parent 31a36ca commit cb31320
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/usf/inspect/core/SessionLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/
public final class SessionLogger implements SessionHandler<Session> {

@Override
@Override //sync. avoid session log collision
public synchronized void handle(Session s) {
log.debug("+ {}", s);
for(var req : s.getRestRequests()) {
Expand Down
40 changes: 23 additions & 17 deletions src/test/java/org/usf/inspect/core/SessionPublisherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import static java.util.concurrent.CompletableFuture.allOf;
import static java.util.concurrent.CompletableFuture.runAsync;
import static java.util.concurrent.Executors.newFixedThreadPool;
import static java.util.stream.IntStream.range;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static java.util.stream.Stream.generate;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.usf.inspect.core.SessionPublisher.emit;
import static org.usf.inspect.core.SessionPublisher.handlers;
Expand Down Expand Up @@ -33,26 +32,33 @@ void clearHandlers() {
@ParameterizedTest
@ValueSource(ints = {1, 5, 10, 20, 50, 100})
void testRegister(int n) {
var service = newFixedThreadPool(n);
var futures = range(0, n)
.mapToObj(i-> runAsync(()-> register(s-> {}), service))
.toArray(CompletableFuture[]::new);
service.shutdown();
assertDoesNotThrow(()-> allOf(futures).get());
nParallelExec(n, ()-> register(s-> {}));
assertEquals(n, handlers.size());
}

@ParameterizedTest
@ValueSource(ints = {1, 5, 10, 20, 50, 100})
void testEmit(int n) {
var reg = new AtomicInteger();
register(s-> reg.incrementAndGet());
var service = newFixedThreadPool(n);
var futures = range(0, n)
.mapToObj(i-> runAsync(()-> emit(new RestSession()), service))
.toArray(CompletableFuture[]::new);
service.shutdown();
assertDoesNotThrow(()-> allOf(futures).get());
assertEquals(n, reg.get());
var reg = generate(AtomicInteger::new).limit(10).toArray(AtomicInteger[]::new);
for(var o : reg) {
register(s-> o.incrementAndGet());
}
nParallelExec(n, ()-> emit(new RestSession()));
for(var o : reg) {
assertEquals(n, o.get());
}
}

static void nParallelExec(int n, Runnable r) {
var pool = newFixedThreadPool(n);
try {
allOf(generate(()-> runAsync(r, pool))
.limit(n)
.toArray(CompletableFuture[]::new))
.join();
}
finally {
pool.shutdown();
}
}
}

0 comments on commit cb31320

Please sign in to comment.