Skip to content

Instantly share code, notes, and snippets.

@Fuud
Created August 25, 2017 06:11
Show Gist options
  • Save Fuud/e3d3d636a9fb50c13d0818bce9d1f2d5 to your computer and use it in GitHub Desktop.
Save Fuud/e3d3d636a9fb50c13d0818bce9d1f2d5 to your computer and use it in GitHub Desktop.
Test kotlin coroutines
package test;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CompletableFuture;
class SubscriptionFinderRegistry {
static <T extends Event> SubscriptionFinder<T> getSubscriptionFinder(T event) {
return new SubscriptionFinder();
}
}
class SubscriptionFinder<T extends Event> {
CompletableFuture<List<Subscription>> findSubscriptions(T event) {
return CompletableFuture.supplyAsync(() -> {
System.out.println("Executing find subscriptions in thread " + Thread.currentThread().getName());
return Arrays.asList(
new Subscription("sub1"),
new Subscription("sub2"));
}
);
}
}
class NotificationBuilderRegistry {
static <T extends Event> NotificationBuilder<T> getNotificationBuilder(T event) {
return new NotificationBuilder<>();
}
}
class NotificationBuilder<T extends Event> {
Notification buildNotification(T event, Subscription subscription) {
return new Notification();
}
}
class NotificationSenderRegistry {
static NotificationSender getNotificationSender(TransportType transportType) {
return new NotificationSender();
}
}
class NotificationSender {
CompletableFuture<Boolean> send(Notification notification) {
return CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(new Random().nextInt(5000));
} catch (Exception ignore) {
}
System.out.println("Executing send notification " + notification + " in thread " + Thread.currentThread().getName());
return true;
});
}
}
interface Event {
}
class PresenceEvent implements Event {
}
class Subscription {
String name;
TransportType transportType = TransportType.PubNub;
public Subscription(String name) {
this.name = name;
}
}
enum TransportType {
PubNub,
WebHook
}
class Notification {
}
Executing find subscriptions in thread ForkJoinPool.commonPool-worker-2
await future java.util.concurrent.CompletableFuture@172ea887[Not completed] in thread Thread[ForkJoinPool.commonPool-worker-1,5,main]
Executing send notification test.Notification@255f8485 in thread ForkJoinPool.commonPool-worker-2
future java.util.concurrent.CompletableFuture@172ea887[Completed normally] done in thread Thread[ForkJoinPool.commonPool-worker-2,5,main]
await future java.util.concurrent.CompletableFuture@8d5011b[Not completed] in thread Thread[ForkJoinPool.commonPool-worker-2,5,main]
Executing send notification test.Notification@d0f76e8 in thread ForkJoinPool.commonPool-worker-3
future java.util.concurrent.CompletableFuture@8d5011b[Completed normally] done in thread Thread[ForkJoinPool.commonPool-worker-3,5,main]
done with result: true
@file:Suppress("EXPERIMENTAL_FEATURE_WARNING")
package test
import kotlinx.coroutines.experimental.CommonPool
import kotlinx.coroutines.experimental.future.await
import kotlinx.coroutines.experimental.future.future
import java.util.concurrent.CompletableFuture
fun main(args: Array<String>) {
val event: Event = PresenceEvent()
future(CommonPool) {
val subscriptionFinder = SubscriptionFinderRegistry.getSubscriptionFinder(event)
val subscriptions: MutableList<Subscription> = subscriptionFinder.findSubscriptions(event).await()
val notificationBuilder: NotificationBuilder<Event> = NotificationBuilderRegistry.getNotificationBuilder(event)
val sentNotifications: List<CompletableFuture<Boolean>> = subscriptions
.map { subscription ->
val notification: Notification = notificationBuilder.buildNotification(event, subscription)
val notificationSender: NotificationSender = NotificationSenderRegistry.getNotificationSender(subscription.transportType)
notificationSender.send(notification)
}
var result: Boolean = true;
sentNotifications.forEach { futureResult ->
println("await future " + futureResult + " in thread " + Thread.currentThread())
result = futureResult.await() && result
println("future " + futureResult + " done in thread " + Thread.currentThread())
}
result
}.whenComplete { result, exception -> print("done with result: " + result) }
Thread.currentThread().join()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment