summaryrefslogtreecommitdiffstats
path: root/messagebus/src/test/java/com/yahoo/messagebus/RateThrottlingTestCase.java
blob: dc4beb5cf9f7f33c381f7f2033cdb19106f2e5f5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.messagebus;

import com.yahoo.messagebus.test.SimpleMessage;
import junit.framework.TestCase;

public class RateThrottlingTestCase extends TestCase {

    public void testPending() {
        CustomTimer timer = new CustomTimer();
        RateThrottlingPolicy policy = new RateThrottlingPolicy(5.0, timer);
        policy.setMaxPendingCount(200);

        // Check that we obey the max still.
        assertFalse(policy.canSend(new SimpleMessage("test"), 300));
    }

    public int getActualRate(double desiredRate) {
        CustomTimer timer = new CustomTimer();
        RateThrottlingPolicy policy = new RateThrottlingPolicy(desiredRate, timer);

        int ok = 0;
        for (int i = 0; i < 10000; ++i) {
            if (policy.canSend(new SimpleMessage("test"), 0)) {
                ok++;
            }
            timer.millis += 10;
        }

        return ok;
    }

    public void testRates() {
        assertEquals(10, getActualRate(0.1), 1);
        assertEquals(1000, getActualRate(10), 100);
        assertEquals(500, getActualRate(5), 50);
        assertEquals(100, getActualRate(1), 10);
    }
}