Skip to content

Commit

Permalink
Try to fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
soywiz committed Jun 24, 2024
1 parent e667b74 commit 9767780
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
11 changes: 11 additions & 0 deletions korge-ipc/src/main/kotlin/korlibs/korge/ipc/KorgeIPC.kt
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class KorgeIPC(val path: String = KorgeIPCInfo.DEFAULT_PATH, val isServer: Boole
try {
val socket = KorgeIPCSocket.openOrListen(socketPath, listener, server = isServer, serverDelete = true, serverDeleteOnExit = true)
try {
println("CONNECTED: isServer=$isServer!")
while (socket.isOpen) {
delay(100L)
}
Expand All @@ -97,8 +98,18 @@ class KorgeIPC(val path: String = KorgeIPCInfo.DEFAULT_PATH, val isServer: Boole

//val socket = KorgeIPCSocket.openOrListen(socketPath, , serverDeleteOnExit = true)

fun waitConnected() {
var n = 0
while (connectedSockets.size == 0) {
Thread.sleep(100L)
n++
if (n >= 20) error("Too long waiting for connected")
}
}

val availableEvents get() = synchronized(_events) { _events.size }
fun writeEvent(e: IPCPacket) {
println("writeEvent: $e")
synchronized(connectedSockets) {
for (socket in connectedSockets) {
socket.writePacket(e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ class KorgeIPCServerSocketTest {

@Test
fun testIPC(): Unit {
val address = "/tmp/demo1"
val address = "$TMP/demo1.sock"
val ipc1 = KorgeIPC(address, isServer = true)
val ipc2 = KorgeIPC(address, isServer = false)
ipc1.onEvent = { socket, e -> println("EVENT1: $socket, $e") }
ipc2.onEvent = { socket, e -> println("EVENT2: $socket, $e") }
ipc1.waitConnected()
ipc1.writeEvent(IPCPacket(777))
assertEquals(777, ipc2.readEvent().type)
ipc2.writeEvent(IPCPacket(888))
Expand Down Expand Up @@ -65,10 +66,10 @@ class KorgeIPCServerSocketTest {

assertEquals("""
onConnect[CLI->SER][KorgeIPCSocket(0)]
onEvent[CLI->SER][KorgeIPCSocket(0)]: Packet(type=1)
onEvent[CLI->SER][KorgeIPCSocket(0)]: Packet(type=0x1, data=bytes[5])
onClose[CLI->SER][KorgeIPCSocket(0)]
onConnect[SER->CLI][KorgeIPCSocket(-1)]
onEvent[SER->CLI][KorgeIPCSocket(-1)]: Packet(type=2)
onEvent[SER->CLI][KorgeIPCSocket(-1)]: Packet(type=0x2, data=bytes[3])
onClose[SER->CLI][KorgeIPCSocket(-1)]
""".trimIndent(), logS.joinToString("\n") + "\n" + logC.joinToString("\n"))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ class KorgePacketRingBufferTest {
ring.write(IPCPacket(2, byteArrayOf(1, 2, 3)))
assertEquals(22, ring.availableRead)
assertEquals(1002, ring.availableWrite)
assertEquals("Packet(type=1, data=bytes[3])", ring.read().toString())
assertEquals("Packet(type=0x1, data=bytes[3])", ring.read().toString())
assertEquals(11, ring.availableRead)
assertEquals(1013, ring.availableWrite)
assertEquals("Packet(type=2, data=bytes[3])", ring.read().toString())
assertEquals("Packet(type=0x2, data=bytes[3])", ring.read().toString())
assertEquals(0, ring.availableRead)
assertEquals(1024, ring.availableWrite)
assertEquals("null", ring.read().toString())
Expand Down

0 comments on commit 9767780

Please sign in to comment.