@@ -4,8 +4,8 @@ defmodule ReplicatedRateLimiterTest do
4
4
5
5
defmodule CrdtEts do
6
6
use ReplicatedRateLimiter ,
7
- default_capacity: 1_000 ,
8
- default_refill: 200
7
+ default_capacity: 10 ,
8
+ default_refill: 2
9
9
end
10
10
11
11
defmodule AnotherRateLimiter do
@@ -17,50 +17,56 @@ defmodule ReplicatedRateLimiterTest do
17
17
18
18
config = CrdtEts . config ( ) |> Map . new ( )
19
19
20
- # Test using the main module's allow? function
21
- result = CrdtEts . allow? ( "test_key" )
22
- assert match? ( { :allow , 999 } , result )
20
+ # Check one time (default cost is 1)
21
+ assert match? ( { :allow , 9 } , CrdtEts . allow? ( "project1" ) )
23
22
24
23
before = System . system_time ( :second )
25
24
26
- # Make several calls to hit the limit
27
- Enum . each ( 1 .. 10 , fn _ -> CrdtEts . allow? ( "test_key_2" , 5 , 1 ) end )
25
+ # Make several calls to hit the limit (default capacity is 10)
26
+ Enum . each ( 1 .. 9 , fn _ -> CrdtEts . allow? ( "project2" ) end )
28
27
29
- assert { 0 , last_updated } =
30
- DeltaCrdt . get ( config . crdt_name , { "test_key_2" , "#{ Node . self ( ) } " } ) ,
31
- "CRDT should have 0 tokens"
28
+ assert_eventually (
29
+ DeltaCrdt . get ( config . crdt_name , { "project2" , "#{ Node . self ( ) } " } ) |> dbg |> elem ( 0 ) == 0 ,
30
+ 1_000
31
+ )
32
32
33
- assert { 0 , ^ last_updated } =
34
- CrdtEts . to_list ( "test_key_2 " ) ,
33
+ assert { 0 , last_updated } =
34
+ CrdtEts . to_list ( "project2 " ) ,
35
35
"ETS should be the same as CRDT"
36
36
37
37
assert before <= last_updated
38
38
39
39
# This should be denied since we consumed all tokens
40
- assert { :deny , 1000 } = CrdtEts . allow? ( "test_key_2 " , 5 , 1 )
40
+ assert { :deny , 1000 } = CrdtEts . allow? ( "project2 " , 10 , 2 )
41
41
42
- # Another node enters the dungeon
42
+ # Node2 enters the dungeon
43
43
{ :ok , test_crdt } =
44
44
DeltaCrdt . start_link ( DeltaCrdt.AWLWWMap )
45
45
46
46
DeltaCrdt . set_neighbours ( config . crdt_name , [ test_crdt ] )
47
47
DeltaCrdt . set_neighbours ( test_crdt , [ config . crdt_name ] )
48
48
49
- # and updates the bucket
49
+ # a time has passed and the bucket is refilled by Node2
50
50
DeltaCrdt . put (
51
51
test_crdt ,
52
- { "test_key_2 " , "another_node" } ,
53
- { 10 , System . system_time ( :second ) + 1 }
52
+ { "project2 " , "another_node" } ,
53
+ { 10 , System . system_time ( :second ) }
54
54
)
55
55
56
+ # Node2 consumes all the credits except one
57
+ Enum . each ( 1 .. 9 , fn i ->
58
+ DeltaCrdt . put (
59
+ test_crdt ,
60
+ { "project2" , "another_node" } ,
61
+ { 10 - i , System . system_time ( :second ) } )
62
+ end )
63
+
56
64
# Wait for the bucket to be updated
57
65
assert_eventually (
58
- CrdtEts . to_list ( "test_key_2" )
59
- |> Tuple . to_list ( )
60
- |> List . first ( ) == 10
66
+ CrdtEts . to_list ( "project2" ) |> elem ( 0 ) == 1
61
67
)
62
68
63
- assert { :allow , 4 } = CrdtEts . allow? ( "test_key_2 " , 5 , 1 )
69
+ assert { :allow , 4 } = CrdtEts . allow? ( "project2 " , 10 , 2 )
64
70
end
65
71
66
72
test "can start multiple rate limiters" do
0 commit comments