Explorar o código

Merge branch 'master' of github.com:grpc/grpc-swift into wait-timeout

Daniel Alm %!s(int64=7) %!d(string=hai) anos
pai
achega
419decfbf4

+ 1 - 0
.gitignore

@@ -1,6 +1,7 @@
 .DS_Store
 project.xcworkspace
 xcuserdata
+DerivedData/
 .build
 /protoc-gen-swift
 /protoc-gen-swiftgrpc

+ 2 - 0
Sources/SwiftGRPC/Core/Channel.swift

@@ -41,6 +41,7 @@ public class Channel {
   /// - Parameter address: the address of the server to be called
   /// - Parameter secure: if true, use TLS
   public init(address: String, secure: Bool = true) {
+    gRPC.initialize()
     host = address
     if secure {
       underlyingChannel = cgrpc_channel_create_secure(address, roots_pem(), nil)
@@ -58,6 +59,7 @@ public class Channel {
   /// - Parameter certificates: a PEM representation of certificates to use
   /// - Parameter host: an optional hostname override
   public init(address: String, certificates: String, host: String?) {
+    gRPC.initialize()
     self.host = address
     underlyingChannel = cgrpc_channel_create_secure(address, certificates, host)
     completionQueue = CompletionQueue(

+ 7 - 0
Sources/SwiftGRPC/Runtime/ServiceClient.swift

@@ -55,6 +55,13 @@ open class ServiceClientBase: ServiceClient {
     metadata = Metadata()
   }
 
+  /// Create a client using a pre-defined channel.
+  public init(channel: Channel) {
+    gRPC.initialize()
+    self.channel = channel
+    self.metadata = Metadata()
+  }
+
   /// Create a client that makes secure connections with a custom certificate and (optional) hostname.
   public init(address: String, certificates: String, host: String?) {
     gRPC.initialize()

+ 4 - 4
SwiftGRPC.podspec

@@ -29,7 +29,7 @@
 
 Pod::Spec.new do |s|
   s.name = 'SwiftGRPC'
-  s.version = '0.3.3'
+  s.version = '0.4.1'
   s.license  = 'New BSD'
   s.summary = 'Swift gRPC code generator plugin and runtime library'
   s.homepage = 'http://www.grpc.io'
@@ -45,7 +45,7 @@ Pod::Spec.new do |s|
   s.source_files = 'Sources/SwiftGRPC/*.swift', 'Sources/SwiftGRPC/**/*.swift', 'Sources/CgRPC/shim/*.[ch]'
   s.public_header_files = 'Sources/CgRPC/shim/cgrpc.h'
 
-  s.dependency 'gRPC-Core', '~> 1.9.1'
-  s.dependency 'BoringSSL', '~> 9.1'
-  s.dependency 'SwiftProtobuf', '~> 1.0.2'
+  s.dependency 'gRPC-Core', '~> 1.11.0'
+  s.dependency 'BoringSSL', '~> 10.0'
+  s.dependency 'SwiftProtobuf', '~> 1.0.3'
 end

+ 4 - 5
Tests/SwiftGRPCTests/BasicEchoTestCase.swift

@@ -32,21 +32,20 @@ extension Echo_EchoResponse {
 
 class BasicEchoTestCase: XCTestCase {
   func makeProvider() -> Echo_EchoProvider { return EchoProvider() }
-  
-  var defaultTimeout: TimeInterval { return 1.0 }
-  
+
   var provider: Echo_EchoProvider!
   var server: Echo_EchoServer!
   var client: Echo_EchoServiceClient!
   
+  var defaultTimeout: TimeInterval { return 1.0 }
   var secure: Bool { return false }
-  
+  var address: String { return "localhost:5050" }
+
   override func setUp() {
     super.setUp()
     
     provider = makeProvider()
     
-    let address = "localhost:5050"
     if secure {
       let certificateString = String(data: certificateForTests, encoding: .utf8)!
       server = Echo_EchoServer(address: address,

+ 68 - 0
Tests/SwiftGRPCTests/ServiceClientTests.swift

@@ -0,0 +1,68 @@
+/*
+ * Copyright 2018, 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 Foundation
+@testable import SwiftGRPC
+import XCTest
+
+class ServiceClientTests: BasicEchoTestCase {
+  private lazy var sharedChannel = Channel(address: address, secure: false)
+
+  override func setUp() {
+    super.setUp()
+    sharedChannel = Channel(address: address, secure: false)
+  }
+
+  func testSharingChannelBetweenClientsUnaryAsync() {
+    let firstCallExpectation = expectation(description: "First call completes successfully")
+    let secondCallExpectation = expectation(description: "Second call completes successfully")
+
+    do {
+      let client1 = Echo_EchoServiceClient(channel: sharedChannel)
+      try _ = client1.get(Echo_EchoRequest(text: "foo")) { _, callResult in
+        XCTAssertEqual(.ok, callResult.statusCode)
+        firstCallExpectation.fulfill()
+      }
+
+      let client2 = Echo_EchoServiceClient(channel: sharedChannel)
+      try _ = client2.get(Echo_EchoRequest(text: "foo")) { _, callResult in
+        XCTAssertEqual(.ok, callResult.statusCode)
+        secondCallExpectation.fulfill()
+      }
+    } catch let error {
+      XCTFail(error.localizedDescription)
+    }
+
+    waitForExpectations(timeout: defaultTimeout)
+  }
+
+  func testSharedChannelStillWorksAfterFirstUnaryClientCompletes() {
+    do {
+      let client1 = Echo_EchoServiceClient(channel: sharedChannel)
+      let response1 = try client1.get(Echo_EchoRequest(text: "foo")).text
+      XCTAssertEqual("Swift echo get: foo", response1)
+    } catch let error {
+      XCTFail(error.localizedDescription)
+    }
+
+    do {
+      let client2 = Echo_EchoServiceClient(channel: sharedChannel)
+      let response2 = try client2.get(Echo_EchoRequest(text: "foo")).text
+      XCTAssertEqual("Swift echo get: foo", response2)
+    } catch let error {
+      XCTFail(error.localizedDescription)
+    }
+  }
+}