-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Add support for deduplication testing #10246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
115 changes: 115 additions & 0 deletions
115
pulsar-testclient/src/main/java/org/apache/pulsar/testclient/DefaultMessageFormatter.java
This file contains hidden or 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,115 @@ | ||
| /** | ||
| * 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.pulsar.testclient; | ||
|
|
||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.Random; | ||
|
|
||
| public class DefaultMessageFormatter implements IMessageFormatter { | ||
| Random r = new Random(); | ||
|
|
||
|
|
||
| @Override | ||
| public byte[] formatMessage(String producerName, long msgId, byte[] message) { | ||
| String sMessage = new String(message, StandardCharsets.UTF_8); | ||
| if (producerName != null && !producerName.isEmpty()) { | ||
| sMessage = sMessage.replaceAll("%p", producerName); | ||
| } | ||
| sMessage = sMessage.replaceAll("%i", String.valueOf(msgId)); | ||
| sMessage = sMessage.replaceAll("%t", String.valueOf(System.nanoTime())); | ||
|
|
||
| int idx = sMessage.indexOf("%"); | ||
| while (idx > 0) { | ||
|
|
||
| float size = 0; | ||
| int i=1; | ||
| for (; idx+i < sMessage.length(); i++) { | ||
| char c = sMessage.charAt(idx + i); | ||
| if (Character.isDigit(c) && c != '.') { | ||
| continue; | ||
| } | ||
| if (c == '.' || c == '-') { | ||
| continue; | ||
| } | ||
| break; | ||
| } | ||
| if (i != 1) { | ||
| size = Float.valueOf(new String(sMessage.substring(idx+1,idx+i))); | ||
| } | ||
|
|
||
| String sub = sMessage.substring(idx, idx+i+1); | ||
|
|
||
| if (sMessage.charAt(idx+i) == 'f') { | ||
| sMessage=sMessage.replaceFirst(sub, getFloatValue(size)); | ||
| } else if (sMessage.charAt(idx+i) == 'l') { | ||
| sMessage=sMessage.replaceFirst(sub, getLongValue(size)); | ||
| } else if (sMessage.charAt(idx+i) == 'd') { | ||
| sMessage = sMessage.replaceFirst(sub, getIntValue(size)); | ||
| } else if (sMessage.charAt(idx+i) == 's') { | ||
| sMessage = sMessage.replaceFirst(sub, getStringValue(size)); | ||
| } | ||
| idx = sMessage.indexOf("%", idx); | ||
| } | ||
| return sMessage.getBytes(StandardCharsets.UTF_8); | ||
| } | ||
|
|
||
| private float _getFloatValue(float size) { | ||
| float f = r.nextFloat(); | ||
| int mag = (int) Math.abs(size); | ||
| f = f * (float) Math.pow(10, mag); | ||
| if (size < 0 && ((int) f) % 2 == 1) { | ||
| return f * -1; | ||
| } | ||
| return f; | ||
| } | ||
|
|
||
| private String getStringValue(float size) { | ||
| int s = (int) size; | ||
| if (size == 0) { | ||
| size = 20; | ||
| }; | ||
| String result = ""; | ||
| for(int i = 0; i < s; i++) { | ||
| result = result + (char) ((int) 'a' + (int) (r.nextFloat() * 26)); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| private String getFloatValue(float size) { | ||
| if (size == 0) { | ||
| return String.valueOf(r.nextFloat()); | ||
| } | ||
| String format = "%" + String.valueOf(size) + "f"; | ||
|
|
||
| return String.format(format, _getFloatValue(size)); | ||
| } | ||
|
|
||
| private String getIntValue(float size) { | ||
| if (size == 0) { | ||
| return String.valueOf(r.nextInt()); | ||
| } | ||
| return String.valueOf((int) _getFloatValue(size)); | ||
| } | ||
| private String getLongValue(float size) { | ||
| if (size == 0) { | ||
| return String.valueOf(r.nextLong()); | ||
| } | ||
| return String.valueOf((long) _getFloatValue(size)); | ||
| } | ||
| } |
23 changes: 23 additions & 0 deletions
23
pulsar-testclient/src/main/java/org/apache/pulsar/testclient/IMessageFormatter.java
This file contains hidden or 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,23 @@ | ||
| /** | ||
| * 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.pulsar.testclient; | ||
|
|
||
| public interface IMessageFormatter { | ||
| byte[] formatMessage(String producerName, long msg, byte[] message); | ||
| } |
This file contains hidden or 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 hidden or 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
74 changes: 74 additions & 0 deletions
74
...ar-testclient/src/test/java/org/apache/pulsar/testclient/TestDefaultMessageFormatter.java
This file contains hidden or 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,74 @@ | ||
| /** | ||
| * 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.pulsar.testclient; | ||
|
|
||
| import com.fasterxml.jackson.core.JsonParseException; | ||
| import com.fasterxml.jackson.databind.JsonNode; | ||
| import org.testng.Assert; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| import java.nio.charset.StandardCharsets; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
|
||
| public class TestDefaultMessageFormatter { | ||
|
|
||
| @Test | ||
| public void testFormatMessage() { | ||
| String producerName = "producer-1"; | ||
| long msgId = 3; | ||
| byte[] message = "{ \"producer\": \"%p\", \"msgId\": %i, \"nanoTime\": %t, \"float1\": %5.2f, \"float2\": %-5.2f, \"long1\": %12l, \"long2\": %l, \"int1\": %d, \"int2\": %1d , \"long3\": %5l, \"str\": \"%5s\" }".getBytes(); | ||
| byte[] formatted = new DefaultMessageFormatter().formatMessage(producerName, msgId, message); | ||
| String jsonString = new String(formatted, StandardCharsets.UTF_8); | ||
|
|
||
| ObjectMapper objectMapper = new ObjectMapper(); | ||
|
|
||
| JsonNode obj = null; | ||
| try { | ||
| obj = objectMapper.readValue(jsonString, JsonNode.class); | ||
|
|
||
| } catch(Exception jpe) { | ||
| Assert.fail("Exception parsing json"); | ||
| } | ||
|
|
||
| String prod = obj.get("producer").asText(); | ||
| int mid = obj.get("msgId").asInt(); | ||
| long nt = obj.get("nanoTime").asLong(); | ||
| float f1 = obj.get("float1").floatValue(); | ||
| float f2 = obj.get("float2").floatValue(); | ||
| long l1 = obj.get("long1").asLong(); | ||
| long l2 = obj.get("long2").asLong(); | ||
| long i1 = obj.get("int1").asInt(); | ||
| long i2 = obj.get("int2").asInt(); | ||
| String str = obj.get("str").asText(); | ||
| long l3 = obj.get("long3").asLong(); | ||
| Assert.assertEquals(producerName, prod); | ||
| Assert.assertEquals(msgId, mid); | ||
| Assert.assertTrue( nt > 0); | ||
| Assert.assertNotEquals(f1, f2); | ||
| Assert.assertNotEquals(l1, l2); | ||
| Assert.assertNotEquals(i1, i2); | ||
| Assert.assertTrue(l3 > 0); | ||
| Assert.assertTrue(l3 <= 99999); | ||
| Assert.assertTrue(i2 < 10); | ||
| Assert.assertTrue(0 < i2); | ||
| Assert.assertTrue(f2 < 100000); | ||
| Assert.assertTrue( -100000 < f2); | ||
|
|
||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.