# The tests in this file verify token bucket requests. They are not meant to be
# basic tests, not comprehensive tests of the logic (those belong in the
# tenanttokenbucket subpackage).

create-tenant tenant=5
----

token-bucket-request tenant=5
instance_id: 1
tokens: 10
----
10 tokens granted over 10s. Fallback rate: 10000 tokens/s

inspect tenant=5
----
Bucket state: token-burst-limit=0  token-refill-rate=10000  token-current=-10  token-current-avg=0
Consumption: ru=0 kvru=0  reads=0 in 0 batches (0 bytes)  writes=0 in 0 batches (0 bytes)  pod-cpu-usage: 0 secs  pgwire-egress=0 bytes  external-egress=0 bytes  external-ingress=0 bytes  estimated-cpu: 0 secs
Rates: write-batches=0,0  estimated-cpu=0,0
Last update: 00:00:00.000
First active instance: 1
  Instance 1:  lease="foo"  seq=1  next-instance=0  last-update=00:00:00.000

advance
10s
----
00:00:10.000

token-bucket-request tenant=5
instance_id: 1
tokens: 500
----
500 tokens granted immediately. Fallback rate: 10027.775 tokens/s

inspect tenant=5
----
Bucket state: token-burst-limit=0  token-refill-rate=10000  token-current=99490  token-current-avg=24997.5
Consumption: ru=0 kvru=0  reads=0 in 0 batches (0 bytes)  writes=0 in 0 batches (0 bytes)  pod-cpu-usage: 0 secs  pgwire-egress=0 bytes  external-egress=0 bytes  external-ingress=0 bytes  estimated-cpu: 0 secs
Rates: write-batches=0,0  estimated-cpu=0,0
Last update: 00:00:10.000
First active instance: 1
  Instance 1:  lease="foo"  seq=2  next-instance=0  last-update=00:00:10.000

token-bucket-request tenant=5
instance_id: 2
tokens: 250
----
250 tokens granted immediately. Fallback rate: 10027.63611 tokens/s

# Verify that if the time goes backward, the tenant's last_updated does not go
# back (which would result in some tokens being doubly refilled). Note that in
# practice this would happen when two nodes with drifting clocks serve token
# bucket requests for the same tenant

advance
-1s
----
00:00:09.000

token-bucket-request tenant=5
instance_id: 1
tokens: 10
----
10 tokens granted immediately. Fallback rate: 10027.56667 tokens/s

# Last update time for the tenant is unchanged. The per-instance time does get updated.
inspect tenant=5
----
Bucket state: token-burst-limit=0  token-refill-rate=10000  token-current=99230  token-current-avg=57525.46875
Consumption: ru=0 kvru=0  reads=0 in 0 batches (0 bytes)  writes=0 in 0 batches (0 bytes)  pod-cpu-usage: 0 secs  pgwire-egress=0 bytes  external-egress=0 bytes  external-ingress=0 bytes  estimated-cpu: 0 secs
Rates: write-batches=0,0  estimated-cpu=0,0
Last update: 00:00:10.000
First active instance: 1
  Instance 1:  lease="foo"  seq=4  next-instance=2  last-update=00:00:09.000
  Instance 2:  lease="foo"  seq=3  next-instance=0  last-update=00:00:10.000

advance
1s
----
00:00:10.000

token-bucket-request tenant=5
instance_id: 1
----

# The current tokens amount stays the same.
inspect tenant=5
----
Bucket state: token-burst-limit=0  token-refill-rate=10000  token-current=99230  token-current-avg=67951.6015625
Consumption: ru=0 kvru=0  reads=0 in 0 batches (0 bytes)  writes=0 in 0 batches (0 bytes)  pod-cpu-usage: 0 secs  pgwire-egress=0 bytes  external-egress=0 bytes  external-ingress=0 bytes  estimated-cpu: 0 secs
Rates: write-batches=0,0  estimated-cpu=0,0
Last update: 00:00:10.000
First active instance: 1
  Instance 1:  lease="foo"  seq=5  next-instance=2  last-update=00:00:10.000
  Instance 2:  lease="foo"  seq=3  next-instance=0  last-update=00:00:10.000

advance
1s
----
00:00:11.000

token-bucket-request tenant=5
instance_id: 1
----

# tokens refilling resumed.
inspect tenant=5
----
Bucket state: token-burst-limit=0  token-refill-rate=10000  token-current=109230  token-current-avg=78271.2011719
Consumption: ru=0 kvru=0  reads=0 in 0 batches (0 bytes)  writes=0 in 0 batches (0 bytes)  pod-cpu-usage: 0 secs  pgwire-egress=0 bytes  external-egress=0 bytes  external-ingress=0 bytes  estimated-cpu: 0 secs
Rates: write-batches=0,0  estimated-cpu=0,0
Last update: 00:00:11.000
First active instance: 1
  Instance 1:  lease="foo"  seq=6  next-instance=2  last-update=00:00:11.000
  Instance 2:  lease="foo"  seq=3  next-instance=0  last-update=00:00:10.000
