Skip to content

Commit

Permalink
fix: use port 0 for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lchenut committed Oct 4, 2024
1 parent f41f255 commit ae7d377
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
16 changes: 8 additions & 8 deletions tests/testsctp.nim
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ suite "SCTP":
dtls: Dtls
sctp: Sctp

proc initSctpStack(localAddress: TransportAddress): SctpStackForTest =
result.localAddress = localAddress
result.udp = UdpTransport.new(result.localAddress)
proc initSctpStack(la: TransportAddress): SctpStackForTest =
result.udp = UdpTransport.new(la)
result.localAddress = result.udp.localAddress()
result.stun = Stun.new(result.udp)
result.dtls = Dtls.new(result.stun)
result.sctp = Sctp.new(result.dtls)
Expand All @@ -45,8 +45,8 @@ suite "SCTP":

asyncTest "Two SCTP nodes connecting to each other, then sending/receiving data":
var
sctpServer = initSctpStack(initTAddress("127.0.0.1:4444"))
sctpClient = initSctpStack(initTAddress("127.0.0.1:5555"))
sctpServer = initSctpStack(initTAddress("127.0.0.1:0"))
sctpClient = initSctpStack(initTAddress("127.0.0.1:0"))
let
serverConnFut = sctpServer.sctp.accept()
clientConn = await sctpClient.sctp.connect(sctpServer.localAddress)
Expand All @@ -68,9 +68,9 @@ suite "SCTP":

asyncTest "Two DTLS nodes connecting to the same DTLS server, sending/receiving data":
var
sctpServer = initSctpStack(initTAddress("127.0.0.1:4444"))
sctpClient1 = initSctpStack(initTAddress("127.0.0.1:5555"))
sctpClient2 = initSctpStack(initTAddress("127.0.0.1:6666"))
sctpServer = initSctpStack(initTAddress("127.0.0.1:0"))
sctpClient1 = initSctpStack(initTAddress("127.0.0.1:0"))
sctpClient2 = initSctpStack(initTAddress("127.0.0.1:0"))
let
serverConn1Fut = sctpServer.sctp.accept()
serverConn2Fut = sctpServer.sctp.accept()
Expand Down
2 changes: 1 addition & 1 deletion webrtc/stun/stun_connection.nim
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ proc new*(
##
var self = T(
udp: udp,
laddr: udp.laddr,
laddr: udp.localAddress(),
raddr: raddr,
closed: false,
dataRecv: newAsyncQueue[seq[byte]](StunMaxQueuingMessages),
Expand Down
2 changes: 1 addition & 1 deletion webrtc/stun/stun_transport.nim
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ proc new*(
##
var self = T(
udp: udp,
laddr: udp.laddr,
laddr: udp.localAddress(),
usernameProvider: usernameProvider,
usernameChecker: usernameChecker,
passwordProvider: passwordProvider,
Expand Down
8 changes: 6 additions & 2 deletions webrtc/udp_transport.nim
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type
raddr: TransportAddress

UdpTransport* = ref object
laddr*: TransportAddress
laddr: TransportAddress
udp: DatagramTransport
dataRecv: AsyncQueue[UdpPacketInfo]
closed: bool
Expand All @@ -33,7 +33,7 @@ const UdpTransportTrackerName* = "webrtc.udp.transport"
proc new*(T: type UdpTransport, laddr: TransportAddress): T =
## Initialize an Udp Transport
##
var self = T(laddr: laddr, closed: false)
var self = T(closed: false)

proc onReceive(
udp: DatagramTransport,
Expand All @@ -49,6 +49,7 @@ proc new*(T: type UdpTransport, laddr: TransportAddress): T =

self.dataRecv = newAsyncQueue[UdpPacketInfo]()
self.udp = newDatagramTransport(onReceive, local = laddr)
self.laddr = self.udp.localAddress()
trackCounter(UdpTransportTrackerName)
return self

Expand Down Expand Up @@ -87,3 +88,6 @@ proc read*(self: UdpTransport): Future[UdpPacketInfo] {.async: (raises: [Cancell
return
trace "UDP read"
return await self.dataRecv.popFirst()

proc localAddress*(self: UdpTransport): TransportAddress =
self.laddr

0 comments on commit ae7d377

Please sign in to comment.