https://kotlinlang.org logo
#ktor
Title
# ktor
r

Rohde Fischer

08/07/2022, 2:56 PM
And another curious question about KTor sockets, apart from the apparently non-existing timeout, how am I supposed to test sockets in a reasonable way? Seems the only way I can find is to wrap the static methods in interfaces so, I can inject test-implementations - but I assume that KTor sockets isn't designed in the non-testable fashions that 10+ years of programming experience have shown us to be bad design?
a

Aleksei Tirman [JB]

08/08/2022, 11:46 AM
Could you please describe your test scenario?
r

Rohde Fischer

08/08/2022, 4:38 PM
In this case, I basically want to ensure that the socket attempts to connect to a certain IP and port in this case, so I'd like to do something along the lines of (in semi pseudo code):
Copy code
val testKtor = TODO()
val myService = Service(testKtor)

val host = Host("127.0.0.1", 22)
myService.perform(host)

testKtor.connections.shouldHaveSize(1)
val connection = testKtor.connections[0]
connection.type = ConnectionTypes.TCP
connection.host.shouldBe("127.0.0.1")
connection.post.shouldBe(22)
(whether it be exactly like that or not doesn't matter, it's more that I can test that it respects the parameters - preferrably in a way that's independent on if I call
.connect(host, port)
,
.connect(SocketAddress(...))
or something else)
a

Aleksei Tirman [JB]

08/09/2022, 10:30 AM
Unfortunately, there are no such inspection capabilities in Ktor so you have to write a wrapper around Ktor’s API.
4 Views