Skip to content

Commit

Permalink
update more test
Browse files Browse the repository at this point in the history
  • Loading branch information
MacOMNI committed Dec 11, 2024
1 parent e0e9663 commit 0320019
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Networking/Sources/MsQuicSwift/QuicConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public final class QuicConnection: Sendable {
}
}

fileprivate func close() {
func close() {
storage.write { storage in
storage = nil
}
Expand Down
81 changes: 81 additions & 0 deletions Networking/Tests/MsQuicSwiftTests/QuicListenerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -304,4 +304,85 @@ struct QuicListenerTests {
#expect(receivedData2[0] == Data("other test data 2".utf8))
#expect(receivedData2[1] == Data("another replay to 2".utf8))
}

@Test
func mockConnectionShutdown() async throws {
let serverHandler = MockQuicEventHandler()
let clientHandler = MockQuicEventHandler()

// create listener

let quicSettings = QuicSettings.defaultSettings
let serverConfiguration = try QuicConfiguration(
registration: registration,
pkcs12: pkcs12Data,
alpns: [Data("testalpn".utf8)],
client: false,
settings: quicSettings
)

let listener = try QuicListener(
handler: serverHandler,
registration: registration,
configuration: serverConfiguration,
listenAddress: NetAddr(ipAddress: "127.0.0.1", port: 0)!,
alpns: [Data("testalpn".utf8)]
)

let listenAddress = try listener.listenAddress()
let (ipAddress, port) = listenAddress.getAddressAndPort()
#expect(ipAddress == "127.0.0.1")
#expect(port != 0)

// create connection to listener

let clientConfiguration = try QuicConfiguration(
registration: registration,
pkcs12: pkcs12Data,
alpns: [Data("testalpn".utf8)],
client: true,
settings: quicSettings
)

let clientConnection = try QuicConnection(
handler: clientHandler,
registration: registration,
configuration: clientConfiguration
)

try clientConnection.connect(to: listenAddress)

let stream1 = try clientConnection.createStream()

try? await Task.sleep(for: .milliseconds(100))
let (serverConnection, info) = serverHandler.events.value.compactMap {
switch $0 {
case let .newConnection(_, connection, info):
(connection, info) as (QuicConnection, ConnectionInfo)?
default:
nil
}
}.first!

let (ipAddress2, _) = info.remoteAddress.getAddressAndPort()

#expect(info.negotiatedAlpn == Data("testalpn".utf8))
#expect(info.serverName == "127.0.0.1")
#expect(info.localAddress == listenAddress)
#expect(ipAddress2 == "127.0.0.1")

try stream1.send(data: Data("test data 1".utf8))
serverConnection.close()

try? await Task.sleep(for: .milliseconds(1000))
let receivedData = serverHandler.events.value.compactMap {
switch $0 {
case let .dataReceived(_, data):
data

Check warning on line 381 in Networking/Tests/MsQuicSwiftTests/QuicListenerTests.swift

View check run for this annotation

Codecov / codecov/patch

Networking/Tests/MsQuicSwiftTests/QuicListenerTests.swift#L381

Added line #L381 was not covered by tests
default:
nil
}
}
#expect(receivedData == [])
}
}

0 comments on commit 0320019

Please sign in to comment.