Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test with monitor #79

Merged
merged 6 commits into from
Oct 29, 2022
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions tests/native/src/test/scala/epollcat/MonitorSuite.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright 2022 Arman Bilge
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package epollcat

import cats.effect.IO
import cats.effect.Resource
import epollcat.unsafe.EventNotificationCallback
import epollcat.unsafe.EventPollingExecutorScheduler
import epollcat.unsafe.EpollRuntime

import scala.scalanative.unsafe._
import scala.scalanative.unsigned._
import scala.scalanative.posix.unistd._

class MonitorSuite extends EpollcatSuite {

class Pipe private (val readFd: Int, val writeFd: Int)
object Pipe {
private val zoneResource: Resource[IO, Zone] =
Resource.make(IO(Zone.open()))(z => IO(z.close()))
val make: Resource[IO, Pipe] = Resource.make {
zoneResource.use { implicit zone =>
val fildes = alloc[CInt](2)
if (pipe(fildes) != 0) {
IO.raiseError(new Exception("Failed to create pipe"))
} else {
IO(new Pipe(fildes(0), fildes(1)))
}
}
}(pipe =>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that calling pipe(fildes) is a side-effect and should be wrapped in IO.

Indeed, can we wrap this entire expression IO, and use a stack allocation for fildes instead of a zone?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, can we wrap this entire expression IO, and use a stack allocation for fildes instead of a zone?

Done ✅

IO {
close(pipe.readFd)
close(pipe.writeFd)
()
})
}

test("monitor a pipe") {
val scheduler = EpollRuntime.global.scheduler.asInstanceOf[EventPollingExecutorScheduler]

Pipe.make.use { pipe =>
IO.async_[Unit] { cb =>
val byte = 10.toByte
var stop: Runnable = null
val monitorCallback = new EventNotificationCallback {
def notifyEvents(readReady: Boolean, writeReady: Boolean): Unit = {
val readBuf = stackalloc[Byte]()
val bytesRead = read(pipe.readFd, readBuf, 1L.toULong)
assertEquals(bytesRead, 1)
assertEquals(readBuf(0), byte)
stop.run()
cb(Right(()))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, sorry, something else I just realized: if there is an assertion failure here, does it actually fail the test? Or maybe we need to capture it and return it as a cb(Left(ex))?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right! It hands if the assert fails instead of failing the test. On it!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done ✅
Now exceptions fail the test and also stops the monitoring.

}
}
stop = scheduler.monitor(pipe.readFd, reads = true, writes = false)(monitorCallback)
val writeBuf = stackalloc[Byte]()
writeBuf(0) = byte
val wroteBytes = write(pipe.writeFd, writeBuf, 1L.toULong)
assertEquals(wroteBytes, 1)
}
}
}
}