Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix stream bug #248

Merged
merged 2 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions 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 Expand Up @@ -272,8 +272,8 @@ private class ConnectionHandle {
case QUIC_CONNECTION_EVENT_PEER_STREAM_STARTED:
logger.debug("Peer stream started")
let streamPtr = event.pointee.PEER_STREAM_STARTED.Stream
if let connection {
let stream = QuicStream(connection: connection, stream: streamPtr!, handler: connection.handler)
if let connection, let streamPtr, connection.api != nil {
let stream = QuicStream(connection: connection, stream: streamPtr, handler: connection.handler)
connection.handler.streamStarted(connection, stream: stream)
} else {
logger.warning("Stream started but connection is gone?")
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 @@
#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 == [])
}
}