Skip to content

Commit

Permalink
Re #53: Moved test helper code out of main sources.
Browse files Browse the repository at this point in the history
  • Loading branch information
tkowalcz committed Jun 18, 2021
1 parent e0dfd67 commit a2b5625
Show file tree
Hide file tree
Showing 12 changed files with 47 additions and 40 deletions.
5 changes: 4 additions & 1 deletion core/src/main/java/pl/tkowalcz/tjahzi/LabelSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,14 @@ public void finishAppendingLabelValue() {
cursor - lastSizePosition - Integer.BYTES);
}

public void appendLabel(String key, String value) {
public LabelSerializer appendLabel(String key, String value) {
appendLabelName(key);

startAppendingLabelValue();
appendPartialLabelValue(value);
finishAppendingLabelValue();

return this;
}

public DirectBuffer getBuffer() {
Expand Down
25 changes: 0 additions & 25 deletions core/src/main/java/pl/tkowalcz/tjahzi/LabelSerializers.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package pl.tkowalcz.tjahzi;

import java.util.Map;

public class LabelSerializers {

private static final ThreadLocal<LabelSerializer> THREAD_LOCAL = ThreadLocal.withInitial(LabelSerializer::new);
Expand All @@ -12,27 +10,4 @@ public static LabelSerializer threadLocal() {

return result;
}

// @VisibleForTests
public static LabelSerializer from(Map<String, String> labels, String... moreLabels) {
LabelSerializer labelSerializer = threadLocal();

labels.forEach(labelSerializer::appendLabel);
for (int i = 0; i < moreLabels.length; i += 2) {
labelSerializer.appendLabel(moreLabels[i], moreLabels[i + 1]);
}

return labelSerializer;
}

// @VisibleForTests
public static LabelSerializer from(String... labels) {
LabelSerializer labelSerializer = threadLocal();

for (int i = 0; i < labels.length; i += 2) {
labelSerializer.appendLabel(labels[i], labels[i + 1]);
}

return labelSerializer;
}
}
2 changes: 1 addition & 1 deletion core/src/test/java/pl/tkowalcz/tjahzi/HeadersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void setUp() {
initializer = new TjahziInitializer();
monitoringModule = new StandardMonitoringModule();

labelSerializer = LabelSerializers.from("level", "warn");
labelSerializer = LabelSerializerCreator.from("level", "warn");
}

@AfterEach
Expand Down
27 changes: 27 additions & 0 deletions core/src/test/java/pl/tkowalcz/tjahzi/LabelSerializerCreator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package pl.tkowalcz.tjahzi;

import java.util.Map;

class LabelSerializerCreator {

public static LabelSerializer from(Map<String, String> labels, String... moreLabels) {
LabelSerializer labelSerializer = LabelSerializers.threadLocal();

labels.forEach(labelSerializer::appendLabel);
for (int i = 0; i < moreLabels.length; i += 2) {
labelSerializer.appendLabel(moreLabels[i], moreLabels[i + 1]);
}

return labelSerializer;
}

public static LabelSerializer from(String... labels) {
LabelSerializer labelSerializer = LabelSerializers.threadLocal();

for (int i = 0; i < labels.length; i += 2) {
labelSerializer.appendLabel(labels[i], labels[i + 1]);
}

return labelSerializer;
}
}
8 changes: 4 additions & 4 deletions core/src/test/java/pl/tkowalcz/tjahzi/LogBufferAgentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void shouldSendDataIfOverSizeLimit() throws IOException {
for (int i = 0; i < 100; i++) {
logger.log(
42L,
LabelSerializers.from(Map.of()),
LabelSerializerCreator.from(Map.of()),
ByteBuffer.wrap((
"Cupcake ipsum dolor sit amet cake wafer. " +
"Soufflé jelly beans biscuit topping. " +
Expand Down Expand Up @@ -95,7 +95,7 @@ void shouldNotSendDataBelowSizeLimit() throws IOException {

logger.log(
42L,
LabelSerializers.from(Map.of()),
LabelSerializerCreator.from(Map.of()),
ByteBuffer.wrap("Test".getBytes())
);

Expand Down Expand Up @@ -164,15 +164,15 @@ void shouldSendDataBelowSizeLimitIfTimeoutExpires() throws IOException {
// When
logger.log(
42L,
LabelSerializers.from(Map.of()),
LabelSerializerCreator.from(Map.of()),
ByteBuffer.wrap("Test".getBytes())
);
clock.tick(wait5s + 1);
agent.doWork();

logger.log(
42L,
LabelSerializers.from(Map.of()),
LabelSerializerCreator.from(Map.of()),
ByteBuffer.wrap("Test".getBytes())
);
clock.tick(wait5s + 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ void shouldSerializeMessage(
UnsafeBuffer buffer = new UnsafeBuffer(ByteBuffer.wrap(new byte[expectedSize]));
LogBufferSerializer serializer = new LogBufferSerializer(buffer);

LabelSerializer labelSerializer = LabelSerializers.from(labels);
LabelSerializer labelSerializer = LabelSerializerCreator.from(labels);
if (logLevelLabel != null) {
labelSerializer.appendLabel(logLevelLabel, logLevel);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void shouldCalculateSizeOfMessage(
UnsafeBuffer buffer = new UnsafeBuffer(new byte[expectedSize]);
LogBufferSerializer serializer = new LogBufferSerializer(buffer);

LabelSerializer labelSerializer = LabelSerializers.from(labels);
LabelSerializer labelSerializer = LabelSerializerCreator.from(labels);
if (logLevelLabel != null) {
labelSerializer.appendLabel(logLevelLabel, logLevel);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void shouldDeserializeMessageAndAddStaticLabels(
);
LogBufferSerializer serializer = new LogBufferSerializer(buffer);

LabelSerializer labelSerializer = LabelSerializers.from(labels);
LabelSerializer labelSerializer = LabelSerializerCreator.from(labels);
if (logLevelLabel != null) {
labelSerializer.appendLabel(logLevelLabel, logLevel);
}
Expand Down Expand Up @@ -115,7 +115,7 @@ void shouldOverrideStaticLabelsWithIncoming() throws InvalidProtocolBufferExcept
UnsafeBuffer buffer = new UnsafeBuffer(ByteBuffer.wrap(new byte[256]));
LogBufferSerializer serializer = new LogBufferSerializer(buffer);

LabelSerializer labelSerializer = LabelSerializers.from(incomingLabels);
LabelSerializer labelSerializer = LabelSerializerCreator.from(incomingLabels);
labelSerializer.appendLabelName("log_level");
labelSerializer.appendLabelName("WARN");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ void sendData() {
long timestamp = System.currentTimeMillis();
TjahziLogger logger = loggingSystem.createLogger();

LabelSerializer labelSerializer = LabelSerializers.from("level", "warn");
LabelSerializer labelSerializer = LabelSerializerCreator.from("level", "warn");

// When
logger.log(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ void sendData() {
for (int i = 0; i < 1000; i++) {
logger.log(
timestamp + i,
LabelSerializers.from(
LabelSerializerCreator.from(
Map.of("level", "warn")
),
ByteBuffer.wrap(("Test" + i).getBytes())
Expand Down
2 changes: 1 addition & 1 deletion core/src/test/java/pl/tkowalcz/tjahzi/ReconnectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void setUp() {
initializer = new TjahziInitializer();
monitoringModule = new StandardMonitoringModule();

labelSerializer = LabelSerializers.from("level", "warn");
labelSerializer = LabelSerializerCreator.from("level", "warn");
}

@AfterEach
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ void shouldWorkWithNoDynamicLabels() {
logEvent.setTimeMillis(timestamp);
logEvent.setContextData(new JdkMapAdapterStringMap(Map.of()));

LabelSerializer labelSerializer = LabelSerializers.from(logLevelLabel, "INFO");
LabelSerializer labelSerializer = LabelSerializers.threadLocal()
.appendLabel(logLevelLabel, "INFO");

// When
logic.accept(logEvent, ByteBuffer.allocate(1024));
Expand Down Expand Up @@ -98,7 +99,8 @@ void shouldPerformSubstitutionOnDynamicLabels() {
logEvent.setTimeMillis(timestamp);
logEvent.setContextData(new JdkMapAdapterStringMap(contextMap));

LabelSerializer labelSerializer = LabelSerializers.from(logLevelLabel, "INFO");
LabelSerializer labelSerializer = LabelSerializers.threadLocal()
.appendLabel(logLevelLabel, "INFO");

// When
logic.accept(logEvent, ByteBuffer.allocate(1024));
Expand Down

0 comments on commit a2b5625

Please sign in to comment.