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 com.att.nsa.cambria;
24 import junit.framework.TestCase;
26 import org.junit.Test;
28 import com.att.nsa.apiServer.util.NsaTestClock;
30 public class CambriaRateLimiterTest
33 public void testRateLimiter ()
35 /*final NsaTestClock clock = new NsaTestClock(1, false);
37 final String topic = "topic";
38 final String consumerGroup = "group";
39 final String clientId = "id";
43 // rate limit: 1 empty call/min avg over 5 minutes, with 10ms delay
44 final CambriaRateLimiter rater = new CambriaRateLimiter ( 1.0, window, 10 );
47 // prime with a call to start rate window
48 rater.onCall ( topic, consumerGroup, clientId );
49 rater.onSend ( topic, consumerGroup, clientId, 1 );
50 clock.addMs ( 1000*60*window );
52 // rate should now be 0, with a good window
53 for ( int i=0; i<4; i++ )
55 clock.addMs ( 1000*15 );
56 rater.onCall ( topic, consumerGroup, clientId );
57 rater.onSend ( topic, consumerGroup, clientId, 0 );
59 // rate is now 0.8 = 4 calls in last 5 minutes = 4/5 = 0.8
61 clock.addMs ( 1000*15 );
62 rater.onCall ( topic, consumerGroup, clientId );
63 rater.onSend ( topic, consumerGroup, clientId, 0 );
64 // rate = 1.0 = 5 calls in last 5 mins
67 rater.onCall ( topic, consumerGroup, clientId );
68 rater.onSend ( topic, consumerGroup, clientId, 0 );
69 // rate = 1.2 = 6 calls in last 5 mins, should fire
71 fail ( "Should have thrown rate limit exception." );
73 catch ( CambriaApiException x )