-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add implementation of ChannelPrimer to establish connection to …
…GFE and integrate into bigtable client
- Loading branch information
1 parent
4e77ace
commit f7aede6
Showing
5 changed files
with
152 additions
and
0 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
48 changes: 48 additions & 0 deletions
48
...oud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/RefreshChannel.java
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,48 @@ | ||
/* | ||
* Copyright 2019 Google LLC | ||
* | ||
* 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 | ||
* | ||
* https://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. | ||
*/ | ||
package com.google.cloud.bigtable.data.v2.internal; | ||
|
||
import com.google.api.core.BetaApi; | ||
import com.google.api.core.InternalApi; | ||
import com.google.api.gax.grpc.ChannelPrimer; | ||
import io.grpc.ConnectivityState; | ||
import io.grpc.ManagedChannel; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* Establish a connection to the Cloud Bigtable service on managedChannel | ||
* | ||
* <p>This class is considered an internal implementation detail and not meant to be used by | ||
* applications. | ||
*/ | ||
@BetaApi("This API depends on gRPC experimental API") | ||
@InternalApi | ||
public final class RefreshChannel implements ChannelPrimer { | ||
@Override | ||
public void primeChannel(ManagedChannel managedChannel) { | ||
for (int i = 0; i < 10; i++) { | ||
ConnectivityState connectivityState = managedChannel.getState(true); | ||
if (connectivityState == ConnectivityState.READY) { | ||
break; | ||
} | ||
try { | ||
TimeUnit.SECONDS.sleep(1); | ||
} catch (InterruptedException e) { | ||
break; | ||
} | ||
} | ||
} | ||
} |
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
38 changes: 38 additions & 0 deletions
38
...bigtable/src/test/java/com/google/cloud/bigtable/data/v2/internal/RefreshChannelTest.java
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,38 @@ | ||
/* | ||
* Copyright 2019 Google LLC | ||
* | ||
* 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 | ||
* | ||
* https://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. | ||
*/ | ||
package com.google.cloud.bigtable.data.v2.internal; | ||
|
||
import io.grpc.ConnectivityState; | ||
import io.grpc.ManagedChannel; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
import org.mockito.Mockito; | ||
|
||
@RunWith(JUnit4.class) | ||
public class RefreshChannelTest { | ||
// RefreshChannel should establish connection to the server through managedChannel.getState(true) | ||
@Test | ||
public void testGetStateIsCalled() { | ||
RefreshChannel refreshChannel = new RefreshChannel(); | ||
ManagedChannel managedChannel = Mockito.mock(ManagedChannel.class); | ||
|
||
Mockito.doReturn(ConnectivityState.READY).when(managedChannel).getState(true); | ||
|
||
refreshChannel.primeChannel(managedChannel); | ||
Mockito.verify(managedChannel, Mockito.atLeastOnce()).getState(true); | ||
} | ||
} |
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