Skip to content

Commit

Permalink
Fixed failing tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Asgeir Nilsen committed Jun 19, 2018
1 parent 84d8a27 commit b61fc35
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package no.fint.provider.events.sse

import no.fint.event.model.Event
import no.fint.provider.events.ProviderProps
import no.fint.util.CurrentThreadExecutor
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter
import spock.lang.Specification

Expand All @@ -15,7 +16,7 @@ class SseServiceSpec extends Specification {
props = Mock(ProviderProps) {
getMaxNumberOfEmitters() >> 5
}
sseService = new SseService(providerProps: props)
sseService = new SseService(providerProps: props, executorService: new CurrentThreadExecutor())
}

def "Return SseEmitter when subscribing with new orgId"() {
Expand Down Expand Up @@ -51,7 +52,7 @@ class SseServiceSpec extends Specification {

def "Do not send event when orgId does not have registered emitters"() {
given:
sseService = new SseService(providerProps: props, clients: [:])
sseService = new SseService(providerProps: props, clients: [:], executorService: new CurrentThreadExecutor())

when:
sseService.send(new Event(orgId: 'hfk.no'))
Expand All @@ -66,7 +67,7 @@ class SseServiceSpec extends Specification {
def emitters = FintSseEmitters.with(5)
emitters.add(emitter)
def clients = ['hfk.no': emitters] as ConcurrentHashMap
sseService = new SseService(providerProps: props, clients: clients)
sseService = new SseService(providerProps: props, clients: clients, executorService: new CurrentThreadExecutor())

when:
sseService.send(new Event(orgId: 'hfk.no'))
Expand Down Expand Up @@ -95,7 +96,7 @@ class SseServiceSpec extends Specification {
def emitters = FintSseEmitters.with(5)
emitters.add(emitter)
def clients = ['hfk.no': emitters] as ConcurrentHashMap
sseService = new SseService(providerProps: props, clients: clients)
sseService = new SseService(providerProps: props, clients: clients, executorService: new CurrentThreadExecutor())

when:
sseService.shutdown()
Expand Down
84 changes: 84 additions & 0 deletions src/test/java/no/fint/util/CurrentThreadExecutor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package no.fint.util;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.*;
import java.util.stream.Collectors;

/**
* Executor which useful for unit testing
*/
public class CurrentThreadExecutor implements ExecutorService {

@Override
public void execute(Runnable command) {
command.run();
}

@Override
public void shutdown() {
}

@Override
public List<Runnable> shutdownNow() {
return Collections.emptyList();
}

@Override
public boolean isShutdown() {
return false;
}

@Override
public boolean isTerminated() {
return false;
}

@Override
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
return false;
}

@Override
public <T> Future<T> submit(Callable<T> task) {
FutureTask<T> f = new FutureTask<T>(task);
f.run();
return f;
}

@Override
public <T> Future<T> submit(Runnable task, T result) {
FutureTask<T> f = new FutureTask<T>(task, result);
f.run();
return f;
}

@Override
public Future<?> submit(Runnable task) {
FutureTask<?> f = new FutureTask<Void>(task, null);
f.run();
return f;
}

@Override
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException {
return tasks.stream().map(this::submit).collect(Collectors.toList());
}

@Override
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException {
return tasks.stream().map(this::submit).collect(Collectors.toList());
}

@Override
public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException {
return tasks.stream().map(this::submit).findFirst().get().get();
}

@Override
public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
return tasks.stream().map(this::submit).findFirst().get().get();
}

}

0 comments on commit b61fc35

Please sign in to comment.