Skip to content
This repository has been archived by the owner on May 23, 2023. It is now read-only.

Commit

Permalink
Remove setInjectionBufferLength() for Binary.
Browse files Browse the repository at this point in the history
Simply pass the length to injectionBuffer() directly,
and refactor BinaryAdapters to wrap a ByteBuffer
for injection (instead of creating a new one).
  • Loading branch information
carlosalberto committed Jun 22, 2018
1 parent fd46b6b commit 31e458b
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,37 +33,29 @@
*/
public interface Binary {
/**
* Hints the buffer length required for SpanContext injection.
* The user may use this to allocate the ByteBuffer
* or resize an existing one. This method is always called
* before injectionBuffer().
* Gets the buffer used to store data as part of {@link SpanContext} injection.
*
* The lenght parameter hints the buffer length required for
* {@link SpanContext} injection. The user may use this to allocate a new
* ByteBuffer or resize an existing one.
*
* It is an error to call this method when Binary is used
* for SpanContext extraction. It is also an error to call
* this method more than one time.
* for {@link SpanContext} extraction.
*
* @param length The buffer length required for SpanContext injection.
* @param length The buffer length required for {@link SpanContext} injection.
* It needs to be larger than zero.
*/
void setInjectionBufferLength(int length);

/**
* Gets the buffer containing the data used for SpanContext injection.
*
* It is an error to call this method when Binary is used
* for SpanContext extraction.
*
* @return The buffer used for SpanContext injection.
* @return The buffer used for {@link SpanContext} injection.
*/
ByteBuffer injectionBuffer();
ByteBuffer injectionBuffer(int lenght);

/**
* Gets the buffer containing the data used for SpanContext extraction.
* Gets the buffer containing the data used for {@link SpanContext} extraction.
*
* It is an error to call this method when Binary is used
* for SpanContext injection.
* for {@link SpanContext} injection.
*
* @return The buffer used for SpanContext extraction.
* @return The buffer used for {@link SpanContext} extraction.
*/
ByteBuffer extractionBuffer();
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ private BinaryAdapters() {}
*
* @param buffer The ByteBuffer used as input.
*
* @return The new Binary carrier used for extraction.
* @return The new {@link Binary} carrier used for extraction.
*/
public static Binary extractionCarrier(ByteBuffer buffer) {
if (buffer == null) {
Expand All @@ -36,15 +36,18 @@ public static Binary extractionCarrier(ByteBuffer buffer) {
}

/**
* Creates an outbound Binary instance used for injection,
* allocating a new ByteBuffer instance when
* setInjectBufferLength() is called. The ByteBuffer can
* be later retrieved using injectBuffer().
* Creates an outbound {@link Binary} instance used for injection with the
* specified ByteBuffer as output. ByteBuffer.limit() will be set to the value
* of the requested length at {@link Binary#injectionBuffer()} time, and
* AssertionError will be thrown if the requested length is larger than
* the remaining length of ByteBuffer.
*
* @param buffer The ByteBuffer used as input.
*
* @return The new Binary carrier used for injection.
*/
public static Binary injectionCarrier() {
return new BinaryInjectAdapter();
public static Binary injectionCarrier(ByteBuffer buffer) {
return new BinaryInjectAdapter(buffer);
}

static class BinaryExtractAdapter implements Binary {
Expand All @@ -55,12 +58,7 @@ public BinaryExtractAdapter(ByteBuffer buffer) {
}

@Override
public void setInjectionBufferLength(int length) {
throw new UnsupportedOperationException();
}

@Override
public ByteBuffer injectionBuffer() {
public ByteBuffer injectionBuffer(int length) {
throw new UnsupportedOperationException();
}

Expand All @@ -73,24 +71,20 @@ public ByteBuffer extractionBuffer() {
static class BinaryInjectAdapter implements Binary {
ByteBuffer buffer;

public BinaryInjectAdapter(ByteBuffer buffer) {
this.buffer = buffer;
}

@Override
public void setInjectionBufferLength(int length) {
public ByteBuffer injectionBuffer(int length) {
if (length < 1) {
throw new IllegalArgumentException("length needs to be larger than 0");
}
if (buffer != null) {
throw new IllegalStateException("injectBuffer() length has already been set.");
}

buffer = ByteBuffer.allocate(length);
}

@Override
public ByteBuffer injectionBuffer() {
if (buffer == null) {
throw new IllegalStateException("setInjectBufferLength() needs to be called before injectBuffer()");
if (length > buffer.remaining()) {
throw new AssertionError("length is larger than the backing ByteBuffer remaining length");
}

buffer.limit(buffer.position() + length);
return buffer;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,33 +37,32 @@ public void testExtractBinaryNull() {
@Test(expected = UnsupportedOperationException.class)
public void testExtractBinaryInjectBuffer() {
Binary binary = BinaryAdapters.extractionCarrier(ByteBuffer.allocate(1));
binary.injectionBuffer();
binary.injectionBuffer(1);
}

@Test
public void testInjectBinary() {
Binary binary = BinaryAdapters.injectionCarrier();
binary.setInjectionBufferLength(1);
assertNotNull(binary.injectionBuffer());
assertEquals(0, binary.injectionBuffer().position());
assertEquals(1, binary.injectionBuffer().capacity());
ByteBuffer buffer = ByteBuffer.allocate(1);
Binary binary = BinaryAdapters.injectionCarrier(buffer);
assertEquals(buffer, binary.injectionBuffer(1));
assertEquals(0, buffer.position());
}

@Test(expected = IllegalArgumentException.class)
public void testInjectBinaryInvalidLength() {
Binary binary = BinaryAdapters.injectionCarrier();
binary.setInjectionBufferLength(0);
Binary binary = BinaryAdapters.injectionCarrier(ByteBuffer.allocate(1));
binary.injectionBuffer(0);
}

@Test(expected = IllegalStateException.class)
public void testInjectBinaryNoLength() {
Binary binary = BinaryAdapters.injectionCarrier();
binary.injectionBuffer();
@Test(expected = AssertionError.class)
public void testInjectBinaryLargerLength() {
Binary binary = BinaryAdapters.injectionCarrier(ByteBuffer.allocate(1));
binary.injectionBuffer(2);
}

@Test(expected = UnsupportedOperationException.class)
public void testInjectBinaryExtractBuffer() {
Binary binary = BinaryAdapters.injectionCarrier();
Binary binary = BinaryAdapters.injectionCarrier(ByteBuffer.allocate(1));
binary.extractionBuffer();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,7 @@ public <C> void inject(MockSpan.MockContext ctx, Format<C> format, C carrier) {
objStream.flush(); // *need* to flush ObjectOutputStream.

byte[] buff = stream.toByteArray();
binary.setInjectionBufferLength(buff.length);
binary.injectionBuffer().put(buff);
binary.injectionBuffer(buff.length).put(buff);

} catch (IOException e) {
throw new RuntimeException("Corrupted state", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,10 @@ public void testBinaryPropagator() {
parentSpan.setBaggageItem("foobag", "fooitem");
parentSpan.finish();

Binary binary = BinaryAdapters.injectionCarrier();
ByteBuffer buffer = ByteBuffer.allocate(128);
Binary binary = BinaryAdapters.injectionCarrier(buffer);
tracer.inject(parentSpan.context(), Format.Builtin.BINARY, binary);

ByteBuffer buffer = binary.injectionBuffer();
buffer.rewind();
SpanContext extract = tracer.extract(Format.Builtin.BINARY, BinaryAdapters.extractionCarrier(buffer));

Expand Down

0 comments on commit 31e458b

Please sign in to comment.