use of com.alipay.sofa.jraft.rhea.storage.KVStoreClosure in project sofa-jraft by sofastack.
the class RocksKVStoreTest method putTest.
/**
* Test method: {@link RocksRawKVStore#put(byte[], byte[], KVStoreClosure)}
*/
@Test
public void putTest() {
final byte[] key = makeKey("put_test");
TestClosure closure = new TestClosure();
this.kvStore.get(key, closure);
byte[] value = (byte[]) closure.getData();
assertNull(value);
value = makeValue("put_test_value");
this.kvStore.put(key, value, null);
byte[] newValue = new SyncKVStore<byte[]>() {
@Override
public void execute(RawKVStore kvStore, KVStoreClosure closure) {
kvStore.get(key, closure);
}
}.apply(this.kvStore);
assertArrayEquals(value, newValue);
}
use of com.alipay.sofa.jraft.rhea.storage.KVStoreClosure in project sofa-jraft by sofastack.
the class RocksKVStoreTest method getAndPutTest.
/**
* Test method: {@link RocksRawKVStore#getAndPut(byte[], byte[], KVStoreClosure)}
*/
@Test
public void getAndPutTest() {
final byte[] key = makeKey("put_test");
TestClosure closure = new TestClosure();
this.kvStore.get(key, closure);
byte[] value = (byte[]) closure.getData();
assertNull(value);
value = makeValue("put_test_value");
KVStoreClosure kvStoreClosure = new BaseKVStoreClosure() {
@Override
public void run(Status status) {
assertEquals(status, Status.OK());
}
};
this.kvStore.getAndPut(key, value, kvStoreClosure);
assertNull(kvStoreClosure.getData());
byte[] newVal = makeValue("put_test_value_new");
this.kvStore.getAndPut(key, newVal, kvStoreClosure);
assertArrayEquals(value, (byte[]) kvStoreClosure.getData());
}
use of com.alipay.sofa.jraft.rhea.storage.KVStoreClosure in project sofa-jraft by sofastack.
the class MemoryKVStoreTest method putTest.
/**
* Test method: {@link MemoryRawKVStore#put(byte[], byte[], KVStoreClosure)}
*/
@Test
public void putTest() {
final byte[] key = makeKey("put_test");
TestClosure closure = new TestClosure();
this.kvStore.get(key, closure);
byte[] value = (byte[]) closure.getData();
assertNull(value);
value = makeValue("put_test_value");
this.kvStore.put(key, value, null);
byte[] newValue = new SyncKVStore<byte[]>() {
@Override
public void execute(RawKVStore kvStore, KVStoreClosure closure) {
kvStore.get(key, closure);
}
}.apply(this.kvStore);
assertArrayEquals(value, newValue);
}
use of com.alipay.sofa.jraft.rhea.storage.KVStoreClosure in project sofa-jraft by sofastack.
the class MemoryKVStoreTest method getSequenceTest.
/**
* Test method: {@link MemoryRawKVStore#getSequence(byte[], int, KVStoreClosure)}
*/
@Test
public void getSequenceTest() throws InterruptedException {
final byte[] seqKey = makeKey("seq_test");
Sequence sequence = new SyncKVStore<Sequence>() {
@Override
public void execute(RawKVStore kvStore, KVStoreClosure closure) {
kvStore.getSequence(seqKey, 199, closure);
}
}.apply(this.kvStore);
assertEquals(sequence.getStartValue(), 0);
assertEquals(sequence.getEndValue(), 199);
Sequence sequence2 = new SyncKVStore<Sequence>() {
@Override
public void execute(RawKVStore kvStore, KVStoreClosure closure) {
kvStore.getSequence(seqKey, 10, closure);
}
}.apply(this.kvStore);
assertEquals(sequence2.getStartValue(), 199);
assertEquals(sequence2.getEndValue(), 209);
this.kvStore.resetSequence(seqKey, null);
Sequence sequence3 = new SyncKVStore<Sequence>() {
@Override
public void execute(RawKVStore kvStore, KVStoreClosure closure) {
kvStore.getSequence(seqKey, 11, closure);
}
}.apply(this.kvStore);
assertEquals(sequence3.getStartValue(), 0);
assertEquals(sequence3.getEndValue(), 11);
// read-only
Sequence sequence4 = new SyncKVStore<Sequence>() {
@Override
public void execute(RawKVStore kvStore, KVStoreClosure closure) {
kvStore.getSequence(seqKey, 0, closure);
}
}.apply(this.kvStore);
assertEquals(sequence4.getStartValue(), 11);
assertEquals(sequence4.getEndValue(), 11);
KVStoreClosure assertFailed = new BaseKVStoreClosure() {
@Override
public void run(Status status) {
assertEquals("Fail to [GET_SEQUENCE], step must >= 0", status.getErrorMsg());
}
};
this.kvStore.getSequence(seqKey, -1, assertFailed);
}
use of com.alipay.sofa.jraft.rhea.storage.KVStoreClosure in project sofa-jraft by sofastack.
the class MemoryKVStoreTest method putListTest.
/**
* Test method: {@link MemoryRawKVStore#put(List, KVStoreClosure)}
*/
@Test
public void putListTest() {
final List<KVEntry> entries = Lists.newArrayList();
for (int i = 0; i < 10; i++) {
entries.add(new KVEntry(makeKey("batch_put_test_key" + i), makeValue("batch_put_test_value" + i)));
}
this.kvStore.put(entries, null);
final List<KVEntry> entries2 = new SyncKVStore<List<KVEntry>>() {
@Override
public void execute(RawKVStore kvStore, KVStoreClosure closure) {
kvStore.scan(makeKey("batch_put_test_key"), makeKey("batch_put_test_key" + 99), closure);
}
}.apply(this.kvStore);
assertEquals(entries.size(), entries2.size());
for (int i = 0; i < entries.size(); i++) {
assertArrayEquals(entries.get(i).getKey(), entries2.get(i).getKey());
assertArrayEquals(entries.get(i).getValue(), entries2.get(i).getValue());
}
}
Aggregations