From e57cd90c11285c8643bbfd2fb778093129806ac9 Mon Sep 17 00:00:00 2001
From: Alexander Polcyn <apolcyn@google.com>
Date: Thu, 2 Mar 2017 16:13:46 -0800
Subject: [PATCH] fix channel connectivity state function

---
 src/ruby/ext/grpc/rb_channel.c | 20 ++++++++++++--------
 src/ruby/spec/channel_spec.rb  | 29 +++++++++++++++++++++++++++++
 2 files changed, 41 insertions(+), 8 deletions(-)

diff --git a/src/ruby/ext/grpc/rb_channel.c b/src/ruby/ext/grpc/rb_channel.c
index 84e43d3f7b..a284852500 100644
--- a/src/ruby/ext/grpc/rb_channel.c
+++ b/src/ruby/ext/grpc/rb_channel.c
@@ -175,19 +175,23 @@ static VALUE grpc_rb_channel_init(int argc, VALUE *argv, VALUE self) {
 
 /*
   call-seq:
-    insecure_channel = Channel:new("myhost:8080", {'arg1': 'value1'})
-    creds = ...
-    secure_channel = Channel:new("myhost:443", {'arg1': 'value1'}, creds)
+    ch.connectivity_state       -> state
+    ch.connectivity_state(true) -> state
 
-  Creates channel instances. */
+  Indicates the current state of the channel, whose value is one of the
+  constants defined in GRPC::Core::ConnectivityStates.
+
+  It also tries to connect if the chennel is idle in the second form. */
 static VALUE grpc_rb_channel_get_connectivity_state(int argc, VALUE *argv,
                                                     VALUE self) {
-  VALUE try_to_connect = Qfalse;
+  VALUE try_to_connect_param = Qfalse;
+  int grpc_try_to_connect = 0;
   grpc_rb_channel *wrapper = NULL;
   grpc_channel *ch = NULL;
 
   /* "01" == 0 mandatory args, 1 (try_to_connect) is optional */
-  rb_scan_args(argc, argv, "01", try_to_connect);
+  rb_scan_args(argc, argv, "01", &try_to_connect_param);
+  grpc_try_to_connect = try_to_connect_param == Qtrue? 1 : 0;
 
   TypedData_Get_Struct(self, grpc_rb_channel, &grpc_channel_data_type, wrapper);
   ch = wrapper->wrapped;
@@ -195,8 +199,8 @@ static VALUE grpc_rb_channel_get_connectivity_state(int argc, VALUE *argv,
     rb_raise(rb_eRuntimeError, "closed!");
     return Qnil;
   }
-  return NUM2LONG(
-      grpc_channel_check_connectivity_state(ch, (int)try_to_connect));
+  return LONG2NUM(
+      grpc_channel_check_connectivity_state(ch, grpc_try_to_connect));
 }
 
 /* Watch for a change in connectivity state.
diff --git a/src/ruby/spec/channel_spec.rb b/src/ruby/spec/channel_spec.rb
index 740eac631a..a289a00f04 100644
--- a/src/ruby/spec/channel_spec.rb
+++ b/src/ruby/spec/channel_spec.rb
@@ -153,6 +153,35 @@ describe GRPC::Core::Channel do
     end
   end
 
+  describe '#connectivity_state' do
+    it 'returns an enum' do
+      ch = GRPC::Core::Channel.new(fake_host, nil, :this_channel_is_insecure)
+      valid_states = [
+        GRPC::Core::ConnectivityStates::IDLE,
+        GRPC::Core::ConnectivityStates::CONNECTING,
+        GRPC::Core::ConnectivityStates::READY,
+        GRPC::Core::ConnectivityStates::TRANSIENT_FAILURE,
+        GRPC::Core::ConnectivityStates::FATAL_FAILURE
+      ]
+
+      expect(valid_states).to include(ch.connectivity_state)
+    end
+
+    it 'returns an enum when trying to connect' do
+      ch = GRPC::Core::Channel.new(fake_host, nil, :this_channel_is_insecure)
+      ch.connectivity_state(true)
+      valid_states = [
+        GRPC::Core::ConnectivityStates::IDLE,
+        GRPC::Core::ConnectivityStates::CONNECTING,
+        GRPC::Core::ConnectivityStates::READY,
+        GRPC::Core::ConnectivityStates::TRANSIENT_FAILURE,
+        GRPC::Core::ConnectivityStates::FATAL_FAILURE
+      ]
+
+      expect(valid_states).to include(ch.connectivity_state)
+    end
+  end
+
   describe '::SSL_TARGET' do
     it 'is a symbol' do
       expect(GRPC::Core::Channel::SSL_TARGET).to be_a(Symbol)
-- 
GitLab