@@ -10,7 +10,7 @@ import (
1010 "sync"
1111 "testing"
1212 "time"
13-
13+
1414 "golang.org/x/time/rate"
1515)
1616
@@ -22,16 +22,16 @@ func TestClientRateLimiter(t *testing.T) {
2222 }{
2323 requestTimesByMethod : make (map [string ][]time.Time ),
2424 }
25-
25+
2626 mockedServer := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
2727 const totalSteps = 2
28-
28+
2929 mockedServerState .mutex .Lock ()
3030 defer mockedServerState .mutex .Unlock ()
31-
31+
3232 mockedServerState .requestTimesByMethod [r .Method ] = append (mockedServerState .requestTimesByMethod [r .Method ], time .Now ())
3333 t .Logf ("request %s %s %s" , time .Now ().Format ("15:04:05" ), r .Method , r .URL .String ())
34-
34+
3535 if r .Method == http .MethodPost {
3636 sessionId := mockedServerState .nextSessionId
3737 mockedServerState .nextSessionId ++
@@ -50,14 +50,14 @@ func TestClientRateLimiter(t *testing.T) {
5050 }
5151 }))
5252 defer mockedServer .Close ()
53-
53+
5454 datasetClient := & dataSetClient {
5555 dataSetUrl : mockedServer .URL ,
5656 apiKey : "<api-key>" ,
5757 netClient : & http.Client {},
5858 rateLimiter : rate .NewLimiter (1 * rate .Every (1 * time .Second ), 1 ),
5959 }
60-
60+
6161 request := LRQRequest {
6262 QueryType : PQ ,
6363 StartTime : time .Now ().Add (- 4 * time .Hour ).Unix (),
@@ -67,7 +67,7 @@ func TestClientRateLimiter(t *testing.T) {
6767 ResultType : TABLE ,
6868 },
6969 }
70-
70+
7171 var waitGroup sync.WaitGroup
7272 for i := 0 ; i < 3 ; i ++ {
7373 waitGroup .Add (1 )
@@ -89,20 +89,20 @@ func TestClientRateLimiter(t *testing.T) {
8989 }
9090 return rv
9191 }
92-
92+
9393 // The rate limiter is set to only allow one session (POST request) per second.
9494 // Verify that the minimum amount of time between consecutive POST requests is at least one second.
9595 if diff := minTimeDiff (mockedServerState .requestTimesByMethod [http .MethodPost ]); diff .Round (time .Second ) < time .Second {
9696 t .Errorf ("expected >= 1s, actual = %v" , diff )
9797 }
98-
98+
9999 // There are no restrictions on GET or DELETE requests.
100100 // Verify that the minimum amount of time between non-POST requests is less than 500 milliseconds.
101101 var requestTimes []time.Time
102102 requestTimes = append (requestTimes , mockedServerState .requestTimesByMethod [http .MethodGet ]... )
103103 requestTimes = append (requestTimes , mockedServerState .requestTimesByMethod [http .MethodDelete ]... )
104104 sort .Slice (requestTimes , func (i , j int ) bool { return requestTimes [i ].Before (requestTimes [j ]) })
105- if diff := minTimeDiff (requestTimes ); diff .Round (time .Millisecond ) > 500 * time .Millisecond {
105+ if diff := minTimeDiff (requestTimes ); diff .Round (time .Millisecond ) > 500 * time .Millisecond {
106106 t .Errorf ("expected < 500ms, actual = %v" , diff )
107107 }
108108}
0 commit comments