Skip to content

Commit

Permalink
Merge pull request findingrish#4 from andrisnoko/add-tests
Browse files Browse the repository at this point in the history
add empty response and GrpcResponseHandler tests
  • Loading branch information
paul-rogers authored Feb 23, 2023
2 parents 2e08d27 + 74a8057 commit 159caf4
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.apache.druid.grpc.proto.QueryOuterClass.QueryResponse;
import org.apache.druid.grpc.proto.QueryOuterClass.QueryResultFormat;
import org.apache.druid.grpc.proto.QueryOuterClass.QueryStatus;
import org.apache.druid.grpc.proto.TestResults;
import org.apache.druid.grpc.proto.TestResults.QueryResult;
import org.apache.druid.grpc.server.QueryDriver;
import org.apache.druid.grpc.server.QueryServer;
import org.junit.AfterClass;
Expand Down Expand Up @@ -104,19 +104,34 @@ public void testBasics()
* Do a very basic query that output protobuf.
*/
@Test
public void testGRPCBasics()
public void testGrpcBasics()
{
QueryRequest request = QueryRequest.newBuilder()
.setQuery("SELECT dim1, dim2, dim3, cnt, m1, m2, unique_dim1, __time AS \"date\" FROM foo")
.setProtobufMessageName(TestResults.QueryResult.class.getName())
.setProtobufMessageName(QueryResult.class.getName())
.setResultFormat(QueryResultFormat.PROTOBUF_INLINE)
.build();

QueryResponse response = client.client.submitQuery(request);
GrpcResponseHandler<TestResults.QueryResult> handler = GrpcResponseHandler.of(TestResults.QueryResult.class);
List<TestResults.QueryResult> queryResults = handler.get(response.getData());

GrpcResponseHandler<QueryResult> handler = GrpcResponseHandler.of(QueryResult.class);
List<QueryResult> queryResults = handler.get(response.getData());
assertEquals(6, queryResults.size());
assertEquals(QueryStatus.OK, response.getStatus());
}

@Test
public void testGrpcEmptyResponse()
{
QueryRequest request = QueryRequest.newBuilder()
.setQuery("SELECT dim1, dim2, dim3, cnt, m1, m2, unique_dim1, __time AS \"date\" FROM foo where cnt = 100000")
.setProtobufMessageName(QueryResult.class.getName())
.setResultFormat(QueryResultFormat.PROTOBUF_INLINE)
.build();

QueryResponse response = client.client.submitQuery(request);
GrpcResponseHandler<QueryResult> handler = GrpcResponseHandler.of(QueryResult.class);
List<QueryResult> queryResults = handler.get(response.getData());
assertEquals(0, queryResults.size());
assertEquals(QueryStatus.OK, response.getStatus());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.druid.grpc.client;

import com.google.protobuf.ByteString;
import org.apache.druid.grpc.proto.QueryOuterClass.QueryResponse;
import org.apache.druid.grpc.proto.TestResults.QueryResult;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class GrpcResponseHandlerTest
{
private static List<QueryResult> EXPECTED_RESULTS = new ArrayList<>();

@BeforeClass
public static void setup()
{
EXPECTED_RESULTS.add(QueryResult.newBuilder().setDim1("test").setCnt(100).build());
EXPECTED_RESULTS.add(QueryResult.newBuilder().setDim2("test2").setCnt(100).setM2(200.10).build());
}

@Test
public void testEmptyResponse()
{
GrpcResponseHandler<QueryResult> handler = GrpcResponseHandler.of(QueryResult.class);
List<QueryResult> queryResults = handler.get(ByteString.EMPTY);
assertTrue(queryResults.isEmpty());
}

@Test
public void testNonEmptysponse()
{
GrpcResponseHandler<QueryResult> handler = GrpcResponseHandler.of(QueryResult.class);
QueryResponse queryResponse = getQueryResponse();
List<QueryResult> queryResults = handler.get(queryResponse.getData());
assertEquals(2, queryResults.size());
assertEquals(EXPECTED_RESULTS, queryResults);
}

private static QueryResponse getQueryResponse()
{
try {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
for (QueryResult queryResult : EXPECTED_RESULTS) {
queryResult.writeDelimitedTo(out);
}
return QueryResponse.newBuilder()
.setData(ByteString.copyFrom(out.toByteArray()))
.build();
}
catch (IOException e) {
throw new RuntimeException(e);
}
}
}

0 comments on commit 159caf4

Please sign in to comment.