-
Notifications
You must be signed in to change notification settings - Fork 420
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow client/server to be initialised with a connected socket (#1385)
Motivation: #1353 Modifications: - A new entry on ConnectionTarget - New API on the ClientConnection builder - Some validation that we're not trying to use this API with a NIOTSEventLoopGroup or NIOTSEventLoop (as far as I can tell, using a file descriptor directly is not possible with Network.framework) - Tests Result: This allows greater flexibility in spawning the client/server; in particular, it allows unix domain sockets for sandboxed apps on macOS.
- Loading branch information
Showing
6 changed files
with
123 additions
and
3 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* 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 | ||
@testable import GRPC | ||
import NIOCore | ||
import NIOPosix | ||
import XCTest | ||
|
||
class WithConnectedSockettests: GRPCTestCase { | ||
func testWithConnectedSocket() throws { | ||
let group = NIOPosix.MultiThreadedEventLoopGroup(numberOfThreads: 1) | ||
defer { | ||
XCTAssertNoThrow(try group.syncShutdownGracefully()) | ||
} | ||
|
||
let path = "/tmp/grpc-\(getpid()).sock" | ||
// Setup a server. | ||
let server = try Server.insecure(group: group) | ||
.withServiceProviders([EchoProvider()]) | ||
.withLogger(self.serverLogger) | ||
.bind(unixDomainSocketPath: path) | ||
.wait() | ||
defer { | ||
XCTAssertNoThrow(try server.close().wait()) | ||
} | ||
|
||
#if os(Linux) | ||
let sockStream = CInt(SOCK_STREAM.rawValue) | ||
#else | ||
let sockStream = SOCK_STREAM | ||
#endif | ||
let clientSocket = socket(AF_UNIX, sockStream, 0) | ||
|
||
XCTAssert(clientSocket != -1) | ||
let addr = try SocketAddress(unixDomainSocketPath: path) | ||
addr.withSockAddr { addr, size in | ||
let ret = connect(clientSocket, addr, UInt32(size)) | ||
XCTAssert(ret != -1) | ||
} | ||
let flags = fcntl(clientSocket, F_GETFL, 0) | ||
XCTAssert(flags != -1) | ||
XCTAssert(fcntl(clientSocket, F_SETFL, flags | O_NONBLOCK) == 0) | ||
|
||
let connection = ClientConnection.insecure(group: group) | ||
.withBackgroundActivityLogger(self.clientLogger) | ||
.withConnectedSocket(clientSocket) | ||
defer { | ||
XCTAssertNoThrow(try connection.close().wait()) | ||
} | ||
|
||
let client = Echo_EchoClient(channel: connection) | ||
let resp = try client.get(Echo_EchoRequest(text: "Hello")).response.wait() | ||
XCTAssertEqual(resp.text, "Swift echo get: Hello") | ||
} | ||
} |