-
Notifications
You must be signed in to change notification settings - Fork 419
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Release stream callback, once the stream has finished (#1363)
- Loading branch information
1 parent
ff11e2a
commit be02b34
Showing
3 changed files
with
106 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
Tests/GRPCTests/StreamResponseHandlerRetainCycleTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Copyright 2022, gRPC Authors All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import EchoImplementation | ||
import EchoModel | ||
import GRPC | ||
import NIOConcurrencyHelpers | ||
import NIOCore | ||
import NIOPosix | ||
import XCTest | ||
|
||
final class StreamResponseHandlerRetainCycleTests: GRPCTestCase { | ||
var group: EventLoopGroup! | ||
var server: Server! | ||
var client: ClientConnection! | ||
|
||
var echo: Echo_EchoClient! | ||
|
||
override func setUp() { | ||
super.setUp() | ||
self.group = MultiThreadedEventLoopGroup(numberOfThreads: 1) | ||
|
||
self.server = try! Server.insecure(group: self.group) | ||
.withServiceProviders([EchoProvider()]) | ||
.withLogger(self.serverLogger) | ||
.bind(host: "localhost", port: 0) | ||
.wait() | ||
|
||
self.client = ClientConnection.insecure(group: self.group) | ||
.withBackgroundActivityLogger(self.clientLogger) | ||
.connect(host: "localhost", port: self.server.channel.localAddress!.port!) | ||
|
||
self.echo = Echo_EchoClient( | ||
channel: self.client, | ||
defaultCallOptions: CallOptions(logger: self.clientLogger) | ||
) | ||
} | ||
|
||
override func tearDown() { | ||
XCTAssertNoThrow(try self.client.close().wait()) | ||
XCTAssertNoThrow(try self.server.close().wait()) | ||
XCTAssertNoThrow(try self.group.syncShutdownGracefully()) | ||
super.tearDown() | ||
} | ||
|
||
func testHandlerClosureIsReleasedOnceStreamEnds() { | ||
final class Counter { | ||
private let atomic = NIOAtomic.makeAtomic(value: 0) | ||
func increment() { self.atomic.add(1) } | ||
var value: Int { | ||
self.atomic.load() | ||
} | ||
} | ||
|
||
var counter = Counter() | ||
XCTAssertTrue(isKnownUniquelyReferenced(&counter)) | ||
let get = self.echo.update { [capturedCounter = counter] _ in | ||
capturedCounter.increment() | ||
} | ||
XCTAssertFalse(isKnownUniquelyReferenced(&counter)) | ||
|
||
get.sendMessage(.init(text: "hello world"), promise: nil) | ||
XCTAssertFalse(isKnownUniquelyReferenced(&counter)) | ||
XCTAssertNoThrow(try get.sendEnd().wait()) | ||
XCTAssertNoThrow(try get.status.wait()) | ||
XCTAssertEqual(counter.value, 1) | ||
XCTAssertTrue(isKnownUniquelyReferenced(&counter)) | ||
} | ||
} |