DMAAP-MR - Merge MR repos
[dmaap/messagerouter/messageservice.git] / src / test / java / org / onap / dmaap / mr / cambria / CambriaRateLimiterTest.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  org.onap.dmaap
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
11  *  
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=========================================================
18  *
19  *  ECOMP is a trademark and service mark of AT&T Intellectual Property.
20  *  
21  *******************************************************************************/
22 package org.onap.dmaap.mr.cambria;
23
24 import org.junit.Test;
25
26 public class CambriaRateLimiterTest 
27 {
28         @Test
29         public void testRateLimiter ()
30         {
31                 /*final NsaTestClock clock = new NsaTestClock(1, false);
32
33                 final String topic = "topic";
34                 final String consumerGroup = "group";
35                 final String clientId = "id";
36
37                 final int window = 5;
38
39                 // rate limit: 1 empty call/min avg over 5 minutes, with 10ms delay
40                 final CambriaRateLimiter rater = new CambriaRateLimiter ( 1.0, window, 10 );
41                 try
42                 {
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 );
47
48                         // rate should now be 0, with a good window
49                         for ( int i=0; i<4; i++ )
50                         {
51                                 clock.addMs ( 1000*15 );
52                                 rater.onCall ( topic, consumerGroup, clientId );
53                                 rater.onSend ( topic, consumerGroup, clientId, 0 );
54                         }
55                         // rate is now 0.8 = 4 calls in last 5 minutes = 4/5 = 0.8
56
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
61
62                         clock.addMs ( 1000 );
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
66
67                         fail ( "Should have thrown rate limit exception." );
68                 }
69                 catch ( CambriaApiException x )
70                 {
71                         // good
72                 }*/
73         }
74 }