|
| 1 | +/* |
| 2 | + * Copyright 2022, gRPC Authors All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +import EchoImplementation |
| 17 | +import EchoModel |
| 18 | +import GRPC |
| 19 | +import NIOConcurrencyHelpers |
| 20 | +import NIOCore |
| 21 | +import NIOPosix |
| 22 | +import XCTest |
| 23 | + |
| 24 | +final class StreamResponseHandlerRetainCycleTests: GRPCTestCase { |
| 25 | + var group: EventLoopGroup! |
| 26 | + var server: Server! |
| 27 | + var client: ClientConnection! |
| 28 | + |
| 29 | + var echo: Echo_EchoClient! |
| 30 | + |
| 31 | + override func setUp() { |
| 32 | + super.setUp() |
| 33 | + self.group = MultiThreadedEventLoopGroup(numberOfThreads: 1) |
| 34 | + |
| 35 | + self.server = try! Server.insecure(group: self.group) |
| 36 | + .withServiceProviders([EchoProvider()]) |
| 37 | + .withLogger(self.serverLogger) |
| 38 | + .bind(host: "localhost", port: 0) |
| 39 | + .wait() |
| 40 | + |
| 41 | + self.client = ClientConnection.insecure(group: self.group) |
| 42 | + .withBackgroundActivityLogger(self.clientLogger) |
| 43 | + .connect(host: "localhost", port: self.server.channel.localAddress!.port!) |
| 44 | + |
| 45 | + self.echo = Echo_EchoClient( |
| 46 | + channel: self.client, |
| 47 | + defaultCallOptions: CallOptions(logger: self.clientLogger) |
| 48 | + ) |
| 49 | + } |
| 50 | + |
| 51 | + override func tearDown() { |
| 52 | + XCTAssertNoThrow(try self.client.close().wait()) |
| 53 | + XCTAssertNoThrow(try self.server.close().wait()) |
| 54 | + XCTAssertNoThrow(try self.group.syncShutdownGracefully()) |
| 55 | + super.tearDown() |
| 56 | + } |
| 57 | + |
| 58 | + func testHandlerClosureIsReleasedOnceStreamEnds() { |
| 59 | + final class Counter { |
| 60 | + private let atomic = NIOAtomic.makeAtomic(value: 0) |
| 61 | + func increment() { self.atomic.add(1) } |
| 62 | + var value: Int { |
| 63 | + self.atomic.load() |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + var counter = Counter() |
| 68 | + XCTAssertTrue(isKnownUniquelyReferenced(&counter)) |
| 69 | + let get = self.echo.update { [capturedCounter = counter] _ in |
| 70 | + capturedCounter.increment() |
| 71 | + } |
| 72 | + XCTAssertFalse(isKnownUniquelyReferenced(&counter)) |
| 73 | + |
| 74 | + get.sendMessage(.init(text: "hello world"), promise: nil) |
| 75 | + XCTAssertFalse(isKnownUniquelyReferenced(&counter)) |
| 76 | + XCTAssertNoThrow(try get.sendEnd().wait()) |
| 77 | + XCTAssertNoThrow(try get.status.wait()) |
| 78 | + XCTAssertEqual(counter.value, 1) |
| 79 | + XCTAssertTrue(isKnownUniquelyReferenced(&counter)) |
| 80 | + } |
| 81 | +} |
0 commit comments