-
Notifications
You must be signed in to change notification settings - Fork 118
/
CompositeTracerTest.java
277 lines (238 loc) · 9.4 KB
/
CompositeTracerTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
* Copyright 2024 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
*
* http://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.spanner;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import com.google.api.gax.tracing.ApiTracer;
import com.google.api.gax.tracing.ApiTracer.Scope;
import com.google.api.gax.tracing.MetricsTracer;
import com.google.common.collect.ImmutableList;
import com.google.spanner.v1.ReadRequest;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.threeten.bp.Duration;
@RunWith(JUnit4.class)
public class CompositeTracerTest {
@Rule public final MockitoRule mockitoRule = MockitoJUnit.rule();
@Mock private ApiTracer child1;
@Mock private ApiTracer child2;
@Mock private OpenTelemetryApiTracer child3;
@Mock private MetricsTracer child4;
private CompositeTracer compositeTracer;
@Before
public void setup() {
compositeTracer = new CompositeTracer(ImmutableList.of(child1, child2, child3, child4));
}
@Test
public void testInScope() {
Scope scope1 = mock(Scope.class);
when(child1.inScope()).thenReturn(scope1);
Scope scope2 = mock(Scope.class);
when(child2.inScope()).thenReturn(scope2);
Scope scope3 = mock(Scope.class);
when(child3.inScope()).thenReturn(scope3);
Scope scope4 = mock(Scope.class);
when(child4.inScope()).thenReturn(scope4);
Scope parentScope = compositeTracer.inScope();
parentScope.close();
verify(scope1, times(1)).close();
verify(scope2, times(1)).close();
verify(scope3, times(1)).close();
verify(scope4, times(1)).close();
}
@Test
public void testOperationSucceeded() {
compositeTracer.operationSucceeded();
verify(child1, times(1)).operationSucceeded();
verify(child2, times(1)).operationSucceeded();
verify(child3, times(1)).operationSucceeded();
verify(child4, times(1)).operationSucceeded();
}
@Test
public void testOperationCancelled() {
compositeTracer.operationCancelled();
verify(child1, times(1)).operationCancelled();
verify(child2, times(1)).operationCancelled();
verify(child3, times(1)).operationCancelled();
verify(child4, times(1)).operationCancelled();
}
@Test
public void testOperationFailed() {
RuntimeException error = new RuntimeException();
compositeTracer.operationFailed(error);
verify(child1, times(1)).operationFailed(error);
verify(child2, times(1)).operationFailed(error);
verify(child3, times(1)).operationFailed(error);
verify(child4, times(1)).operationFailed(error);
}
@Test
public void testConnectionSelected() {
compositeTracer.connectionSelected("connection-one");
verify(child1, times(1)).connectionSelected("connection-one");
verify(child2, times(1)).connectionSelected("connection-one");
verify(child3, times(1)).connectionSelected("connection-one");
verify(child4, times(1)).connectionSelected("connection-one");
}
@Test
public void testAttemptStarted() {
ReadRequest request = ReadRequest.getDefaultInstance();
compositeTracer.attemptStarted(request, 3);
verify(child1, times(1)).attemptStarted(request, 3);
verify(child2, times(1)).attemptStarted(request, 3);
verify(child3, times(1)).attemptStarted(request, 3);
verify(child4, times(1)).attemptStarted(request, 3);
}
@Test
public void testAttemptSucceeded() {
compositeTracer.attemptSucceeded();
verify(child1, times(1)).attemptSucceeded();
verify(child2, times(1)).attemptSucceeded();
verify(child3, times(1)).attemptSucceeded();
verify(child4, times(1)).attemptSucceeded();
}
@Test
public void testAttemptCancelled() {
compositeTracer.attemptCancelled();
verify(child1, times(1)).attemptCancelled();
verify(child2, times(1)).attemptCancelled();
verify(child3, times(1)).attemptCancelled();
verify(child4, times(1)).attemptCancelled();
}
@Test
public void testAttemptFailed() {
RuntimeException error = new RuntimeException();
Duration delay = Duration.ofMillis(10);
compositeTracer.attemptFailed(error, delay);
verify(child1, times(1)).attemptFailed(error, delay);
verify(child2, times(1)).attemptFailed(error, delay);
verify(child3, times(1)).attemptFailed(error, delay);
verify(child4, times(1)).attemptFailed(error, delay);
}
@Test
public void testAttemptFailedRetriesExhausted() {
RuntimeException error = new RuntimeException();
compositeTracer.attemptFailedRetriesExhausted(error);
verify(child1, times(1)).attemptFailedRetriesExhausted(error);
verify(child2, times(1)).attemptFailedRetriesExhausted(error);
verify(child3, times(1)).attemptFailedRetriesExhausted(error);
verify(child4, times(1)).attemptFailedRetriesExhausted(error);
}
@Test
public void testAttemptPermanentFailure() {
RuntimeException error = new RuntimeException();
compositeTracer.attemptPermanentFailure(error);
verify(child1, times(1)).attemptPermanentFailure(error);
verify(child2, times(1)).attemptPermanentFailure(error);
verify(child3, times(1)).attemptPermanentFailure(error);
verify(child4, times(1)).attemptPermanentFailure(error);
}
@Test
public void testLroStartFailed() {
RuntimeException error = new RuntimeException();
compositeTracer.lroStartFailed(error);
verify(child1, times(1)).lroStartFailed(error);
verify(child2, times(1)).lroStartFailed(error);
verify(child3, times(1)).lroStartFailed(error);
verify(child4, times(1)).lroStartFailed(error);
}
@Test
public void testLroStartSucceeded() {
compositeTracer.lroStartSucceeded();
verify(child1, times(1)).lroStartSucceeded();
verify(child2, times(1)).lroStartSucceeded();
verify(child3, times(1)).lroStartSucceeded();
verify(child4, times(1)).lroStartSucceeded();
}
@Test
public void testResponseReceived() {
compositeTracer.responseReceived();
verify(child1, times(1)).responseReceived();
verify(child2, times(1)).responseReceived();
verify(child3, times(1)).responseReceived();
verify(child4, times(1)).responseReceived();
}
@Test
public void testRequestSent() {
compositeTracer.requestSent();
verify(child1, times(1)).requestSent();
verify(child2, times(1)).requestSent();
verify(child3, times(1)).requestSent();
verify(child4, times(1)).requestSent();
}
@Test
public void testBatchRequestSent() {
compositeTracer.batchRequestSent(2, 20);
verify(child1, times(1)).batchRequestSent(2, 20);
verify(child2, times(1)).batchRequestSent(2, 20);
verify(child3, times(1)).batchRequestSent(2, 20);
verify(child4, times(1)).batchRequestSent(2, 20);
}
@Test
public void testMethodsOverrideMetricsTracer() {
Method[] metricsTracerMethods = MetricsTracer.class.getDeclaredMethods();
Method[] compositeTracerMethods = CompositeTracer.class.getDeclaredMethods();
List<String> visibleForTestingMethods = Arrays.asList("getAttributes", "extractStatus");
Set<Method> compositeMethodsSet = new HashSet<>(Arrays.asList(compositeTracerMethods));
for (Method metricsMethod : metricsTracerMethods) {
if (!visibleForTestingMethods.contains(metricsMethod.getName())
&& !containsMethod(compositeMethodsSet, metricsMethod)) {
throw new AssertionError("Method not found in compositeTracerMethods: " + metricsMethod);
}
}
}
@Test
public void testMethodsOverrideOpenTelemetryTracer() {
Method[] compositeTracerMethods = CompositeTracer.class.getDeclaredMethods();
List<Method> openTelemetryTracerMethods =
Arrays.stream(OpenTelemetryApiTracer.class.getDeclaredMethods())
.filter(method -> java.lang.reflect.Modifier.isPublic(method.getModifiers()))
.collect(Collectors.toList());
Set<Method> compositeMethodsSet = new HashSet<>(Arrays.asList(compositeTracerMethods));
for (Method metricsMethod : openTelemetryTracerMethods) {
if (!containsMethod(compositeMethodsSet, metricsMethod)) {
throw new AssertionError("Method not found in compositeTracerMethods: " + metricsMethod);
}
}
}
private boolean compareMethods(Method actual, Method expected) {
return actual.getName().equals(expected.getName())
&& Arrays.equals(actual.getParameterTypes(), expected.getParameterTypes())
&& actual.getModifiers() == expected.getModifiers()
&& actual.getReturnType().equals(expected.getReturnType());
}
public boolean containsMethod(Set<Method> methodSet, Method method) {
for (Method m : methodSet) {
if (compareMethods(m, method)) {
return true;
}
}
return false;
}
}