Search in sources :

Example 1 with InvalidStreamNameException

use of org.apache.distributedlog.exceptions.InvalidStreamNameException in project bookkeeper by apache.

the class TestBKDistributedLogNamespace method testInvalidStreamName.

@Test(timeout = 60000)
public void testInvalidStreamName() throws Exception {
    assertFalse(DLUtils.isReservedStreamName("test"));
    assertTrue(DLUtils.isReservedStreamName(".test"));
    URI uri = createDLMURI("/" + runtime.getMethodName());
    Namespace namespace = NamespaceBuilder.newBuilder().conf(conf).uri(uri).build();
    try {
        namespace.openLog(".test1");
        fail("Should fail to create invalid stream .test");
    } catch (InvalidStreamNameException isne) {
    // expected
    }
    DistributedLogManager dlm = namespace.openLog("test1");
    LogWriter writer = dlm.startLogSegmentNonPartitioned();
    writer.write(DLMTestUtil.getLogRecordInstance(1));
    writer.close();
    dlm.close();
    try {
        namespace.openLog(".test2");
        fail("Should fail to create invalid stream .test2");
    } catch (InvalidStreamNameException isne) {
    // expected
    }
    try {
        namespace.openLog("/ test2");
        fail("should fail to create invalid stream / test2");
    } catch (InvalidStreamNameException isne) {
    // expected
    }
    try {
        char[] chars = new char[6];
        for (int i = 0; i < chars.length; i++) {
            chars[i] = 'a';
        }
        chars[0] = 0;
        String streamName = new String(chars);
        namespace.openLog(streamName);
        fail("should fail to create invalid stream " + streamName);
    } catch (InvalidStreamNameException isne) {
    // expected
    }
    try {
        char[] chars = new char[6];
        for (int i = 0; i < chars.length; i++) {
            chars[i] = 'a';
        }
        chars[3] = '\u0010';
        String streamName = new String(chars);
        namespace.openLog(streamName);
        fail("should fail to create invalid stream " + streamName);
    } catch (InvalidStreamNameException isne) {
    // expected
    }
    DistributedLogManager newDLM = namespace.openLog("test_2-3");
    LogWriter newWriter = newDLM.startLogSegmentNonPartitioned();
    newWriter.write(DLMTestUtil.getLogRecordInstance(1));
    newWriter.close();
    newDLM.close();
    Iterator<String> streamIter = namespace.getLogs();
    Set<String> streamSet = Sets.newHashSet(streamIter);
    assertEquals(2, streamSet.size());
    assertTrue(streamSet.contains("test1"));
    assertTrue(streamSet.contains("test_2-3"));
    namespace.close();
}
Also used : InvalidStreamNameException(org.apache.distributedlog.exceptions.InvalidStreamNameException) LogWriter(org.apache.distributedlog.api.LogWriter) DistributedLogManager(org.apache.distributedlog.api.DistributedLogManager) URI(java.net.URI) Namespace(org.apache.distributedlog.api.namespace.Namespace) Test(org.junit.Test)

Example 2 with InvalidStreamNameException

use of org.apache.distributedlog.exceptions.InvalidStreamNameException in project bookkeeper by apache.

the class TestBKDistributedLogManager method testInvalidStreamFromInvalidZkPath.

@Test(timeout = 60000)
public void testInvalidStreamFromInvalidZkPath() throws Exception {
    String baseName = testNames.getMethodName();
    String streamName = "\0blah";
    URI uri = createDLMURI("/" + baseName);
    Namespace namespace = NamespaceBuilder.newBuilder().conf(conf).uri(uri).build();
    DistributedLogManager dlm = null;
    AsyncLogWriter writer = null;
    try {
        dlm = namespace.openLog(streamName);
        writer = dlm.startAsyncLogSegmentNonPartitioned();
        fail("should have thrown");
    } catch (InvalidStreamNameException e) {
    } finally {
        if (null != writer) {
            Utils.close(writer);
        }
        if (null != dlm) {
            dlm.close();
        }
        namespace.close();
    }
}
Also used : InvalidStreamNameException(org.apache.distributedlog.exceptions.InvalidStreamNameException) DistributedLogManager(org.apache.distributedlog.api.DistributedLogManager) AsyncLogWriter(org.apache.distributedlog.api.AsyncLogWriter) URI(java.net.URI) Namespace(org.apache.distributedlog.api.namespace.Namespace) Test(org.junit.Test)

Aggregations

URI (java.net.URI)2 DistributedLogManager (org.apache.distributedlog.api.DistributedLogManager)2 Namespace (org.apache.distributedlog.api.namespace.Namespace)2 InvalidStreamNameException (org.apache.distributedlog.exceptions.InvalidStreamNameException)2 Test (org.junit.Test)2 AsyncLogWriter (org.apache.distributedlog.api.AsyncLogWriter)1 LogWriter (org.apache.distributedlog.api.LogWriter)1