use of org.apache.bookkeeper.statelib.api.exceptions.MVCCStoreException in project bookkeeper by apache.
the class TableStoreUtilsTest method testHandleCause.
@Test
public void testHandleCause() {
StatusCode[] protoCodes = new StatusCode[] { StatusCode.SUCCESS, StatusCode.INTERNAL_SERVER_ERROR, StatusCode.BAD_REQUEST, StatusCode.BAD_REQUEST, StatusCode.UNEXPECTED, StatusCode.BAD_REVISION, StatusCode.BAD_REVISION, StatusCode.KEY_NOT_FOUND, StatusCode.KEY_EXISTS };
Code[] codes = new Code[] { Code.OK, Code.INTERNAL_ERROR, Code.INVALID_ARGUMENT, Code.ILLEGAL_OP, Code.UNEXPECTED, Code.BAD_REVISION, Code.SMALLER_REVISION, Code.KEY_NOT_FOUND, Code.KEY_EXISTS };
for (int i = 0; i < codes.length; i++) {
Code code = codes[i];
MVCCStoreException exception = new MVCCStoreException(code, "test-" + code);
assertEquals(protoCodes[i], handleCause(exception));
}
}
use of org.apache.bookkeeper.statelib.api.exceptions.MVCCStoreException in project bookkeeper by apache.
the class TableStoreUtilsTest method testHandleExecutionException.
@Test
public void testHandleExecutionException() {
MVCCStoreException exception = new MVCCStoreException(Code.BAD_REVISION, "bad-revision");
ExecutionException ee = new ExecutionException(exception);
assertEquals(StatusCode.BAD_REVISION, handleCause(ee));
}
use of org.apache.bookkeeper.statelib.api.exceptions.MVCCStoreException in project bookkeeper by apache.
the class TestMVCCAsyncBytesStoreImpl method testBasicOps.
@Test
public void testBasicOps() throws Exception {
this.streamName = "test-basic-ops";
StateStoreSpec spec = initSpec(streamName);
result(store.init(spec));
// normal put
{
assertNull(result(store.get(getKey(0))));
result(store.put(getKey(0), getValue(0)));
assertArrayEquals(getValue(0), result(store.get(getKey(0))));
}
// putIfAbsent
{
// failure case
assertArrayEquals(getValue(0), result(store.putIfAbsent(getKey(0), getValue(99))));
assertArrayEquals(getValue(0), result(store.get(getKey(0))));
// success case
byte[] key1 = getKey(1);
assertNull(result(store.putIfAbsent(key1, getValue(1))));
assertArrayEquals(getValue(1), result(store.get(key1)));
}
// vPut
{
// key-not-found case
int key = 2;
int initialVal = 2;
int casVal = 99;
try {
result(store.vPut(getKey(key), getValue(initialVal), 100L));
fail("key2 doesn't exist yet");
} catch (MVCCStoreException e) {
assertEquals(Code.KEY_NOT_FOUND, e.getCode());
}
// vPut(k, v, -1L)
try {
result(store.vPut(getKey(key), getValue(initialVal), -1L));
fail("key2 doesn't exist yet");
} catch (MVCCStoreException e) {
assertEquals(Code.KEY_NOT_FOUND, e.getCode());
}
// put(key2, v)
assertNull(result(store.putIfAbsent(getKey(key), getValue(initialVal))));
// vPut(key2, v, 0)
assertEquals(1L, result(store.vPut(getKey(key), getValue(casVal), 0)).longValue());
assertArrayEquals(getValue(casVal), result(store.get(getKey(key))));
}
// rPut
{
// key-not-found case
int key = 3;
int initialVal = 3;
int casVal = 99;
try {
result(store.rPut(getKey(key), getValue(initialVal), 100L));
fail("key2 doesn't exist yet");
} catch (MVCCStoreException e) {
assertEquals(Code.KEY_NOT_FOUND, e.getCode());
}
// vPut(k, v, -1L)
try {
result(store.rPut(getKey(key), getValue(initialVal), -1L));
fail("key2 doesn't exist yet");
} catch (MVCCStoreException e) {
assertEquals(Code.KEY_NOT_FOUND, e.getCode());
}
// put(key2, v)
assertNull(result(store.putIfAbsent(getKey(key), getValue(initialVal))));
KeyValue<byte[], byte[]> kv = result(store.getKeyValue(getKey(key)));
long revision = kv.modifiedRevision();
assertArrayEquals(getValue(initialVal), kv.value());
// vPut(key2, v, 0)
assertEquals(revision + 1, result(store.rPut(getKey(key), getValue(casVal), revision)).longValue());
assertArrayEquals(getValue(casVal), result(store.get(getKey(key))));
}
// delete(k)
{
// key not found
assertNull(result(store.delete(getKey(99))));
// key exists
int key = 0;
assertArrayEquals(getValue(key), result(store.get(getKey(key))));
assertArrayEquals(getValue(key), result(store.delete(getKey(key))));
assertNull(result(store.get(getKey(key))));
}
// delete(k, v)
{
// key not found
assertNull(result(store.delete(getKey(99))));
// key exists
int key = 1;
assertArrayEquals(getValue(key), result(store.get(getKey(key))));
assertFalse(result(store.delete(getKey(key), getValue(99))));
assertArrayEquals(getValue(key), result(store.get(getKey(key))));
assertTrue(result(store.delete(getKey(key), getValue(key))));
assertNull(result(store.get(getKey(key))));
}
// vDelete
{
int key = 2;
@Cleanup KeyValue<byte[], byte[]> kv = result(store.getKeyValue(getKey(key)));
long expectedVersion = kv.version();
try {
result(store.vDelete(getKey(key), expectedVersion + 1));
fail("should fail to delete a key with wrong version");
} catch (MVCCStoreException e) {
assertEquals(Code.BAD_REVISION, e.getCode());
}
// vDelete(k, -1L)
try {
result(store.vDelete(getKey(key), -1L));
fail("Should fail to delete a key with version(-1)");
} catch (MVCCStoreException e) {
assertEquals(Code.BAD_REVISION, e.getCode());
}
// vDelete(key2, version)
@Cleanup KeyValue<byte[], byte[]> deletedKv = (result(store.vDelete(getKey(key), expectedVersion)));
assertNotNull(deletedKv);
assertEquals(kv.createRevision(), deletedKv.createRevision());
assertEquals(kv.modifiedRevision(), deletedKv.modifiedRevision());
assertEquals(kv.version(), deletedKv.version());
assertArrayEquals(kv.value(), deletedKv.value());
assertNull(result(store.get(getKey(key))));
}
// rPut
{
int key = 3;
@Cleanup KeyValue<byte[], byte[]> kv = result(store.getKeyValue(getKey(key)));
long expectedRevision = kv.modifiedRevision();
try {
result(store.rDelete(getKey(key), expectedRevision + 1));
fail("should fail to delete a key with wrong revision");
} catch (MVCCStoreException e) {
assertEquals(Code.BAD_REVISION, e.getCode());
}
// rDelete(k, -1L)
try {
result(store.rDelete(getKey(key), -1L));
fail("Should fail to delete a key with revision(-1)");
} catch (MVCCStoreException e) {
assertEquals(Code.BAD_REVISION, e.getCode());
}
// rDelete(key2, revision)
@Cleanup KeyValue<byte[], byte[]> deletedKv = (result(store.rDelete(getKey(key), expectedRevision)));
assertNotNull(deletedKv);
assertEquals(kv.createRevision(), deletedKv.createRevision());
assertEquals(kv.modifiedRevision(), deletedKv.modifiedRevision());
assertEquals(kv.version(), deletedKv.version());
assertArrayEquals(kv.value(), deletedKv.value());
assertNull(result(store.get(getKey(key))));
}
// increment failure
{
int ki = 3;
byte[] key = getKey(ki);
result(store.put(key, getValue(ki)));
try {
result(store.increment(key, 100L));
fail("Can't increment a non-number key");
} catch (MVCCStoreException e) {
assertEquals(Code.ILLEGAL_OP, e.getCode());
}
}
// increment success
{
int ki = 4;
byte[] key = getKey(ki);
for (int i = 0; i < 5; i++) {
result(store.increment(key, 100L));
@Cleanup KeyValue<byte[], byte[]> kv = result(store.getKeyValue(key));
assertEquals(100L * (i + 1), kv.numberValue());
}
}
}
Aggregations