Skip to content

Commit

Permalink
Add test for async.
Browse files Browse the repository at this point in the history
  • Loading branch information
quelgar committed Jan 15, 2024
1 parent 21e8101 commit 1ebb18f
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 36 deletions.
51 changes: 51 additions & 0 deletions src/test/scala/scalauv/AsyncSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package scalauv

import org.junit.Test
import org.junit.Assert.*
import scalanative.unsafe.*
import scalanative.unsigned.*

final class AsyncSpec {

import AsyncSpec.*

@Test
def runsAnAsyncCallback(): Unit = {
withZone {
val loop = LibUv.uv_default_loop()

val asyncHandle = AsyncHandle.zoneAllocate()

UvUtils.attempt {
LibUv.uv_async_init(loop, asyncHandle, callback).checkErrorThrowIO()

UvUtils.onFail(LibUv.uv_close(asyncHandle, closeCallback))

LibUv.uv_async_send(asyncHandle).checkErrorThrowIO()
}

LibUv.uv_run(loop, RunMode.DEFAULT).checkErrorThrowIO()
()
}

assertTrue("callback was run", callbackRun)
assertTrue("handle was closed", closed)
}

}

object AsyncSpec {

private var callbackRun = false

private var closed = false

private val closeCallback: LibUv.CloseCallback = { (handle: Handle) =>
closed = true
}

private val callback: LibUv.AsyncCallback = { (handle: AsyncHandle) =>
callbackRun = true
LibUv.uv_close(handle, closeCallback)
}
}
67 changes: 31 additions & 36 deletions src/test/scala/scalauv/TcpSpec.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package scalauv

import org.junit.Test
import org.junit.Assert.*
import LibUv.*
Expand All @@ -10,42 +11,6 @@ final class TcpSpec {

import TcpSpec.*

def recordReceived(s: String): Unit = {
receivedData = receivedData :+ s
}

def setFailed(msg: String): Unit = {
failed = Some(msg)
}

def onClose: CloseCallback = (h: Handle) => stdlib.free(h.toPtr)

def onRead: StreamReadCallback = {
(handle: StreamHandle, numRead: CSSize, buf: Buffer) =>
numRead match {
case ErrorCodes.EOF =>
uv_close(handle, onClose)
case code if code < 0 =>
uv_close(handle, onClose)
setFailed(UvUtils.errorMessage(code.toInt))
case _ =>
val (text, done) =
buf.asUtf8String(numRead.toInt).span(_ != DoneMarker)
recordReceived(text)
if done.nonEmpty then {
val listenHandle = Handle.unsafeFromPtr(uv_handle_get_data(handle))
uv_close(listenHandle, null)
}
}
stdlib.free(buf.base)
}

@Test
def foo(): Unit = {
println("XXXXXX")
assertEquals(1, 1)
}

@Test
def listen(): Unit = {

Expand Down Expand Up @@ -152,4 +117,34 @@ object TcpSpec {
private var receivedData = Vector.empty[String]
private var failed = Option.empty[String]

def recordReceived(s: String): Unit = {
receivedData = receivedData :+ s
}

def setFailed(msg: String): Unit = {
failed = Some(msg)
}

def onClose: CloseCallback = (h: Handle) => stdlib.free(h.toPtr)

def onRead: StreamReadCallback = {
(handle: StreamHandle, numRead: CSSize, buf: Buffer) =>
numRead match {
case ErrorCodes.EOF =>
uv_close(handle, onClose)
case code if code < 0 =>
uv_close(handle, onClose)
setFailed(UvUtils.errorMessage(code.toInt))
case _ =>
val (text, done) =
buf.asUtf8String(numRead.toInt).span(_ != DoneMarker)
recordReceived(text)
if done.nonEmpty then {
val listenHandle = Handle.unsafeFromPtr(uv_handle_get_data(handle))
uv_close(listenHandle, null)
}
}
stdlib.free(buf.base)
}

}

0 comments on commit 1ebb18f

Please sign in to comment.