-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make synchronous local connection a proper non test implementation
I am unreasonably satisfied that it just works like this
- Loading branch information
Showing
2 changed files
with
53 additions
and
44 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
Modules/Channels/shared/src/main/scala/channels/SynchronousLocalConnection.scala
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,47 @@ | ||
package channels | ||
|
||
import de.rmgk.delay.{Async, Callback, Promise, Sync} | ||
|
||
/** Allows establishing a single direct synchronous connection. */ | ||
class SynchronousLocalConnection[T] { | ||
object client extends LatentConnection[T] { | ||
|
||
object connection extends Connection[T] { | ||
def send(msg: T): Async[Any, Unit] = Async { | ||
val cb = server.incomingMessageCallback.async.bind | ||
cb.succeed(msg) | ||
} | ||
override def close(): Unit = () | ||
} | ||
|
||
def prepare(incomingHandler: Handler[T]): Async[Abort, Connection[T]] = Async { | ||
val callback = incomingHandler.getCallbackFor(connection) | ||
|
||
val serverConn = new Connection[T] { | ||
override def close(): Unit = () | ||
override def send(message: T): Async[Any, Unit] = Sync { | ||
callback.succeed(message) | ||
} | ||
} | ||
|
||
val established = server.connectionEstablished.async.bind | ||
established.succeed(serverConn) | ||
connection | ||
} | ||
} | ||
|
||
object server extends LatentConnection[T] { | ||
|
||
val incomingMessageCallback: Promise[Callback[T]] = Promise() | ||
|
||
val connectionEstablished: Promise[Callback[Connection[T]]] = Promise() | ||
|
||
def prepare(incomingHandler: Handler[T]): Async[Abort, Connection[T]] = Async.fromCallback[Connection[T]] { | ||
connectionEstablished.succeed(Async.handler) | ||
}.map { conn => | ||
incomingMessageCallback.succeed(incomingHandler.getCallbackFor(conn)) | ||
conn | ||
} | ||
} | ||
|
||
} |
50 changes: 6 additions & 44 deletions
50
Modules/Replication/shared/src/test/scala/replication/example/DeltaDisseminationTest.scala
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