@@ -243,31 +243,36 @@ class TLSSocketSuite extends TLSSuite {
243243 def setOption [A ](key : SocketOption .Key [A ], value : A ) = raw.setOption(key, value)
244244 def supportedOptions = raw.supportedOptions
245245
246- private var totalWritten : Long = 0
246+ private var totalWritten : Int = 0
247247 def write (bytes : Chunk [Byte ]) =
248248 if (totalWritten >= limit) endOfOutput
249249 else {
250- val b = bytes.take(limit)
251- raw.write(b) >> IO (totalWritten += b.size)
250+ val b = bytes.take(limit - totalWritten)
251+ raw.write(b) >> IO (totalWritten += b.size) >> IO (totalWritten >= limit)
252+ .ifM(endOfOutput, IO .unit)
252253 }
253254 }
254255
256+ // Setup an HTTPS echo server & a client that starts a TLS handshake but only sends the first few bytes and then signals no more output
257+ // Doing so should not cause the server to peg a CPU
255258 val setup = for {
256259 tlsContext <- Resource .eval(testTlsContext)
257260 serverSocket <- Network [IO ].bind(SocketAddress (ip " 127.0.0.1 " , Port .Wildcard ))
261+ echoServer =
262+ serverSocket.accept
263+ .flatMap(s => Stream .resource(tlsContext.serverBuilder(s).withLogger(logger).build))
264+ .map { socket =>
265+ socket.reads.chunks.foreach(socket.write)
266+ }
267+ .parJoinUnbounded
258268 client <- Network [IO ].connect(serverSocket.address).flatMap { rawClient =>
259- tlsContext.clientBuilder( rawClient).withLogger(logger).build
269+ tlsContext.client(limitWrites( rawClient, 10 ))
260270 }
261- } yield serverSocket.accept
262- .flatMap(s => Stream .resource(tlsContext.server(limitWrites(s, 20 )))) -> client
271+ } yield echoServer -> client
263272
264273 Stream
265274 .resource(setup)
266- .flatMap { case (server, clientSocket) =>
267- val echoServer = server.map { socket =>
268- socket.reads.chunks.foreach(socket.write(_))
269- }.parJoinUnbounded
270-
275+ .flatMap { case (echoServer, clientSocket) =>
271276 val client =
272277 Stream .exec(clientSocket.write(msg)).onFinalize(clientSocket.endOfOutput) ++
273278 clientSocket.reads.take(msg.size.toLong)
@@ -276,7 +281,6 @@ class TLSSocketSuite extends TLSSuite {
276281 }
277282 .compile
278283 .drain
279- .attempt
280284 }
281285 }
282286
0 commit comments