1 /*
2  * Copyright 2017 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.internal.testing;
18 
19 import io.grpc.ServerStreamTracer;
20 import io.grpc.Status;
21 import java.util.concurrent.TimeUnit;
22 import java.util.concurrent.atomic.AtomicReference;
23 
24 /**
25  * A {@link ServerStreamTracer} suitable for testing.
26  */
27 public class TestServerStreamTracer extends ServerStreamTracer implements TestStreamTracer {
28   private final TestBaseStreamTracer delegate = new TestBaseStreamTracer();
29   protected final AtomicReference<ServerCallInfo<?, ?>> serverCallInfo =
30       new AtomicReference<ServerCallInfo<?, ?>>();
31 
32   @Override
await()33   public void await() throws InterruptedException {
34     delegate.await();
35   }
36 
37   @Override
await(long timeout, TimeUnit timeUnit)38   public boolean await(long timeout, TimeUnit timeUnit) throws InterruptedException {
39     return delegate.await(timeout, timeUnit);
40   }
41 
42   /**
43    * Returns the ServerCall passed to {@link ServerStreamTracer#serverCallStarted}.
44    */
getServerCallInfo()45   public ServerCallInfo<?, ?> getServerCallInfo() {
46     return serverCallInfo.get();
47   }
48 
49   @Override
getStatus()50   public Status getStatus() {
51     return delegate.getStatus();
52   }
53 
54   @Override
getInboundWireSize()55   public long getInboundWireSize() {
56     return delegate.getInboundWireSize();
57   }
58 
59   @Override
getInboundUncompressedSize()60   public long getInboundUncompressedSize() {
61     return delegate.getInboundUncompressedSize();
62   }
63 
64   @Override
getOutboundWireSize()65   public long getOutboundWireSize() {
66     return delegate.getOutboundWireSize();
67   }
68 
69   @Override
getOutboundUncompressedSize()70   public long getOutboundUncompressedSize() {
71     return delegate.getOutboundUncompressedSize();
72   }
73 
74   @Override
setFailDuplicateCallbacks(boolean fail)75   public void setFailDuplicateCallbacks(boolean fail) {
76     delegate.setFailDuplicateCallbacks(fail);
77   }
78 
79   @Override
nextOutboundEvent()80   public String nextOutboundEvent() {
81     return delegate.nextOutboundEvent();
82   }
83 
84   @Override
nextInboundEvent()85   public String nextInboundEvent() {
86     return delegate.nextInboundEvent();
87   }
88 
89   @Override
outboundWireSize(long bytes)90   public void outboundWireSize(long bytes) {
91     delegate.outboundWireSize(bytes);
92   }
93 
94   @Override
inboundWireSize(long bytes)95   public void inboundWireSize(long bytes) {
96     delegate.inboundWireSize(bytes);
97   }
98 
99   @Override
outboundUncompressedSize(long bytes)100   public void outboundUncompressedSize(long bytes) {
101     delegate.outboundUncompressedSize(bytes);
102   }
103 
104   @Override
inboundUncompressedSize(long bytes)105   public void inboundUncompressedSize(long bytes) {
106     delegate.inboundUncompressedSize(bytes);
107   }
108 
109   @Override
streamClosed(Status status)110   public void streamClosed(Status status) {
111     delegate.streamClosed(status);
112   }
113 
114   @Override
inboundMessage(int seqNo)115   public void inboundMessage(int seqNo) {
116     delegate.inboundMessage(seqNo);
117   }
118 
119   @Override
outboundMessage(int seqNo)120   public void outboundMessage(int seqNo) {
121     delegate.outboundMessage(seqNo);
122   }
123 
124   @Override
outboundMessageSent(int seqNo, long optionalWireSize, long optionalUncompressedSize)125   public void outboundMessageSent(int seqNo, long optionalWireSize, long optionalUncompressedSize) {
126     delegate.outboundMessageSent(seqNo, optionalWireSize, optionalUncompressedSize);
127   }
128 
129   @Override
inboundMessageRead(int seqNo, long optionalWireSize, long optionalUncompressedSize)130   public void inboundMessageRead(int seqNo, long optionalWireSize, long optionalUncompressedSize) {
131     delegate.inboundMessageRead(seqNo, optionalWireSize, optionalUncompressedSize);
132   }
133 
134   @Override
serverCallStarted(ServerCallInfo<?, ?> callInfo)135   public void serverCallStarted(ServerCallInfo<?, ?> callInfo) {
136     if (!serverCallInfo.compareAndSet(null, callInfo) && delegate.failDuplicateCallbacks.get()) {
137       throw new AssertionError("serverCallStarted called more than once");
138     }
139   }
140 }
141