This repository has been archived by the owner on Jan 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
74cdfe4
commit 0780706
Showing
4 changed files
with
91 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
kafka-impl/src/test/java/io/streamnative/pulsar/handlers/kop/EntryFormatterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
* 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 io.streamnative.pulsar.handlers.kop; | ||
|
||
import static org.testng.Assert.assertEquals; | ||
|
||
import io.streamnative.pulsar.handlers.kop.format.EntryFormatter; | ||
import java.nio.ByteBuffer; | ||
import java.util.Arrays; | ||
import java.util.stream.IntStream; | ||
import org.apache.kafka.common.record.CompressionType; | ||
import org.apache.kafka.common.record.MemoryRecords; | ||
import org.apache.kafka.common.record.MemoryRecordsBuilder; | ||
import org.apache.kafka.common.record.RecordBatch; | ||
import org.apache.kafka.common.record.TimestampType; | ||
import org.testng.annotations.DataProvider; | ||
import org.testng.annotations.Test; | ||
|
||
/** | ||
* Tests for EntryFormatter. | ||
*/ | ||
public class EntryFormatterTest { | ||
|
||
@DataProvider(name = "compressionTypes") | ||
Object[] allCompressionTypes() { | ||
return Arrays.stream(CompressionType.values()).map(x -> (Object) x).toArray(); | ||
} | ||
|
||
@Test(dataProvider = "compressionTypes") | ||
public void testParseNumMessages(CompressionType compressionType) { | ||
ByteBuffer buffer = ByteBuffer.allocate(1024); | ||
int[] batchSizes = {3, 2, 5}; | ||
|
||
int baseOffset = 0; | ||
for (int batchSize : batchSizes) { | ||
MemoryRecordsBuilder builder = MemoryRecords.builder(buffer, RecordBatch.CURRENT_MAGIC_VALUE, | ||
compressionType, TimestampType.LOG_APPEND_TIME, baseOffset); | ||
for (int i = 0; i < batchSize; i++) { | ||
builder.append(0L, "a".getBytes(), "1".getBytes()); | ||
} | ||
baseOffset += batchSize; | ||
// Normally the offsets of batches are continuous, here we add an extra interval just for robustness. | ||
baseOffset += 1; | ||
builder.close(); | ||
} | ||
|
||
buffer.flip(); | ||
final MemoryRecords records = MemoryRecords.readableRecords(buffer); | ||
assertEquals(EntryFormatter.parseNumMessages(records), IntStream.of(batchSizes).sum()); | ||
} | ||
} |