File: token_bucket.ut.cpp

package info (click to toggle)
falcosecurity-libs 0.1.1dev%2Bgit20220316.e5c53d64-5.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 4,732 kB
  • sloc: cpp: 55,770; ansic: 37,330; makefile: 74; sh: 13
file content (67 lines) | stat: -rw-r--r-- 1,537 bytes parent folder | download
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
#include "token_bucket.h"
#include <gtest.h>
#include <memory>

// token bucket default ctor
TEST(token_bucket, constructor)
{
	auto tb = std::make_shared<token_bucket>();

	EXPECT_EQ(tb->get_tokens(), 1);

	// initialising with specific time, rate 2 tokens/sec
	auto max = 2.0;
	uint64_t now = 1;
	tb->init(1.0, max, now);
	EXPECT_EQ(tb->get_last_seen(), now);
	EXPECT_EQ(tb->get_tokens(), max);
	
}

// token bucket ctor with custom timer
TEST(token_bucket, constructor_custom_timer)
{
	auto t = []() -> uint64_t { return 22; };
	auto tb = std::make_shared<token_bucket>(t);

	EXPECT_EQ(tb->get_tokens(), 1);
	EXPECT_EQ(tb->get_last_seen(), 22);
}

// token bucket with 2 tokens/sec rate, max 10 tokens
TEST(token_bucket, two_token_per_sec_ten_max)
{
	auto tb = std::make_shared<token_bucket>();
	tb->init(2.0, 10, 1);

	// claiming 5 tokens
	{
		bool claimed = tb->claim(5, 1000000001);
		EXPECT_EQ(tb->get_last_seen(), 1000000001);
		EXPECT_EQ(tb->get_tokens(), 5.0);
		EXPECT_TRUE(claimed);
	}

	// claiming all the 7 remaining tokens	
	{ 
		bool claimed = tb->claim(7, 2000000001);
		EXPECT_EQ(tb->get_last_seen(), 2000000001);
		EXPECT_EQ(tb->get_tokens(), 0.0);
		EXPECT_TRUE(claimed);
	}

	// claiming 1 token more than the 2 available fails		
	{
		bool claimed = tb->claim(3, 3000000001);
		EXPECT_EQ(tb->get_last_seen(),3000000001);
		EXPECT_EQ(tb->get_tokens(), 2.0);
		EXPECT_FALSE(claimed);
	}
}

// token bucket default initialization
TEST(token_bucket, default_init)
{
	token_bucket tb;
	EXPECT_EQ(tb.get_tokens(), 1);
}