summaryrefslogtreecommitdiffstats
path: root/media/mtransport/test/simpletokenbucket_unittest.cpp
blob: 70c962bb84989a849b9c6f86efe176e842f9c0aa (plain)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

/* Original author: bcampen@mozilla.com */

#include "simpletokenbucket.h"

#define GTEST_HAS_RTTI 0
#include "gtest/gtest.h"
#include "gtest_utils.h"

using mozilla::SimpleTokenBucket;

class TestSimpleTokenBucket : public SimpleTokenBucket {
  public:
    TestSimpleTokenBucket(size_t bucketSize, size_t tokensPerSecond) :
      SimpleTokenBucket(bucketSize, tokensPerSecond) {
    }

    void fastForward(int32_t timeMilliSeconds) {
      if (timeMilliSeconds >= 0) {
        last_time_tokens_added_ -= PR_MillisecondsToInterval(timeMilliSeconds);
      } else {
        last_time_tokens_added_ += PR_MillisecondsToInterval(-timeMilliSeconds);
      }
    }
};

TEST(SimpleTokenBucketTest, TestConstruct) {
  TestSimpleTokenBucket b(10, 1);
}

TEST(SimpleTokenBucketTest, TestGet) {
  TestSimpleTokenBucket b(10, 1);
  ASSERT_EQ(5U, b.getTokens(5));
}

TEST(SimpleTokenBucketTest, TestGetAll) {
  TestSimpleTokenBucket b(10, 1);
  ASSERT_EQ(10U, b.getTokens(10));
}

TEST(SimpleTokenBucketTest, TestGetInsufficient) {
  TestSimpleTokenBucket b(10, 1);
  ASSERT_EQ(5U, b.getTokens(5));
  ASSERT_EQ(5U, b.getTokens(6));
}

TEST(SimpleTokenBucketTest, TestGetBucketCount) {
  TestSimpleTokenBucket b(10, 1);
  ASSERT_EQ(10U, b.getTokens(UINT32_MAX));
  ASSERT_EQ(5U, b.getTokens(5));
  ASSERT_EQ(5U, b.getTokens(UINT32_MAX));
}

TEST(SimpleTokenBucketTest, TestTokenRefill) {
  TestSimpleTokenBucket b(10, 1);
  ASSERT_EQ(5U, b.getTokens(5));
  b.fastForward(1000);
  ASSERT_EQ(6U, b.getTokens(6));
}

TEST(SimpleTokenBucketTest, TestNoTimeWasted) {
  // Makes sure that when the time elapsed is insufficient to add any
  // tokens to the bucket, the internal timestamp that is used in this
  // calculation is not updated (ie; two subsequent 0.5 second elapsed times
  // counts as a full second)
  TestSimpleTokenBucket b(10, 1);
  ASSERT_EQ(5U, b.getTokens(5));
  b.fastForward(500);
  ASSERT_EQ(5U, b.getTokens(6));
  b.fastForward(500);
  ASSERT_EQ(6U, b.getTokens(6));
}

TEST(SimpleTokenBucketTest, TestNegativeTime) {
  TestSimpleTokenBucket b(10, 1);
  b.fastForward(-1000);
  // Make sure we don't end up with an invalid number of tokens, but otherwise
  // permit anything.
  ASSERT_GT(11U, b.getTokens(100));
}

TEST(SimpleTokenBucketTest, TestEmptyBucket) {
  TestSimpleTokenBucket b(10, 1);
  ASSERT_EQ(10U, b.getTokens(10));
  ASSERT_EQ(0U, b.getTokens(10));
}

TEST(SimpleTokenBucketTest, TestEmptyThenFillBucket) {
  TestSimpleTokenBucket b(10, 1);
  ASSERT_EQ(10U, b.getTokens(10));
  ASSERT_EQ(0U, b.getTokens(1));
  b.fastForward(50000);
  ASSERT_EQ(10U, b.getTokens(10));
}

TEST(SimpleTokenBucketTest, TestNoOverflow) {
  TestSimpleTokenBucket b(10, 1);
  ASSERT_EQ(10U, b.getTokens(10));
  ASSERT_EQ(0U, b.getTokens(1));
  b.fastForward(50000);
  ASSERT_EQ(10U, b.getTokens(11));
}