/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.zookeeper.test;
import java.io.IOException;
import org.apache.log4j.Logger;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.Quotas;
import org.apache.zookeeper.StatsTrack;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.ZooKeeperMain;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.data.Stat;
/**
* this class tests quota on a single
* zookeeper server.
*
*/
public class ZooKeeperQuotaTest extends ClientBase {
private static final Logger LOG = Logger.getLogger(
ZooKeeperQuotaTest.class);
@Override
protected void setUp() throws Exception {
super.setUp();
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
}
public void testQuota() throws IOException,
InterruptedException, KeeperException {
final ZooKeeper zk = createClient();
final String path = "/a/b/v";
// making sure setdata works on /
zk.setData("/", "some".getBytes(), -1);
zk.create("/a", "some".getBytes(), Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
zk.create("/a/b", "some".getBytes(), Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
zk.create("/a/b/v", "some".getBytes(), Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
zk.create("/a/b/v/d", "some".getBytes(), Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
ZooKeeperMain.createQuota(zk, path, 1000L, 1000);
// see if its set
String absolutePath = Quotas.quotaZookeeper + path + "/" + Quotas.limitNode;
byte[] data = zk.getData(absolutePath, false, new Stat());
StatsTrack st = new StatsTrack(new String(data));
assertTrue("bytes are set", st.getBytes() == 1000L);
assertTrue("num count is set", st.getCount() == 1000);
String statPath = Quotas.quotaZookeeper + path + "/" + Quotas.statNode;
byte[] qdata = zk.getData(statPath, false, new Stat());
StatsTrack qst = new StatsTrack(new String(qdata));
assertTrue("bytes are set", qst.getBytes() == 8L);
assertTrue("cound is set", qst.getCount() == 2);
}
}