diff --git a/Sources/NIO/ByteBuffer-core.swift b/Sources/NIO/ByteBuffer-core.swift index b34b312c7..07085f024 100644 --- a/Sources/NIO/ByteBuffer-core.swift +++ b/Sources/NIO/ByteBuffer-core.swift @@ -663,6 +663,7 @@ public struct ByteBuffer { if !isKnownUniquelyReferenced(&self._storage) { self._storage = self._storage.allocateStorage() } + self._slice = self._storage.fullSlice self._moveWriterIndex(to: 0) self._moveReaderIndex(to: 0) } diff --git a/Tests/NIOTests/ByteBufferTest+XCTest.swift b/Tests/NIOTests/ByteBufferTest+XCTest.swift index a35d15913..dc7cee8c1 100644 --- a/Tests/NIOTests/ByteBufferTest+XCTest.swift +++ b/Tests/NIOTests/ByteBufferTest+XCTest.swift @@ -107,6 +107,8 @@ extension ByteBufferTest { ("testWritableBytesAccountsForSlicing", testWritableBytesAccountsForSlicing), ("testClearDupesStorageIfTheresTwoBuffersSharingStorage", testClearDupesStorageIfTheresTwoBuffersSharingStorage), ("testClearDoesNotDupeStorageIfTheresOnlyOneBuffer", testClearDoesNotDupeStorageIfTheresOnlyOneBuffer), + ("testClearResetsTheSliceCapacityIfTheresOnlyOneBuffer", testClearResetsTheSliceCapacityIfTheresOnlyOneBuffer), + ("testClearResetsTheSliceCapacityIfTheresTwoSlicesSharingStorage", testClearResetsTheSliceCapacityIfTheresTwoSlicesSharingStorage), ("testWeUseFastWriteForContiguousCollections", testWeUseFastWriteForContiguousCollections), ("testUnderestimatingSequenceWorks", testUnderestimatingSequenceWorks), ("testZeroSizeByteBufferResizes", testZeroSizeByteBufferResizes), diff --git a/Tests/NIOTests/ByteBufferTest.swift b/Tests/NIOTests/ByteBufferTest.swift index c0ca0fd9c..f03229e73 100644 --- a/Tests/NIOTests/ByteBufferTest.swift +++ b/Tests/NIOTests/ByteBufferTest.swift @@ -238,7 +238,7 @@ class ByteBufferTest: XCTestCase { func testWithMutableWritePointerWithMinimumSpecifiedAdjustsCapacity() { XCTAssertEqual(0, buf.writerIndex) - XCTAssertEqual(512, buf.capacity) + XCTAssertEqual(1024, buf.capacity) var bytesWritten = buf.writeWithUnsafeMutableBytes(minimumWritableBytes: 256) { XCTAssertTrue($0.count >= 256) @@ -246,7 +246,7 @@ class ByteBufferTest: XCTestCase { } XCTAssertEqual(256, bytesWritten) XCTAssertEqual(256, buf.writerIndex) - XCTAssertEqual(512, buf.capacity) + XCTAssertEqual(1024, buf.capacity) bytesWritten += buf.writeWithUnsafeMutableBytes(minimumWritableBytes: 1024) { XCTAssertTrue($0.count >= 1024) @@ -260,7 +260,7 @@ class ByteBufferTest: XCTestCase { func testWithMutableWritePointerWithMinimumSpecifiedWhileAtMaxCapacity() { XCTAssertEqual(0, buf.writerIndex) - XCTAssertEqual(512, buf.capacity) + XCTAssertEqual(1024, buf.capacity) var bytesWritten = buf.writeWithUnsafeMutableBytes(minimumWritableBytes: 512) { XCTAssertTrue($0.count >= 512) @@ -268,7 +268,7 @@ class ByteBufferTest: XCTestCase { } XCTAssertEqual(512, bytesWritten) XCTAssertEqual(512, buf.writerIndex) - XCTAssertEqual(512, buf.capacity) + XCTAssertEqual(1024, buf.capacity) bytesWritten += buf.writeWithUnsafeMutableBytes(minimumWritableBytes: 1) { XCTAssertTrue($0.count >= 1) @@ -1228,6 +1228,32 @@ class ByteBufferTest: XCTestCase { XCTAssertEqual(bufPtrValPre, bufPtrValPost) } + func testClearResetsTheSliceCapacityIfTheresOnlyOneBuffer() { + let alloc = ByteBufferAllocator() + var buf = alloc.buffer(capacity: 16) + buf.writeString("qwertyuiop") + XCTAssertEqual(buf.capacity, 16) + + var slice = buf.getSlice(at: 3, length: 4)! + XCTAssertEqual(slice.capacity, 4) + + slice.clear() + XCTAssertEqual(slice.capacity, 16) + } + + func testClearResetsTheSliceCapacityIfTheresTwoSlicesSharingStorage() { + let alloc = ByteBufferAllocator() + var buf = alloc.buffer(capacity: 16) + buf.writeString("qwertyuiop") + + var slice1 = buf.getSlice(at: 3, length: 4)! + let slice2 = slice1 + + slice1.clear() + XCTAssertEqual(slice1.capacity, 16) + XCTAssertEqual(slice2.capacity, 4) + } + func testWeUseFastWriteForContiguousCollections() throws { struct WrongCollection: Collection { let storage: [UInt8] = [1, 2, 3]