Path: blob/main/components/supervisor/pkg/dropwriter/dropwriter_test.go
2500 views
// Copyright (c) 2020 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package dropwriter_test56import (7"testing"8"time"910"github.com/gitpod-io/gitpod/supervisor/pkg/dropwriter"11)1213func TestBucket(t *testing.T) {14steps := func(dt time.Duration, c int) []time.Duration {15res := make([]time.Duration, c)16for i := range res {17res[i] = dt18}19return res20}21constv := func(v int64, c int) []int64 {22res := make([]int64, c)23for i := range res {24res[i] = v25}26return res27}28clock := func(dt []time.Duration) dropwriter.Clock {29i := 030t := time.Time{}31return func() time.Time {32ot := t33t = t.Add(dt[i])34i++35return ot36}37}3839tests := []struct {40Name string41Bucket *dropwriter.Bucket42Reqs []int6443Expectations []int6444}{45{46Name: "below limit",47Bucket: dropwriter.NewBucketClock(20, 15, clock(steps(1300*time.Millisecond, 10))),48Reqs: constv(10, 10),49Expectations: constv(10, 10),50},51{52Name: "above limit",53Bucket: dropwriter.NewBucketClock(1, 1, clock(steps(1300*time.Millisecond, 10))),54Reqs: constv(10, 10),55Expectations: constv(1, 10),56},57{58Name: "upfront peak",59Bucket: dropwriter.NewBucketClock(20, 5, clock(steps(1300*time.Millisecond, 10))),60Reqs: append(constv(10, 1), constv(1, 9)...),61Expectations: append(constv(10, 1), constv(1, 9)...),62},63{64Name: "mid peak",65Bucket: dropwriter.NewBucketClock(20, 5, clock(steps(1300*time.Millisecond, 10))),66Reqs: append(constv(1, 4), append(constv(30, 1), constv(1, 5)...)...),67Expectations: append(constv(1, 4), append(constv(20, 1), constv(1, 5)...)...),68},69}7071for _, test := range tests {72t.Run(test.Name, func(t *testing.T) {73for step := range test.Reqs {74grant := test.Bucket.TakeAvailable(test.Reqs[step])75if grant != test.Expectations[step] {76t.Errorf("step %d: did not receive expected tokens: requested %d, expected %d, received %d", step, test.Reqs[step], test.Expectations[step], grant)77}78}79})80}81}828384