1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ============LICENSE_END=========================================================
19 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21 *******************************************************************************/
22 package org.onap.dmaap.mr.cambria;
24 import org.junit.Test;
26 public class CambriaRateLimiterTest
29 public void testRateLimiter ()
31 /*final NsaTestClock clock = new NsaTestClock(1, false);
33 final String topic = "topic";
34 final String consumerGroup = "group";
35 final String clientId = "id";
39 // rate limit: 1 empty call/min avg over 5 minutes, with 10ms delay
40 final CambriaRateLimiter rater = new CambriaRateLimiter ( 1.0, window, 10 );
43 // prime with a call to start rate window
44 rater.onCall ( topic, consumerGroup, clientId );
45 rater.onSend ( topic, consumerGroup, clientId, 1 );
46 clock.addMs ( 1000*60*window );
48 // rate should now be 0, with a good window
49 for ( int i=0; i<4; i++ )
51 clock.addMs ( 1000*15 );
52 rater.onCall ( topic, consumerGroup, clientId );
53 rater.onSend ( topic, consumerGroup, clientId, 0 );
55 // rate is now 0.8 = 4 calls in last 5 minutes = 4/5 = 0.8
57 clock.addMs ( 1000*15 );
58 rater.onCall ( topic, consumerGroup, clientId );
59 rater.onSend ( topic, consumerGroup, clientId, 0 );
60 // rate = 1.0 = 5 calls in last 5 mins
63 rater.onCall ( topic, consumerGroup, clientId );
64 rater.onSend ( topic, consumerGroup, clientId, 0 );
65 // rate = 1.2 = 6 calls in last 5 mins, should fire
67 fail ( "Should have thrown rate limit exception." );
69 catch ( CambriaApiException x )