Search in sources :

Example 71 with Timeout

use of org.junit.jupiter.api.Timeout in project zookeeper by apache.

the class ReadOnlyModeTest method testSeekForRwServer.

/**
 * Ensures that client seeks for r/w servers while it's connected to r/o
 * server.
 */
@SuppressWarnings("deprecation")
@Test
@Timeout(value = 90)
public void testSeekForRwServer() throws Exception {
    qu.enableLocalSession(true);
    qu.startQuorum();
    try (LoggerTestTool loggerTestTool = new LoggerTestTool("org.apache.zookeeper")) {
        ByteArrayOutputStream os = loggerTestTool.getOutputStream();
        qu.shutdown(2);
        CountdownWatcher watcher = new CountdownWatcher();
        ZooKeeper zk = new ZooKeeper(qu.getConnString(), CONNECTION_TIMEOUT, watcher, true);
        watcher.waitForConnected(CONNECTION_TIMEOUT);
        // if we don't suspend a peer it will rejoin a quorum
        qu.getPeer(1).peer.suspend();
        // start two servers to form a quorum; client should detect this and
        // connect to one of them
        watcher.reset();
        qu.start(2);
        qu.start(3);
        ClientBase.waitForServerUp(qu.getConnString(), 2000);
        watcher.waitForConnected(CONNECTION_TIMEOUT);
        zk.create("/test", "test".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        // resume poor fellow
        qu.getPeer(1).peer.resume();
        String log = os.toString();
        assertFalse(StringUtils.isEmpty(log), "OutputStream doesn't have any log messages");
        LineNumberReader r = new LineNumberReader(new StringReader(log));
        String line;
        Pattern p = Pattern.compile(".*Majority server found.*");
        boolean found = false;
        while ((line = r.readLine()) != null) {
            if (p.matcher(line).matches()) {
                found = true;
                break;
            }
        }
        assertTrue(found, "Majority server wasn't found while connected to r/o server");
    }
}
Also used : Pattern(java.util.regex.Pattern) ZooKeeper(org.apache.zookeeper.ZooKeeper) CountdownWatcher(org.apache.zookeeper.test.ClientBase.CountdownWatcher) StringReader(java.io.StringReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) LineNumberReader(java.io.LineNumberReader) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 72 with Timeout

use of org.junit.jupiter.api.Timeout in project zookeeper by apache.

the class ReconfigExceptionTest method testReconfigEnabledWithAuthAndWrongACL.

@Test
@Timeout(value = 10)
public void testReconfigEnabledWithAuthAndWrongACL() throws InterruptedException {
    resetZKAdmin();
    try {
        zkAdmin.addAuthInfo("digest", "super:test".getBytes());
        // There is ACL however the permission is wrong - need WRITE permission at leaste.
        ArrayList<ACL> acls = new ArrayList<ACL>(Collections.singletonList(new ACL(ZooDefs.Perms.READ, new Id("digest", "user:tl+z3z0vO6PfPfEENfLF96E6pM0="))));
        zkAdmin.setACL(ZooDefs.CONFIG_NODE, acls, -1);
        resetZKAdmin();
        zkAdmin.addAuthInfo("digest", "user:test".getBytes());
        reconfigPort();
        fail("Reconfig should fail with an ACL that is read only!");
    } catch (KeeperException e) {
        assertTrue(e.code() == KeeperException.Code.NOAUTH);
    }
}
Also used : ArrayList(java.util.ArrayList) ACL(org.apache.zookeeper.data.ACL) Id(org.apache.zookeeper.data.Id) KeeperException(org.apache.zookeeper.KeeperException) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 73 with Timeout

use of org.junit.jupiter.api.Timeout in project zookeeper by apache.

the class RemoveWatchesTest method testRemoveAllChildWatchesOnAPath.

/**
 * Test verifies WatcherType.Children - removes only the configured child
 * watcher function
 */
@ParameterizedTest
@ValueSource(booleans = { true, false })
@Timeout(value = 90)
public void testRemoveAllChildWatchesOnAPath(boolean useAsync) throws Exception {
    zk1.create("/node1", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    final CountDownLatch cWatchCount = new CountDownLatch(2);
    final CountDownLatch rmWatchCount = new CountDownLatch(2);
    Watcher w1 = event -> {
        switch(event.getType()) {
            case ChildWatchRemoved:
                rmWatchCount.countDown();
                break;
            case NodeChildrenChanged:
                cWatchCount.countDown();
                break;
            default:
                break;
        }
    };
    Watcher w2 = event -> {
        switch(event.getType()) {
            case ChildWatchRemoved:
                rmWatchCount.countDown();
                break;
            case NodeChildrenChanged:
                cWatchCount.countDown();
                break;
            default:
                break;
        }
    };
    // Add multiple child watches
    LOG.info("Adding child watcher {} on path {}", w1, "/node1");
    assertEquals(0, zk2.getChildren("/node1", w1).size(), "Didn't set child watches");
    LOG.info("Adding child watcher {} on path {}", w2, "/node1");
    assertEquals(0, zk2.getChildren("/node1", w2).size(), "Didn't set child watches");
    assertTrue(isServerSessionWatcher(zk2.getSessionId(), "/node1", WatcherType.Children), "Server session is not a watcher");
    removeAllWatches(zk2, "/node1", WatcherType.Children, false, Code.OK, useAsync);
    assertTrue(rmWatchCount.await(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS), "Didn't remove child watcher");
    assertFalse(isServerSessionWatcher(zk2.getSessionId(), "/node1", WatcherType.Children), "Server session is still a watcher after removal");
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) Assertions.fail(org.junit.jupiter.api.Assertions.fail) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) BeforeEach(org.junit.jupiter.api.BeforeEach) Ids(org.apache.zookeeper.ZooDefs.Ids) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) Mockito.spy(org.mockito.Mockito.spy) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) Map(java.util.Map) WatcherType(org.apache.zookeeper.Watcher.WatcherType) ServerCnxn(org.apache.zookeeper.server.ServerCnxn) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Mockito.doReturn(org.mockito.Mockito.doReturn) ValueSource(org.junit.jupiter.params.provider.ValueSource) Logger(org.slf4j.Logger) Set(java.util.Set) IOException(java.io.IOException) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) AfterEach(org.junit.jupiter.api.AfterEach) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) EventType(org.apache.zookeeper.Watcher.Event.EventType) ClientBase(org.apache.zookeeper.test.ClientBase) Timeout(org.junit.jupiter.api.Timeout) Code(org.apache.zookeeper.KeeperException.Code) CountDownLatch(java.util.concurrent.CountDownLatch) ValueSource(org.junit.jupiter.params.provider.ValueSource) Timeout(org.junit.jupiter.api.Timeout) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 74 with Timeout

use of org.junit.jupiter.api.Timeout in project zookeeper by apache.

the class X509UtilTest method testCreateSSLContextWithCustomProtocol.

@ParameterizedTest
@MethodSource("data")
@Timeout(value = 5)
public void testCreateSSLContextWithCustomProtocol(X509KeyType caKeyType, X509KeyType certKeyType, String keyPassword, Integer paramIndex) throws Exception {
    final String protocol = "TLSv1.1";
    init(caKeyType, certKeyType, keyPassword, paramIndex);
    System.setProperty(x509Util.getSslProtocolProperty(), protocol);
    SSLContext sslContext = x509Util.getDefaultSSLContext();
    assertEquals(protocol, sslContext.getProtocol());
}
Also used : SSLContext(javax.net.ssl.SSLContext) Timeout(org.junit.jupiter.api.Timeout) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 75 with Timeout

use of org.junit.jupiter.api.Timeout in project zookeeper by apache.

the class X509UtilTest method testCreateSSLServerSocketWithoutPort.

@ParameterizedTest
@MethodSource("data")
@Timeout(value = 5)
public void testCreateSSLServerSocketWithoutPort(X509KeyType caKeyType, X509KeyType certKeyType, String keyPassword, Integer paramIndex) throws Exception {
    init(caKeyType, certKeyType, keyPassword, paramIndex);
    setCustomCipherSuites();
    SSLServerSocket sslServerSocket = x509Util.createSSLServerSocket();
    assertArrayEquals(customCipherSuites, sslServerSocket.getEnabledCipherSuites());
    assertTrue(sslServerSocket.getNeedClientAuth());
}
Also used : SSLServerSocket(javax.net.ssl.SSLServerSocket) Timeout(org.junit.jupiter.api.Timeout) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

Timeout (org.junit.jupiter.api.Timeout)291 Test (org.junit.jupiter.api.Test)235 CountDownLatch (java.util.concurrent.CountDownLatch)71 ZooKeeper (org.apache.zookeeper.ZooKeeper)33 AtomicReference (java.util.concurrent.atomic.AtomicReference)32 ArrayList (java.util.ArrayList)31 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)29 RepeatedTest (org.junit.jupiter.api.RepeatedTest)29 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)29 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)28 LocalChannel (io.netty.channel.local.LocalChannel)27 CountdownWatcher (org.apache.zookeeper.test.ClientBase.CountdownWatcher)26 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)25 IOException (java.io.IOException)25 Bootstrap (io.netty.bootstrap.Bootstrap)24 MethodSource (org.junit.jupiter.params.provider.MethodSource)24 Channel (io.netty.channel.Channel)23 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)21 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)19 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)19