|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the SwiftNIO open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2019-2021 Apple Inc. and the SwiftNIO project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of SwiftNIO project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import NIOCore |
| 16 | +import NIOEmbedded |
| 17 | +import NIOSSL |
| 18 | + |
| 19 | +func runManyWrites(writeCount: Int) throws { |
| 20 | + let serverContext = try NIOSSLContext( |
| 21 | + configuration: .makeServerConfiguration( |
| 22 | + certificateChain: [.certificate(.forTesting())], |
| 23 | + privateKey: .privateKey(.forTesting()) |
| 24 | + ) |
| 25 | + ) |
| 26 | + |
| 27 | + var clientConfig = TLSConfiguration.makeClientConfiguration() |
| 28 | + clientConfig.trustRoots = try .certificates([.forTesting()]) |
| 29 | + let clientContext = try NIOSSLContext(configuration: clientConfig) |
| 30 | + |
| 31 | + let dummyAddress = try SocketAddress(ipAddress: "1.2.3.4", port: 5678) |
| 32 | + let backToBack = BackToBackEmbeddedChannel() |
| 33 | + let serverHandler = NIOSSLServerHandler(context: serverContext) |
| 34 | + let clientHandler = try NIOSSLClientHandler(context: clientContext, serverHostname: "localhost") |
| 35 | + try backToBack.client.pipeline.addHandler(clientHandler).wait() |
| 36 | + try backToBack.server.pipeline.addHandler(serverHandler).wait() |
| 37 | + |
| 38 | + // To trigger activation of both channels we use connect(). |
| 39 | + try backToBack.client.connect(to: dummyAddress).wait() |
| 40 | + try backToBack.server.connect(to: dummyAddress).wait() |
| 41 | + |
| 42 | + try backToBack.interactInMemory() |
| 43 | + |
| 44 | + // Let's try 512 bytes. |
| 45 | + var buffer = backToBack.client.allocator.buffer(capacity: 512) |
| 46 | + buffer.writeBytes(repeatElement(0, count: 512)) |
| 47 | + |
| 48 | + for _ in 0..<writeCount { |
| 49 | + // A vector of 100 writes. |
| 50 | + for _ in 0..<100 { |
| 51 | + backToBack.client.write(buffer, promise: nil) |
| 52 | + } |
| 53 | + backToBack.client.flush() |
| 54 | + |
| 55 | + try backToBack.interactInMemory() |
| 56 | + |
| 57 | + // Pull any data out of the server to avoid ballooning in memory. |
| 58 | + while let _ = try backToBack.server.readInbound(as: ByteBuffer.self) {} |
| 59 | + } |
| 60 | +} |
0 commit comments