2828import io .grpc .ServerInterceptor ;
2929import io .grpc .Status ;
3030import java .util .ArrayList ;
31+ import java .util .Arrays ;
32+ import java .util .Comparator ;
3133import java .util .List ;
3234import java .util .Map ;
3335import java .util .Objects ;
@@ -162,7 +164,7 @@ public MethodAndRequestId[] accumulatedUnaryValues() {
162164 this .unaryResults .forEach (
163165 (String method , CopyOnWriteArrayList <XGoogSpannerRequestId > values ) -> {
164166 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 )));
166168 }
167169 });
168170 return accumulated .toArray (new MethodAndRequestId [0 ]);
@@ -173,25 +175,33 @@ public MethodAndRequestId[] accumulatedStreamingValues() {
173175 this .streamingResults .forEach (
174176 (String method , CopyOnWriteArrayList <XGoogSpannerRequestId > values ) -> {
175177 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 )));
177179 }
178180 });
179181 return accumulated .toArray (new MethodAndRequestId [0 ]);
180182 }
181183
182184 public void checkExpectedUnaryXGoogRequestIds (MethodAndRequestId ... wantUnaryValues ) {
183185 MethodAndRequestId [] gotUnaryValues = this .accumulatedUnaryValues ();
184- System .out .println ("\033 [34mUnary: " + gotUnaryValues + "\033 [00m" );
185186 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" );
187192 }
188193 assertEquals (wantUnaryValues , gotUnaryValues );
189194 }
190195
196+ private void sortValues (MethodAndRequestId [] values ) {
197+ Arrays .sort (values , new MethodAndRequestIdComparator ());
198+ }
199+
191200 public void checkExpectedStreamingXGoogRequestIds (MethodAndRequestId ... wantStreamingValues ) {
192201 MethodAndRequestId [] gotStreamingValues = this .accumulatedStreamingValues ();
202+ sortValues (gotStreamingValues );
193203 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" );
195205 }
196206 assertEquals (wantStreamingValues , gotStreamingValues );
197207 }
@@ -205,24 +215,50 @@ public void reset() {
205215
206216 public static class MethodAndRequestId {
207217 String method ;
208- String requestId ;
218+ XGoogSpannerRequestId requestId ;
209219
210- public MethodAndRequestId (String method , String requestId ) {
220+ public MethodAndRequestId (String method , XGoogSpannerRequestId requestId ) {
211221 this .method = method ;
212222 this .requestId = requestId ;
213223 }
214224
215225 public String toString () {
216- return "{" + this .method + ":" + this .requestId + "}" ;
226+ return "{" + this .method + ":" + this .requestId . toString () + "}" ;
217227 }
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+ }
218254 }
219255
220256 public static MethodAndRequestId ofMethodAndRequestId (String method , String reqId ) {
221- return new MethodAndRequestId (method , reqId );
257+ return new MethodAndRequestId (method , XGoogSpannerRequestId . of ( reqId ) );
222258 }
223259
224260 public static MethodAndRequestId ofMethodAndRequestId (
225261 String method , XGoogSpannerRequestId reqId ) {
226- return new MethodAndRequestId (method , reqId . toString () );
262+ return new MethodAndRequestId (method , reqId );
227263 }
228264}
0 commit comments