28
28
import io .grpc .ServerInterceptor ;
29
29
import io .grpc .Status ;
30
30
import java .util .ArrayList ;
31
+ import java .util .Arrays ;
32
+ import java .util .Comparator ;
31
33
import java .util .List ;
32
34
import java .util .Map ;
33
35
import java .util .Objects ;
@@ -162,7 +164,7 @@ public MethodAndRequestId[] accumulatedUnaryValues() {
162
164
this .unaryResults .forEach (
163
165
(String method , CopyOnWriteArrayList <XGoogSpannerRequestId > values ) -> {
164
166
for (int i = 0 ; i < values .size (); i ++) {
165
- accumulated .add (new MethodAndRequestId (method , values .get (i ). toString () ));
167
+ accumulated .add (new MethodAndRequestId (method , values .get (i )));
166
168
}
167
169
});
168
170
return accumulated .toArray (new MethodAndRequestId [0 ]);
@@ -173,25 +175,33 @@ public MethodAndRequestId[] accumulatedStreamingValues() {
173
175
this .streamingResults .forEach (
174
176
(String method , CopyOnWriteArrayList <XGoogSpannerRequestId > values ) -> {
175
177
for (int i = 0 ; i < values .size (); i ++) {
176
- accumulated .add (new MethodAndRequestId (method , values .get (i ). toString () ));
178
+ accumulated .add (new MethodAndRequestId (method , values .get (i )));
177
179
}
178
180
});
179
181
return accumulated .toArray (new MethodAndRequestId [0 ]);
180
182
}
181
183
182
184
public void checkExpectedUnaryXGoogRequestIds (MethodAndRequestId ... wantUnaryValues ) {
183
185
MethodAndRequestId [] gotUnaryValues = this .accumulatedUnaryValues ();
184
- System .out .println ("\033 [34mUnary: " + gotUnaryValues + "\033 [00m" );
185
186
for (int i = 0 ; i < gotUnaryValues .length ; i ++) {
186
- System .out .println ("ith: " + i + ":: " + gotUnaryValues [i ]);
187
+ System .out .println ("\033 [34misUnary: #" + i + ":: " + gotUnaryValues [i ] + "\033 [00m" );
188
+ }
189
+ sortValues (gotUnaryValues );
190
+ for (int i = 0 ; i < gotUnaryValues .length ; i ++) {
191
+ System .out .println ("\033 [33misUnary: #" + i + ":: " + gotUnaryValues [i ] + "\033 [00m" );
187
192
}
188
193
assertEquals (wantUnaryValues , gotUnaryValues );
189
194
}
190
195
196
+ private void sortValues (MethodAndRequestId [] values ) {
197
+ Arrays .sort (values , new MethodAndRequestIdComparator ());
198
+ }
199
+
191
200
public void checkExpectedStreamingXGoogRequestIds (MethodAndRequestId ... wantStreamingValues ) {
192
201
MethodAndRequestId [] gotStreamingValues = this .accumulatedStreamingValues ();
202
+ sortValues (gotStreamingValues );
193
203
for (int i = 0 ; i < gotStreamingValues .length ; i ++) {
194
- System .out .println ("ith: " + i + ":: " + gotStreamingValues [i ]);
204
+ System .out .println ("\033 [32misStreaming: # " + i + ":: " + gotStreamingValues [i ] + " \033 [00m" );
195
205
}
196
206
assertEquals (wantStreamingValues , gotStreamingValues );
197
207
}
@@ -205,24 +215,50 @@ public void reset() {
205
215
206
216
public static class MethodAndRequestId {
207
217
String method ;
208
- String requestId ;
218
+ XGoogSpannerRequestId requestId ;
209
219
210
- public MethodAndRequestId (String method , String requestId ) {
220
+ public MethodAndRequestId (String method , XGoogSpannerRequestId requestId ) {
211
221
this .method = method ;
212
222
this .requestId = requestId ;
213
223
}
214
224
215
225
public String toString () {
216
- return "{" + this .method + ":" + this .requestId + "}" ;
226
+ return "{" + this .method + ":" + this .requestId . toString () + "}" ;
217
227
}
228
+
229
+ @ Override
230
+ public boolean equals (Object o ) {
231
+ if (!(o instanceof MethodAndRequestId )) {
232
+ return false ;
233
+ }
234
+ MethodAndRequestId other = (MethodAndRequestId ) o ;
235
+ return Objects .equals (this .method , other .method ) && Objects .equals (this .requestId , other .requestId );
236
+ }
237
+ }
238
+
239
+ static class MethodAndRequestIdComparator implements Comparator <MethodAndRequestId > {
240
+ @ Override
241
+ public int compare (MethodAndRequestId mr1 , MethodAndRequestId mr2 ) {
242
+ int cmpMethod = mr1 .method .compareTo (mr2 .method );
243
+ if (cmpMethod != 0 ) {
244
+ return cmpMethod ;
245
+ }
246
+ if (Objects .equals (mr1 .requestId , mr2 .requestId )) {
247
+ return 0 ;
248
+ }
249
+ if (mr1 .requestId .isGreaterThan (mr2 .requestId )) {
250
+ return +1 ;
251
+ }
252
+ return -1 ;
253
+ }
218
254
}
219
255
220
256
public static MethodAndRequestId ofMethodAndRequestId (String method , String reqId ) {
221
- return new MethodAndRequestId (method , reqId );
257
+ return new MethodAndRequestId (method , XGoogSpannerRequestId . of ( reqId ) );
222
258
}
223
259
224
260
public static MethodAndRequestId ofMethodAndRequestId (
225
261
String method , XGoogSpannerRequestId reqId ) {
226
- return new MethodAndRequestId (method , reqId . toString () );
262
+ return new MethodAndRequestId (method , reqId );
227
263
}
228
264
}
0 commit comments