@Test public void responseCorrectlyTransformed() throws Exception { ApiFuture<List<KeyOffset>> result = callable.futureCall("my-table"); inner.response.set( ImmutableList.of( SampleRowKeysResponse.newBuilder() .setRowKey(ByteString.copyFromUtf8("key1")) .setOffsetBytes(100) .build(), SampleRowKeysResponse.newBuilder() .setRowKey(ByteString.copyFromUtf8("")) .setOffsetBytes(1000) .build())); assertThat(result.get(1, TimeUnit.SECONDS)) .isEqualTo( ImmutableList.of( KeyOffset.create(ByteString.copyFromUtf8("key1"), 100), KeyOffset.create(ByteString.EMPTY, 1000))); }
@Test @SuppressWarnings("all") public void sampleRowKeysTest() throws Exception { ByteString rowKey = ByteString.copyFromUtf8("122"); long offsetBytes = 889884095L; SampleRowKeysResponse expectedResponse = SampleRowKeysResponse.newBuilder().setRowKey(rowKey).setOffsetBytes(offsetBytes).build(); mockBigtable.addResponse(expectedResponse); TableName tableName = TableName.of("[PROJECT]", "[INSTANCE]", "[TABLE]"); SampleRowKeysRequest request = SampleRowKeysRequest.newBuilder().setTableName(tableName.toString()).build(); MockStreamObserver<SampleRowKeysResponse> responseObserver = new MockStreamObserver<>(); ServerStreamingCallable<SampleRowKeysRequest, SampleRowKeysResponse> callable = client.sampleRowKeysCallable(); callable.serverStreamingCall(request, responseObserver); List<SampleRowKeysResponse> actualResponses = responseObserver.future().get(); Assert.assertEquals(1, actualResponses.size()); Assert.assertEquals(expectedResponse, actualResponses.get(0)); }
/** Sets up the sample row keys for the specified table. */ void setupSampleRowKeys(String tableId, int numSamples, long bytesPerRow) { verifyTableExists(tableId); checkArgument(numSamples > 0, "Number of samples must be positive: %s", numSamples); checkArgument(bytesPerRow > 0, "Bytes/Row must be positive: %s", bytesPerRow); ImmutableList.Builder<SampleRowKeysResponse> ret = ImmutableList.builder(); SortedMap<ByteString, ByteString> rows = getTable(tableId); int currentSample = 1; int rowsSoFar = 0; for (Map.Entry<ByteString, ByteString> entry : rows.entrySet()) { if (((double) rowsSoFar) / rows.size() >= ((double) currentSample) / numSamples) { // add the sample with the total number of bytes in the table before this key. ret.add( SampleRowKeysResponse.newBuilder() .setRowKey(entry.getKey()) .setOffsetBytes(rowsSoFar * bytesPerRow) .build()); // Move on to next sample currentSample++; } ++rowsSoFar; } // Add the last sample indicating the end of the table, with all rows before it. ret.add(SampleRowKeysResponse.newBuilder().setOffsetBytes(rows.size() * bytesPerRow).build()); sampleRowKeys.put(tableId, ret.build()); } }