-
Notifications
You must be signed in to change notification settings - Fork 29
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
Showing
4 changed files
with
103 additions
and
86 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
21 changes: 21 additions & 0 deletions
21
testkit/src/main/java/com/github/loki4j/testkit/dummy/FailingStringWriter.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,21 @@ | ||
package com.github.loki4j.testkit.dummy; | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
import com.github.loki4j.client.batch.LogRecordBatch; | ||
import com.github.loki4j.client.util.ByteBufferFactory; | ||
|
||
public class FailingStringWriter extends StringWriter { | ||
public AtomicBoolean fail = new AtomicBoolean(false); | ||
|
||
public FailingStringWriter(int capacity, ByteBufferFactory bufferFactory) { | ||
super(capacity, bufferFactory); | ||
} | ||
|
||
@Override | ||
public void serializeBatch(LogRecordBatch batch) { | ||
if (fail.get()) | ||
throw new RuntimeException("Text exception"); | ||
super.serializeBatch(batch); | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
testkit/src/main/java/com/github/loki4j/testkit/dummy/StringWriter.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,80 @@ | ||
package com.github.loki4j.testkit.dummy; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
import java.util.Arrays; | ||
|
||
import com.github.loki4j.client.batch.LogRecordBatch; | ||
import com.github.loki4j.client.util.ByteBufferFactory; | ||
import com.github.loki4j.client.writer.Writer; | ||
|
||
public class StringWriter implements Writer { | ||
private final ByteBufferFactory bf; | ||
private ByteBuffer b; | ||
private int size = 0; | ||
|
||
public StringWriter(int capacity, ByteBufferFactory bufferFactory) { | ||
bf = bufferFactory; | ||
b = bf.allocate(capacity); | ||
} | ||
|
||
public static String batchToString(LogRecordBatch batch) { | ||
var s = new StringBuilder(); | ||
for (int i = 0; i < batch.size(); i++) { | ||
var r = batch.get(i); | ||
s | ||
.append(Arrays.toString(r.stream.labels)) | ||
.append(StringPayload.LABELS_MESSAGE_SEPARATOR) | ||
.append(Arrays.toString(r.metadata)) | ||
.append(StringPayload.LABELS_MESSAGE_SEPARATOR) | ||
.append("ts=") | ||
.append(r.timestampMs) | ||
.append(" ") | ||
.append(r.message) | ||
.append('\n'); | ||
} | ||
return s.toString(); | ||
} | ||
|
||
@Override | ||
public void serializeBatch(LogRecordBatch batch) { | ||
b.clear(); | ||
var str = batchToString(batch); | ||
var data = str.getBytes(StandardCharsets.UTF_8); | ||
if (b.capacity() < data.length) | ||
b = bf.allocate(data.length); | ||
b.put(data); | ||
b.flip(); | ||
size = data.length; | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return size; | ||
} | ||
|
||
@Override | ||
public void toByteBuffer(ByteBuffer buffer) { | ||
buffer.put(b); | ||
buffer.flip(); | ||
} | ||
|
||
@Override | ||
public byte[] toByteArray() { | ||
byte[] r = new byte[b.remaining()]; | ||
b.get(r); | ||
return r; | ||
} | ||
|
||
@Override | ||
public void reset() { | ||
size = 0; | ||
b.clear(); | ||
} | ||
|
||
@Override | ||
public boolean isBinary() { | ||
return false; | ||
} | ||
} |