-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KTOR-4046 Check file descriptor before executing FD_CLR or FD_SET (#4137
- Loading branch information
Showing
3 changed files
with
94 additions
and
4 deletions.
There are no files selected for viewing
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
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
73 changes: 73 additions & 0 deletions
73
ktor-network/nix/test/io/ktor/network/sockets/tests/SelectNixTest.kt
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,73 @@ | ||
/* | ||
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package io.ktor.network.sockets.tests | ||
|
||
import io.ktor.network.selector.* | ||
import io.ktor.network.selector.EventInfo | ||
import io.ktor.network.selector.SelectorHelper | ||
import io.ktor.test.dispatcher.* | ||
import kotlinx.coroutines.* | ||
import kotlin.coroutines.* | ||
import kotlin.test.* | ||
|
||
class SelectNixTest { | ||
@Test | ||
fun selectDescriptorIsEqualOrLargerThanFdSetSize() = testSuspend { | ||
val scope = CoroutineScope( | ||
CoroutineExceptionHandler { _, cause -> | ||
val regexStr = """File descriptor \d+ is larger or equal to FD_SETSIZE \(3\)""" | ||
val message = cause.message | ||
assertNotNull(message) | ||
assertTrue( | ||
regexStr.toRegex().matches(message), | ||
"Expected message in format \"$regexStr\", got $message" | ||
) | ||
} | ||
) | ||
|
||
val selector = SelectorHelper(3) | ||
val job = selector.start(scope) | ||
|
||
selector.interest(EventInfo(1, SelectInterest.READ, Continuation(coroutineContext) {})) | ||
selector.interest(EventInfo(2, SelectInterest.READ, Continuation(coroutineContext) {})) | ||
selector.interest(EventInfo(3, SelectInterest.READ, Continuation(coroutineContext) {})) | ||
|
||
launch { | ||
delay(1000) | ||
if (scope.isActive) { | ||
selector.requestTermination() | ||
job.cancel() | ||
fail("Exception should have been thrown") | ||
} | ||
} | ||
|
||
job.join() | ||
} | ||
|
||
@Test | ||
fun selectDescriptorIsNegative() = testSuspend { | ||
val scope = CoroutineScope( | ||
CoroutineExceptionHandler { _, cause -> | ||
assertEquals("File descriptor -1 is negative", cause.message) | ||
} | ||
) | ||
|
||
val selector = SelectorHelper() | ||
val job = selector.start(scope) | ||
|
||
selector.interest(EventInfo(-1, SelectInterest.READ, Continuation(coroutineContext) {})) | ||
|
||
launch { | ||
delay(1000) | ||
if (scope.isActive) { | ||
selector.requestTermination() | ||
job.cancel() | ||
fail("Exception should have been thrown") | ||
} | ||
} | ||
|
||
job.join() | ||
} | ||
} |