|
2 | 2 | // |
3 | 3 | // This source file is part of the SwiftNIO open source project |
4 | 4 | // |
5 | | -// Copyright (c) 2019 Apple Inc. and the SwiftNIO project authors |
| 5 | +// Copyright (c) 2019-2021 Apple Inc. and the SwiftNIO project authors |
6 | 6 | // Licensed under Apache License v2.0 |
7 | 7 | // |
8 | 8 | // See LICENSE.txt for license information |
@@ -1192,12 +1192,7 @@ final class ChildChannelMultiplexerTests: XCTestCase { |
1192 | 1192 | XCTAssertNoThrow(try harness.multiplexer.receiveMessage(self.openConfirmation(originalChannelID: channelID!, peerChannelID: 1))) |
1193 | 1193 |
|
1194 | 1194 | // The default window size is 1<<24 bytes. Sadly, we need a buffer that size. |
1195 | | - // Sorry for the unsafe code, but otherwise this test takes _ages_. |
1196 | | - var buffer = channel.allocator.buffer(capacity: (1 << 24) + 1) |
1197 | | - buffer.writeWithUnsafeMutableBytes(minimumWritableBytes: (1 << 24) + 1) { ptr in |
1198 | | - memset(ptr.baseAddress!, 0, (1 << 24) + 1) |
1199 | | - return (1 << 24) + 1 |
1200 | | - } |
| 1195 | + let buffer = ByteBuffer.bigBuffer |
1201 | 1196 |
|
1202 | 1197 | // We're going to write one byte short. |
1203 | 1198 | XCTAssertEqual(harness.flushedMessages.count, 1) |
@@ -1237,6 +1232,46 @@ final class ChildChannelMultiplexerTests: XCTestCase { |
1237 | 1232 | self.assertChannelClose(harness.flushedMessages.last, recipientChannel: 1) |
1238 | 1233 | } |
1239 | 1234 |
|
| 1235 | + func testWeDontResizeTheWindowOnClose() throws { |
| 1236 | + let harness = self.harnessForbiddingInboundChannels() |
| 1237 | + defer { |
| 1238 | + harness.finish() |
| 1239 | + } |
| 1240 | + |
| 1241 | + var childChannel: Channel? |
| 1242 | + harness.multiplexer.createChildChannel(channelType: .session) { channel, _ in |
| 1243 | + childChannel = channel |
| 1244 | + return channel.setOption(ChannelOptions.autoRead, value: false) |
| 1245 | + } |
| 1246 | + |
| 1247 | + guard let channel = childChannel else { |
| 1248 | + XCTFail("Did not create child channel") |
| 1249 | + return |
| 1250 | + } |
| 1251 | + |
| 1252 | + // Activate channel. |
| 1253 | + let channelID = self.assertChannelOpen(harness.flushedMessages.first) |
| 1254 | + XCTAssertNoThrow(try harness.multiplexer.receiveMessage(self.openConfirmation(originalChannelID: channelID!, peerChannelID: 1))) |
| 1255 | + |
| 1256 | + // The default window size is 1<<24 bytes. Sadly, we need a buffer that size. |
| 1257 | + let buffer = ByteBuffer.bigBuffer |
| 1258 | + |
| 1259 | + // We're going to write the whole window. |
| 1260 | + XCTAssertEqual(harness.flushedMessages.count, 1) |
| 1261 | + XCTAssertNoThrow(try harness.multiplexer.receiveMessage(self.data(peerChannelID: channelID!, |
| 1262 | + data: buffer.getSlice(at: buffer.readerIndex, length: 1 << 24)!))) |
| 1263 | + |
| 1264 | + // Auto read is off, so nothing happens. |
| 1265 | + XCTAssertEqual(harness.flushedMessages.count, 1) |
| 1266 | + |
| 1267 | + // Now we send a close message. This is going to forcibly close the channel immediately. |
| 1268 | + XCTAssertNoThrow(try harness.multiplexer.receiveMessage(self.close(peerChannelID: channelID!))) |
| 1269 | + |
| 1270 | + // This should trigger no message. |
| 1271 | + // (Actually, it should trigger a close, but that's a different bug and to be fixed in a different patch.) |
| 1272 | + XCTAssertEqual(harness.flushedMessages.count, 1) |
| 1273 | + } |
| 1274 | + |
1240 | 1275 | func testRespectingMaxMessageSize() throws { |
1241 | 1276 | let harness = self.harnessForbiddingInboundChannels() |
1242 | 1277 | defer { |
@@ -1694,3 +1729,12 @@ final class ChildChannelMultiplexerTests: XCTestCase { |
1694 | 1729 | self.assertChannelOpenConfirmation(harness.flushedMessages.first, recipientChannel: 1) |
1695 | 1730 | } |
1696 | 1731 | } |
| 1732 | + |
| 1733 | +extension ByteBuffer { |
| 1734 | + /// A buffer `(1 << 24) + 1` bytes large. |
| 1735 | + fileprivate static let bigBuffer: ByteBuffer = { |
| 1736 | + // The default window size is 1<<24 bytes. Sadly, we need a buffer that size. |
| 1737 | + // We store it in a static so that we don't have to re-create it for every test. |
| 1738 | + ByteBuffer(repeating: 0, count: (1 << 24) + 1) |
| 1739 | + }() |
| 1740 | +} |
0 commit comments