use of org.junit.jupiter.api.Timeout in project zookeeper by apache.
the class NIOServerCnxnTest method testOperationsAfterCnxnClose.
/**
* Test operations on ServerCnxn after socket closure.
*/
@Test
@Timeout(value = 60)
public void testOperationsAfterCnxnClose() throws IOException, InterruptedException, KeeperException {
final ZooKeeper zk = createClient();
final String path = "/a";
try {
// make sure zkclient works
zk.create(path, "test".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
assertNotNull(zk.exists(path, false), "Didn't create znode:" + path);
// Defaults ServerCnxnFactory would be instantiated with
// NIOServerCnxnFactory
assertTrue(serverFactory instanceof NIOServerCnxnFactory, "Didn't instantiate ServerCnxnFactory with NIOServerCnxnFactory!");
Iterable<ServerCnxn> connections = serverFactory.getConnections();
for (ServerCnxn serverCnxn : connections) {
serverCnxn.close(ServerCnxn.DisconnectReason.CHANNEL_CLOSED_EXCEPTION);
try {
serverCnxn.toString();
} catch (Exception e) {
LOG.error("Exception while getting connection details!", e);
fail("Shouldn't throw exception while " + "getting connection details!");
}
}
} finally {
zk.close();
}
}
use of org.junit.jupiter.api.Timeout in project zookeeper by apache.
the class NettyServerCnxnTest method testMaxConnectionPerIpSurpased.
/**
* In the {@link #setUp()} routine, the maximum number of connections per IP
* is set to 1. This tests that if more than one connection is attempted, the
* connection fails.
*/
@Test
@Timeout(value = 40)
public void testMaxConnectionPerIpSurpased() {
assertTrue(serverFactory instanceof NettyServerCnxnFactory, "Did not instantiate ServerCnxnFactory with NettyServerCnxnFactory!");
assertThrows(ProtocolException.class, () -> {
try (final ZooKeeper zk1 = createClient();
final ZooKeeper zk2 = createClient()) {
}
});
}
use of org.junit.jupiter.api.Timeout in project zookeeper by apache.
the class DataTreeTest method testSerializeDoesntLockDataNodeWhileWriting.
/*
* ZOOKEEPER-2201 - OutputArchive.writeRecord can block for long periods of
* time, we must call it outside of the node lock.
* We call tree.serialize, which calls our modified writeRecord method that
* blocks until it can verify that a separate thread can lock the DataNode
* currently being written, i.e. that DataTree.serializeNode does not hold
* the DataNode lock while calling OutputArchive.writeRecord.
*/
@Test
@Timeout(value = 60)
public void testSerializeDoesntLockDataNodeWhileWriting() throws Exception {
DataTree tree = new DataTree();
tree.createNode("/marker", new byte[] { 42 }, null, -1, 1, 1, 1);
final DataNode markerNode = tree.getNode("/marker");
final AtomicBoolean ranTestCase = new AtomicBoolean();
DataOutputStream out = new DataOutputStream(new ByteArrayOutputStream());
BinaryOutputArchive oa = new BinaryOutputArchive(out) {
@Override
public void writeRecord(Record r, String tag) throws IOException {
// which adds default ACL to config node.
if (r instanceof DataNode) {
DataNode node = (DataNode) r;
if (node.data.length == 1 && node.data[0] == 42) {
final Semaphore semaphore = new Semaphore(0);
new Thread(new Runnable() {
@Override
public void run() {
synchronized (markerNode) {
// When we lock markerNode, allow writeRecord to continue
semaphore.release();
}
}
}).start();
try {
boolean acquired = semaphore.tryAcquire(30, TimeUnit.SECONDS);
// This is the real assertion - could another thread lock
// the DataNode we're currently writing
assertTrue(acquired, "Couldn't acquire a lock on the DataNode while we were calling tree.serialize");
} catch (InterruptedException e1) {
throw new RuntimeException(e1);
}
ranTestCase.set(true);
}
}
super.writeRecord(r, tag);
}
};
tree.serialize(oa, "test");
// Let's make sure that we hit the code that ran the real assertion above
assertTrue(ranTestCase.get(), "Didn't find the expected node");
}
use of org.junit.jupiter.api.Timeout in project zookeeper by apache.
the class DataTreeTest method testRootWatchTriggered.
@Test
@Timeout(value = 60)
public void testRootWatchTriggered() throws Exception {
DataTree dt = new DataTree();
CompletableFuture<Void> fire = new CompletableFuture<>();
// set a watch on the root node
dt.getChildren("/", new Stat(), event -> {
if (event.getPath().equals("/")) {
fire.complete(null);
}
});
// add a new node, should trigger a watch
dt.createNode("/xyz", new byte[0], null, 0, dt.getNode("/").stat.getCversion() + 1, 1, 1);
assertTrue(fire.isDone(), "Root node watch not triggered");
}
use of org.junit.jupiter.api.Timeout in project zookeeper by apache.
the class DataTreeTest method testPathTrieClearOnDeserialize.
@Test
@Timeout(value = 60)
public void testPathTrieClearOnDeserialize() throws Exception {
// Create a DataTree with quota nodes so PathTrie get updated
DataTree dserTree = new DataTree();
dserTree.createNode("/bug", new byte[20], null, -1, 1, 1, 1);
dserTree.createNode(Quotas.quotaPath("/bug"), null, null, -1, 1, 1, 1);
dserTree.createNode(Quotas.limitPath("/bug"), new byte[20], null, -1, 1, 1, 1);
dserTree.createNode(Quotas.statPath("/bug"), new byte[20], null, -1, 1, 1, 1);
// deserialize a DataTree; this should clear the old /bug nodes and pathTrie
DataTree tree = new DataTree();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BinaryOutputArchive oa = BinaryOutputArchive.getArchive(baos);
tree.serialize(oa, "test");
baos.flush();
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
BinaryInputArchive ia = BinaryInputArchive.getArchive(bais);
dserTree.deserialize(ia, "test");
Field pfield = DataTree.class.getDeclaredField("pTrie");
pfield.setAccessible(true);
PathTrie pTrie = (PathTrie) pfield.get(dserTree);
// Check that the node path is removed from pTrie
assertEquals("/", pTrie.findMaxPrefix("/bug"), "/bug is still in pTrie");
}
Aggregations