Skip to content

Commit

Permalink
Updating tests
Browse files Browse the repository at this point in the history
  • Loading branch information
LikeTheSalad committed Feb 13, 2025
1 parent 902fd94 commit 050577a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,13 @@ class UdpClientTest {

companion object {
private const val SERVER_HOST = "localhost"
private const val SERVER_PORT = 4447
}

@BeforeEach
fun setUp() {
server = TestUdpServer(SERVER_PORT)
server = TestUdpServer()
server.start()
client = UdpClient(SERVER_HOST, SERVER_PORT, 256)
client = UdpClient(SERVER_HOST, server.getPort(), 256)
}

@AfterEach
Expand Down Expand Up @@ -99,7 +98,7 @@ class UdpClientTest {

@Test
fun `Server port is not reachable`() {
client = UdpClient(SERVER_HOST, SERVER_PORT + 1, 256)
client = UdpClient(SERVER_HOST, server.getPort() + 1, 256)

assertThrows<SocketTimeoutException> {
client.send("Example".toByteArray(), Duration.ofSeconds(1))
Expand All @@ -117,7 +116,7 @@ class UdpClientTest {

@Test
fun `Server host not found`() {
client = UdpClient("nonexistent", SERVER_PORT, 256)
client = UdpClient("nonexistent", 1, 256)
assertThrows<UnknownHostException> {
client.send("Example".toByteArray())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import java.net.DatagramPacket
import java.net.DatagramSocket
import java.net.SocketException

class TestUdpServer(port: Int) : Thread() {
class TestUdpServer : Thread() {
private val buf = ByteArray(256)
val socket = DatagramSocket(port)
val socket = DatagramSocket()

@Volatile
var responseHandler: (DatagramPacket) -> Unit = { clientPacket ->
Expand Down Expand Up @@ -53,4 +53,8 @@ class TestUdpServer(port: Int) : Thread() {
fun close() {
socket.close()
}

fun getPort(): Int {
return socket.localPort
}
}

0 comments on commit 050577a

Please sign in to comment.