Skip to content

Commit

Permalink
QUEUE-26 cleaned up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Austin committed Jan 18, 2016
1 parent 2031b0a commit e9c8bc9
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 23 deletions.
Expand Up @@ -52,14 +52,6 @@ public interface Excerpt extends ExcerptTailer {
*/
boolean moveToIndex(long index) throws IOException;

/**
* Randomly select an Excerpt.
*
* @param cycle cycle
* @param index index to look up
* @return true if this is a valid entries and not padding.
*/
boolean moveToIndex(int cycle, long index) throws IOException;

/**
* Replay from the lower.
Expand Down
Expand Up @@ -36,20 +36,24 @@ public interface ExcerptTailer extends ExcerptCommon {
boolean readDocument(@NotNull ReadMarshallable reader) throws IOException;

/**
* @param marshallable user to read the document
* @param marshallable used to read the document
* @return {@code true} if successful
* @throws IOException if not able to read the chronicle file
*/
boolean readBytes(@NotNull ReadBytesMarshallable marshallable) throws IOException;


/**
* @param using used to read the document
* @return {@code true} if successful
* @throws IOException if not able to read the chronicle file
*/
boolean readBytes(@NotNull Bytes using) throws IOException;


/**
* @return the index just read
*/
long moveToIndex();
long index();


/**
Expand Down
Expand Up @@ -35,7 +35,8 @@
import java.util.function.Consumer;

import static net.openhft.chronicle.bytes.Bytes.elasticByteBuffer;
import static net.openhft.chronicle.queue.ChronicleQueue.*;
import static net.openhft.chronicle.queue.ChronicleQueue.toCycle;
import static net.openhft.chronicle.queue.ChronicleQueue.toSubIndex;

public class Excerpts {

Expand Down Expand Up @@ -291,7 +292,7 @@ private boolean readAtIndex(@NotNull Consumer<Wire> c) throws IOException {

if (success) {

this.index = index(cycle, toSubIndex(index) + 1);
this.index = ChronicleQueue.index(cycle, toSubIndex(index) + 1);
return true;
}
// roll detected, move to next cycle;
Expand Down Expand Up @@ -349,18 +350,17 @@ private boolean readAtSubIndex(@NotNull Consumer<Wire> c) throws IOException {

}


/**
* @return provides an index that includes the cycle number
*/
@Override
public long moveToIndex() {
//TODO: should we raise an exception ?
public long index() {

if (this.store == null) {
throw new IllegalArgumentException("This tailer is not bound to any cycle");
}

return index(this.cycle, this.index);
return ChronicleQueue.index(this.cycle, this.index);
}


Expand All @@ -386,7 +386,7 @@ public boolean moveToIndex(long index) throws IOException {
final long subIndex = toSubIndex(index);
if (subIndex == -1) {
bytes.readPosition(0);
this.index = index(cycle, subIndex);
this.index = ChronicleQueue.index(cycle, subIndex);
return true;
}

Expand All @@ -397,7 +397,7 @@ public boolean moveToIndex(long index) throws IOException {

bytes.readPosition(position);
bytes.readLimit(bytes.capacity());
this.index = index(cycle, subIndex - 1);
this.index = ChronicleQueue.index(cycle, subIndex - 1);
return true;
}

Expand Down Expand Up @@ -448,7 +448,7 @@ private StoreTailer cycle(long cycle) throws IOException {
this.store = this.queue.storeForCycle(cycle, queue.epoch());

wire = queue.wireType().apply(store.mappedBytes());
moveToIndex(index(cycle, -1));
moveToIndex(ChronicleQueue.index(cycle, -1));
if (LOG.isDebugEnabled())
LOG.debug("tailer=" + ((MappedBytes) wire.bytes()).mappedFile().file().getAbsolutePath());

Expand Down
Expand Up @@ -95,15 +95,15 @@ public void testAppendAndRead() throws IOException {
for (int i = 0; i < 10; i++) {
final int n = i;
assertTrue(tailer.readDocument(r -> assertEquals(n, r.read(TestKey.test).int32())));
assertEquals(n, toSubIndex(tailer.moveToIndex()));
assertEquals(n, toSubIndex(tailer.index()));
}

// Random read
for (int i = 0; i < 10; i++) {
final int n = i;
assertTrue(tailer.moveToIndex(index(cycle, n)));
assertTrue(tailer.readDocument(r -> assertEquals(n, r.read(TestKey.test).int32())));
assertEquals(n, toSubIndex(tailer.moveToIndex()));
assertEquals(n, toSubIndex(tailer.index()));
}
}

Expand Down Expand Up @@ -240,7 +240,7 @@ public void testAppendAndReadAtIndex() throws IOException {
final int n = i;
assertTrue(tailer.readDocument(r -> assertEquals(n, toSubIndex(r.read(TestKey.test)
.int32()))));
assertEquals(n, toSubIndex(tailer.moveToIndex()));
assertEquals(n, toSubIndex(tailer.index()));
}
}

Expand Down

0 comments on commit e9c8bc9

Please sign in to comment.