Skip to content

Commit

Permalink
fix: Implement DefaultRoutingPolicyTest (#188)
Browse files Browse the repository at this point in the history
* fix: Implement DefaultRoutingPolicyTest, which has a fixed set of test cases to ensure that all client libraries route the same keys to the same partitions.

* fix: Add license header to DefaultRoutingPolicyTest

* fix: Run fmt:format
  • Loading branch information
dpcollins-google committed Aug 21, 2020
1 parent 30a276b commit fa49931
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
5 changes: 5 additions & 0 deletions google-cloud-pubsublite/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@
<version>2.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<scope>test</scope>
</dependency>
<!-- Need testing utility classes for generated gRPC clients tests -->
<dependency>
<groupId>com.google.api</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2020 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.pubsublite.internal;

import static com.google.common.truth.Truth.assertThat;

import com.google.cloud.pubsublite.Partition;
import com.google.common.collect.ImmutableMap;
import com.google.gson.Gson;
import com.google.protobuf.ByteString;
import io.grpc.StatusException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Map;
import java.util.function.Supplier;
import org.junit.Test;

public class DefaultRoutingPolicyTest {
private static final int NUM_PARTITIONS = 29;
private static final RoutingPolicy policy =
((Supplier<RoutingPolicy>)
() -> {
try {
return new DefaultRoutingPolicy(NUM_PARTITIONS);
} catch (StatusException e) {
throw e.getStatus().asRuntimeException();
}
})
.get();

private static Map<ByteString, Partition> loadTestCases() throws Exception {
Gson gson = new Gson();
String json =
new String(
Files.readAllBytes(
Paths.get(
DefaultRoutingPolicyTest.class.getResource("/routing_tests.json").toURI())));
Map<String, Double> map = gson.fromJson(json, Map.class);
ImmutableMap.Builder<ByteString, Partition> output = ImmutableMap.builder();
for (String key : map.keySet()) {
output.put(ByteString.copyFromUtf8(key), Partition.of((int) map.get(key).doubleValue()));
}
return output.build();
}

@Test
public void routingPerformedCorrectly() throws Exception {
Map<ByteString, Partition> map = loadTestCases();
ImmutableMap.Builder<ByteString, Partition> results = ImmutableMap.builder();
for (ByteString key : map.keySet()) {
results.put(key, policy.route(key));
}
assertThat(results.build()).containsExactlyEntriesIn(map);
}
}
20 changes: 20 additions & 0 deletions google-cloud-pubsublite/src/test/resources/routing_tests.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// File defining a map from routing keys to the expected partition in a 29
// partition topic for test purposes. This file should be copied into all client
// library test suites.
{
"oaisdhfoiahsd": 18,
"P(#*YNPOIUDF": 9,
"LCIUNDFPOASIUN":8,
";odsfiupoius": 9,
"OPISUDfpoiu": 2,
"dokjwO:IDf": 21,
"%^&*": 19,
"XXXXXXXXX": 15,
"dpcollins": 28,
"#()&$IJHLOIURF": 2,
"dfasiduyf": 6,
"983u2poer": 3,
"8888888": 6,
"OPUIPOUYPOIOPUIOIPUOUIPJOP": 2,
"x": 16
}

0 comments on commit fa49931

Please sign in to comment.