github.com/voedger/voedger@v0.0.0-20240520144910-273e84102129/pkg/sys/it/impl_rates_test.go (about)

     1  /*
     2   * Copyright (c) 2020-present unTill Pro, Ltd.
     3   */
     4  
     5  package sys_it
     6  
     7  import (
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/voedger/voedger/pkg/istructs"
    12  	coreutils "github.com/voedger/voedger/pkg/utils"
    13  	it "github.com/voedger/voedger/pkg/vit"
    14  )
    15  
    16  func TestRates_BasicUsage(t *testing.T) {
    17  	vit := it.NewVIT(t, &it.SharedConfig_App1)
    18  	defer vit.TearDown()
    19  
    20  	ws := vit.WS(istructs.AppQName_test1_app1, "test_ws")
    21  	bodyQry := `{"args":{}, "elements":[{"path":"","fields":["Fld"]}]}`
    22  	bodyCmd := `{"args":{}}`
    23  
    24  	// first 2 calls are ok
    25  	for i := 0; i < 2; i++ {
    26  		vit.PostWS(ws, "q.app1pkg.RatedQry", bodyQry)
    27  		vit.PostWS(ws, "c.app1pkg.RatedCmd", bodyCmd)
    28  	}
    29  
    30  	// 3rd is failed because per-minute rate is exceeded
    31  	vit.PostWS(ws, "q.app1pkg.RatedQry", bodyQry, coreutils.Expect429())
    32  	vit.PostWS(ws, "c.app1pkg.RatedCmd", bodyCmd, coreutils.Expect429())
    33  
    34  	// proceed to the next minute to restore per-minute rates
    35  	vit.TimeAdd(time.Minute)
    36  
    37  	// next 2 calls are ok again
    38  	for i := 0; i < 2; i++ {
    39  		vit.PostWS(ws, "q.app1pkg.RatedQry", bodyQry)
    40  		vit.PostWS(ws, "c.app1pkg.RatedCmd", bodyCmd)
    41  	}
    42  
    43  	// next are failed again because per-minute rate is exceeded again
    44  	vit.PostWS(ws, "q.app1pkg.RatedQry", bodyQry, coreutils.Expect429())
    45  	vit.PostWS(ws, "c.app1pkg.RatedCmd", bodyCmd, coreutils.Expect429())
    46  
    47  	// proceed to the next minute to restore per-minute rates
    48  	vit.TimeAdd(time.Minute)
    49  
    50  	// next are failed again because per-hour rate is exceeded
    51  	vit.PostWS(ws, "q.app1pkg.RatedQry", bodyQry, coreutils.Expect429())
    52  	vit.PostWS(ws, "c.app1pkg.RatedCmd", bodyCmd, coreutils.Expect429())
    53  
    54  	// proceed to the next hour to restore per-hour rates
    55  	vit.TimeAdd(time.Hour)
    56  
    57  	// next 2 calls are ok again
    58  	for i := 0; i < 2; i++ {
    59  		vit.PostWS(ws, "q.app1pkg.RatedQry", bodyQry)
    60  		vit.PostWS(ws, "c.app1pkg.RatedCmd", bodyCmd)
    61  	}
    62  }