-
Notifications
You must be signed in to change notification settings - Fork 6
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
Changes from 4 commits
e876930
f4eb729
81e9e10
fa5b7c9
54d8310
1186619
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 => | ||
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(())) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done ✅ |
||
} | ||
} | ||
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) | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
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 inIO
.Indeed, can we wrap this entire expression
IO
, and use a stack allocation forfildes
instead of a zone?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done ✅