• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

grpc / grpc-java / #19779

15 Apr 2025 08:31AM UTC coverage: 88.598% (+0.001%) from 88.597%
#19779

push

github

web-flow
stub: Utility method StreamObservers.nextAndComplete() that does both onNext and onComplete (#11778) (#12021)

34617 of 39072 relevant lines covered (88.6%)

0.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

15.79
/../stub/src/main/java/io/grpc/stub/StreamObservers.java
1
/*
2
 * Copyright 2015 The gRPC Authors
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16

17
package io.grpc.stub;
18

19
import com.google.common.base.Preconditions;
20
import io.grpc.ExperimentalApi;
21
import java.util.Iterator;
22

23
/**
24
 * Utility functions for working with {@link StreamObserver} and it's common subclasses like
25
 * {@link CallStreamObserver}.
26
 */
27
public final class StreamObservers {
28
  // Prevent instantiation
29
  private StreamObservers() { }
30

31
  /**
32
   * Utility method to call {@link StreamObserver#onNext(Object)} and
33
   * {@link StreamObserver#onCompleted()} on the specified responseObserver.
34
   */
35
  @ExperimentalApi("https://github.com/grpc/grpc-java/issues/10957")
36
  public static <T> void nextAndComplete(StreamObserver<T> responseObserver, T response) {
37
    responseObserver.onNext(response);
1✔
38
    responseObserver.onCompleted();
1✔
39
  }
1✔
40

41
  /**
42
   * Copy the values of an {@link Iterator} to the target {@link CallStreamObserver} while properly
43
   * accounting for outbound flow-control.  After calling this method, {@code target} should no
44
   * longer be used.
45
   *
46
   * <p>For clients this method is safe to call inside {@link ClientResponseObserver#beforeStart},
47
   * on servers it is safe to call inside the service method implementation.
48
   * </p>
49
   *
50
   * @param source of values expressed as an {@link Iterator}.
51
   * @param target {@link CallStreamObserver} which accepts values from the source.
52
   * @deprecated Of questionable utility and generally not used.
53
   */
54
  @Deprecated
55
  @ExperimentalApi("https://github.com/grpc/grpc-java/issues/4694")
56
  public static <V> void copyWithFlowControl(final Iterator<V> source,
57
      final CallStreamObserver<V> target) {
58
    Preconditions.checkNotNull(source, "source");
×
59
    Preconditions.checkNotNull(target, "target");
×
60

61
    final class FlowControllingOnReadyHandler implements Runnable {
×
62
      private boolean completed;
63

64
      @Override
65
      public void run() {
66
        if (completed) {
×
67
          return;
×
68
        }
69

70
        while (target.isReady() && source.hasNext()) {
×
71
          target.onNext(source.next());
×
72
        }
73

74
        if (!source.hasNext()) {
×
75
          completed = true;
×
76
          target.onCompleted();
×
77
        }
78
      }
×
79
    }
80

81
    target.setOnReadyHandler(new FlowControllingOnReadyHandler());
×
82
  }
×
83

84
  /**
85
   * Copy the values of an {@link Iterable} to the target {@link CallStreamObserver} while properly
86
   * accounting for outbound flow-control.  After calling this method, {@code target} should no
87
   * longer be used.
88
   *
89
   * <p>For clients this method is safe to call inside {@link ClientResponseObserver#beforeStart},
90
   * on servers it is safe to call inside the service method implementation.
91
   * </p>
92
   *
93
   * @param source of values expressed as an {@link Iterable}.
94
   * @param target {@link CallStreamObserver} which accepts values from the source.
95
   * @deprecated Of questionable utility and generally not used.
96
   */
97
  @Deprecated
98
  @ExperimentalApi("https://github.com/grpc/grpc-java/issues/4694")
99
  public static <V> void copyWithFlowControl(final Iterable<V> source,
100
      CallStreamObserver<V> target) {
101
    Preconditions.checkNotNull(source, "source");
×
102
    copyWithFlowControl(source.iterator(), target);
×
103
  }
×
104
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc