Search in sources :

Example 46 with ManagedChannel

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ManagedChannel in project beam by apache.

the class GrpcLoggingServiceTest method testServerCloseHangsUpClients.

@Test
public void testServerCloseHangsUpClients() throws Exception {
    LinkedBlockingQueue<LogEntry> logs = new LinkedBlockingQueue<>();
    ExecutorService executorService = Executors.newCachedThreadPool();
    Collection<Future<Void>> futures = new ArrayList<>();
    final GrpcLoggingService service = GrpcLoggingService.forWriter(new CollectionAppendingLogWriter(logs));
    try (GrpcFnServer<GrpcLoggingService> server = GrpcFnServer.allocatePortAndCreateFor(service, InProcessServerFactory.create())) {
        for (int i = 1; i <= 3; ++i) {
            final long instructionId = i;
            futures.add(executorService.submit(() -> {
                {
                    CountDownLatch waitForServerHangup = new CountDownLatch(1);
                    ManagedChannel channel = InProcessChannelBuilder.forName(server.getApiServiceDescriptor().getUrl()).build();
                    StreamObserver<LogEntry.List> outboundObserver = BeamFnLoggingGrpc.newStub(channel).logging(TestStreams.withOnNext(messageDiscarder).withOnCompleted(new CountDown(waitForServerHangup)).build());
                    outboundObserver.onNext(createLogsWithIds(instructionId));
                    waitForServerHangup.await();
                    return null;
                }
            }));
        }
        // Wait till each client has sent their message showing that they have connected.
        for (int i = 1; i <= 3; ++i) {
            logs.take();
        }
    }
    for (Future<Void> future : futures) {
        future.get();
    }
}
Also used : ArrayList(java.util.ArrayList) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) ManagedChannel(org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ManagedChannel) ArrayList(java.util.ArrayList) LogEntry(org.apache.beam.model.fnexecution.v1.BeamFnApi.LogEntry) Test(org.junit.Test)

Example 47 with ManagedChannel

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ManagedChannel in project grpc-java by grpc.

the class AndroidChannelBuilderTest method networkChanges_api24.

@Test
@Config(sdk = 24)
public void networkChanges_api24() {
    shadowOf(connectivityManager).setActiveNetworkInfo(MOBILE_DISCONNECTED);
    TestChannel delegateChannel = new TestChannel();
    ManagedChannel androidChannel = new AndroidChannelBuilder.AndroidChannel(delegateChannel, ApplicationProvider.getApplicationContext());
    assertThat(delegateChannel.enterIdleCount).isEqualTo(0);
    // Establish an initial network connection
    shadowOf(connectivityManager).setActiveNetworkInfo(MOBILE_CONNECTED);
    assertThat(delegateChannel.enterIdleCount).isEqualTo(1);
    // Switch to another network to trigger enterIdle()
    shadowOf(connectivityManager).setActiveNetworkInfo(WIFI_CONNECTED);
    assertThat(delegateChannel.enterIdleCount).isEqualTo(2);
    // Switch to an offline network and then to null
    shadowOf(connectivityManager).setActiveNetworkInfo(WIFI_DISCONNECTED);
    shadowOf(connectivityManager).setActiveNetworkInfo(null);
    assertThat(delegateChannel.enterIdleCount).isEqualTo(2);
    // Establish a connection
    shadowOf(connectivityManager).setActiveNetworkInfo(MOBILE_CONNECTED);
    assertThat(delegateChannel.enterIdleCount).isEqualTo(3);
    // Disconnect, then shutdown the channel and verify that the callback has been unregistered
    shadowOf(connectivityManager).setActiveNetworkInfo(null);
    androidChannel.shutdown();
    shadowOf(connectivityManager).setActiveNetworkInfo(MOBILE_CONNECTED);
    assertThat(delegateChannel.enterIdleCount).isEqualTo(3);
}
Also used : ManagedChannel(io.grpc.ManagedChannel) Test(org.junit.Test) Config(org.robolectric.annotation.Config)

Example 48 with ManagedChannel

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ManagedChannel in project grpc-java by grpc.

the class AndroidChannelBuilderTest method shutdownNowUnregistersNetworkCallback_api24.

@Test
@Config(sdk = 24)
public void shutdownNowUnregistersNetworkCallback_api24() {
    shadowOf(connectivityManager).setActiveNetworkInfo(null);
    TestChannel delegateChannel = new TestChannel();
    ManagedChannel androidChannel = new AndroidChannelBuilder.AndroidChannel(delegateChannel, ApplicationProvider.getApplicationContext());
    androidChannel.shutdownNow();
    shadowOf(connectivityManager).setActiveNetworkInfo(WIFI_CONNECTED);
    assertThat(delegateChannel.enterIdleCount).isEqualTo(0);
}
Also used : ManagedChannel(io.grpc.ManagedChannel) Test(org.junit.Test) Config(org.robolectric.annotation.Config)

Example 49 with ManagedChannel

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ManagedChannel in project grpc-java by grpc.

the class AndroidChannelBuilderTest method nullContextDoesNotThrow_api24.

@Test
@Config(sdk = 24)
public void nullContextDoesNotThrow_api24() {
    shadowOf(connectivityManager).setActiveNetworkInfo(MOBILE_DISCONNECTED);
    TestChannel delegateChannel = new TestChannel();
    ManagedChannel androidChannel = new AndroidChannelBuilder.AndroidChannel(delegateChannel, null);
    // Network change and shutdown should be no-op for the channel without an Android Context
    shadowOf(connectivityManager).setActiveNetworkInfo(MOBILE_CONNECTED);
    androidChannel.shutdown();
    assertThat(delegateChannel.enterIdleCount).isEqualTo(0);
}
Also used : ManagedChannel(io.grpc.ManagedChannel) Test(org.junit.Test) Config(org.robolectric.annotation.Config)

Example 50 with ManagedChannel

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ManagedChannel in project grpc-java by grpc.

the class AndroidChannelBuilderTest method shutdownNowUnregistersBroadcastReceiver_api23.

@Test
@Config(sdk = 23)
public void shutdownNowUnregistersBroadcastReceiver_api23() {
    TestChannel delegateChannel = new TestChannel();
    ManagedChannel androidChannel = new AndroidChannelBuilder.AndroidChannel(delegateChannel, ApplicationProvider.getApplicationContext());
    shadowOf(connectivityManager).setActiveNetworkInfo(null);
    ApplicationProvider.getApplicationContext().sendBroadcast(new Intent(ConnectivityManager.CONNECTIVITY_ACTION));
    androidChannel.shutdownNow();
    shadowOf(connectivityManager).setActiveNetworkInfo(WIFI_CONNECTED);
    ApplicationProvider.getApplicationContext().sendBroadcast(new Intent(ConnectivityManager.CONNECTIVITY_ACTION));
    assertThat(delegateChannel.enterIdleCount).isEqualTo(0);
}
Also used : ManagedChannel(io.grpc.ManagedChannel) Intent(android.content.Intent) Test(org.junit.Test) Config(org.robolectric.annotation.Config)

Aggregations

ManagedChannel (io.grpc.ManagedChannel)163 Test (org.junit.Test)92 CountDownLatch (java.util.concurrent.CountDownLatch)26 ManagedChannel (org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ManagedChannel)26 ArrayList (java.util.ArrayList)20 Metadata (io.grpc.Metadata)18 EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)15 ExecutorService (java.util.concurrent.ExecutorService)13 ByteString (com.google.protobuf.ByteString)12 Status (io.grpc.Status)12 StreamObserver (org.apache.beam.vendor.grpc.v1p43p2.io.grpc.stub.StreamObserver)11 StreamObserver (io.grpc.stub.StreamObserver)10 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)10 AtomicReference (java.util.concurrent.atomic.AtomicReference)10 BeamFnApi (org.apache.beam.model.fnexecution.v1.BeamFnApi)10 CallOptions (io.grpc.CallOptions)9 Subchannel (io.grpc.LoadBalancer.Subchannel)9 SubchannelPicker (io.grpc.LoadBalancer.SubchannelPicker)9 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)9 Endpoints (org.apache.beam.model.pipeline.v1.Endpoints)9