zookeeper-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] anmolnar commented on a change in pull request #769: ZOOKEEPER-3242: Add server side connecting throttling
Date Mon, 21 Jan 2019 17:56:06 GMT
anmolnar commented on a change in pull request #769: ZOOKEEPER-3242: Add server side connecting
throttling
URL: https://github.com/apache/zookeeper/pull/769#discussion_r249539145
 
 

 ##########
 File path: zookeeper-server/src/test/java/org/apache/zookeeper/server/BlueThrottleTest.java
 ##########
 @@ -0,0 +1,132 @@
+/**
+ * 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.server;
+
+import org.apache.zookeeper.ZKTestCase;
+import org.junit.Assert;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class BlueThrottleTest extends ZKTestCase {
+    private static final Logger LOG = LoggerFactory.getLogger(BlueThrottleTest.class);
+
+    @Test
+    public void testThrottleDisabled() {
+        BlueThrottle throttler = new BlueThrottle();
+        Assert.assertTrue("Throttle should be disabled by default", throttler.checkLimit(1));
+    }
+
+    @Test
+    public void testThrottleWithoutRefill() {
+        BlueThrottle throttler = new BlueThrottle();
+        throttler.setMaxTokens(1);
+        throttler.setFillTime(2000);
+        Assert.assertTrue("First request should be allowed", throttler.checkLimit(1));
+        Assert.assertFalse("Second request should be denied", throttler.checkLimit(1));
+    }
+
+    @Test
+    public void testThrottleWithRefill() throws InterruptedException {
+        BlueThrottle throttler = new BlueThrottle();
+        throttler.setMaxTokens(1);
+        throttler.setFillTime(500);
+        Assert.assertTrue("First request should be allowed", throttler.checkLimit(1));
+        Assert.assertFalse("Second request should be denied", throttler.checkLimit(1));
+
+        //wait for the bucket to be refilled
+        Thread.sleep(750);
+        Assert.assertTrue("Third request should be allowed since we've got a new token",
throttler.checkLimit(1));
+    }
+
+    @Test
+    public void testThrottleWithoutRandomDropping() throws InterruptedException {
+        int maxTokens = 10;
+        BlueThrottle throttler = new BlueThrottle();
+        throttler.setMaxTokens(maxTokens);
+        throttler.setFillCount(maxTokens);
+        throttler.setFillTime(1000);
+
+        for (int i=0;i<maxTokens;i++) {
+            throttler.checkLimit(1);
+        }
+        Assert.assertEquals("All tokens should be used up by now", throttler.getMaxTokens(),
throttler.getDeficit());
+
+        Thread.sleep(110);
+        throttler.checkLimit(1);
+        Assert.assertFalse("Dropping probability should still be zero", throttler.getDropChance()>0);
+
+        //allow bucket to be refilled
+        Thread.sleep(1500);
+
+        for (int i=0;i<maxTokens;i++) {
+            Assert.assertTrue("The first " + maxTokens + " requests should be allowed", throttler.checkLimit(1));
+        }
+
+        for (int i=0;i<maxTokens;i++) {
+            Assert.assertFalse("The latter " + maxTokens + " requests should be denied",
throttler.checkLimit(1));
+        }
+    }
+
+    @Test
+    public void testThrottleWithRandomDropping() throws InterruptedException {
+        int maxTokens = 10;
+        BlueThrottle throttler = new BlueThrottle();
+        throttler.setMaxTokens(maxTokens);
+        throttler.setFillCount(maxTokens);
+        throttler.setFillTime(1000);
+        throttler.setFreezeTime(100);
+        throttler.setDropIncrease(0.5);
+
+        for (int i=0;i<maxTokens;i++)
+            throttler.checkLimit(1);
+        Assert.assertEquals("All tokens should be used up by now", throttler.getMaxTokens(),
throttler.getDeficit());
+
+        Thread.sleep(110);
 
 Review comment:
   Have you tried to run this tests multiple times? 100x or 1000x ... ?
   Tests with `Thread.sleep` are usually a smell of flaky tests, especially in Apache infra.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

Mime
View raw message