@@ -2,16 +2,99 @@ package throttle
22
33import (
44"context"
5+ "github.com/aws/aws-sdk-go/aws/client/metadata"
56"github.com/aws/aws-sdk-go/aws/request"
7+ "github.com/aws/aws-sdk-go/service/appmesh"
8+ "github.com/aws/aws-sdk-go/service/servicediscovery"
69"github.com/stretchr/testify/assert"
710"golang.org/x/time/rate"
811"net/http"
12+ "regexp"
913"sync"
1014"sync/atomic"
1115"testing"
1216"time"
1317)
1418
19+ func Test_NewThrottler (t * testing.T ) {
20+ config := ServiceOperationsThrottleConfig {
21+ value : map [string ][]throttleConfig {
22+ appmesh .ServiceID : {
23+ {
24+ operationPtn : regexp .MustCompile ("^Describe" ),
25+ r : 4.2 ,
26+ burst : 5 ,
27+ },
28+ {
29+ operationPtn : regexp .MustCompile ("CreateMesh" ),
30+ r : 3.8 ,
31+ burst : 4 ,
32+ },
33+ },
34+ servicediscovery .ServiceID : {
35+ {
36+ operationPtn : regexp .MustCompile ("^Create" ),
37+ r : 1.2 ,
38+ burst : 2 ,
39+ },
40+ },
41+ },
42+ }
43+
44+ throttler := NewThrottler (& config )
45+ assert .Equal (t , 3 , len (throttler .conditionLimiters ))
46+ }
47+
48+ func Test_throttler_WithConditionThrottle (t * testing.T ) {
49+ throttler := & throttler {}
50+ throttler .WithConditionThrottle (matchService (appmesh .ServiceID ), 5.0 , 10 )
51+
52+ assert .Equal (t , 1 , len (throttler .conditionLimiters ))
53+
54+ cl := throttler .conditionLimiters [0 ]
55+ assert .True (t , cl .condition (& request.Request {ClientInfo : metadata.ClientInfo {ServiceID : appmesh .ServiceID }}))
56+ assert .Equal (t , rate .NewLimiter (5.0 , 10 ), cl .limiter )
57+ }
58+
59+ func Test_throttler_WithServiceThrottle (t * testing.T ) {
60+ throttler := & throttler {}
61+ throttler .WithServiceThrottle (appmesh .ServiceID , 5.0 , 10 )
62+
63+ assert .Equal (t , 1 , len (throttler .conditionLimiters ))
64+
65+ cl := throttler .conditionLimiters [0 ]
66+ assert .True (t , cl .condition (& request.Request {ClientInfo : metadata.ClientInfo {ServiceID : appmesh .ServiceID }}))
67+ assert .Equal (t , rate .NewLimiter (5.0 , 10 ), cl .limiter )
68+ }
69+
70+ func Test_throttler_WithOperationThrottle (t * testing.T ) {
71+ throttler := & throttler {}
72+ throttler .WithOperationThrottle (appmesh .ServiceID , "CreateMesh" , 5.0 , 10 )
73+
74+ assert .Equal (t , 1 , len (throttler .conditionLimiters ))
75+
76+ cl := throttler .conditionLimiters [0 ]
77+ assert .True (t , cl .condition (& request.Request {
78+ ClientInfo : metadata.ClientInfo {ServiceID : appmesh .ServiceID },
79+ Operation : & request.Operation {Name : "CreateMesh" },
80+ }))
81+ assert .Equal (t , rate .NewLimiter (5.0 , 10 ), cl .limiter )
82+ }
83+
84+ func Test_throttler_WithOperationPatternThrottle (t * testing.T ) {
85+ throttler := & throttler {}
86+ throttler .WithOperationPatternThrottle (appmesh .ServiceID , regexp .MustCompile ("^Create" ), 5.0 , 10 )
87+
88+ assert .Equal (t , 1 , len (throttler .conditionLimiters ))
89+
90+ cl := throttler .conditionLimiters [0 ]
91+ assert .True (t , cl .condition (& request.Request {
92+ ClientInfo : metadata.ClientInfo {ServiceID : appmesh .ServiceID },
93+ Operation : & request.Operation {Name : "CreateMesh" },
94+ }))
95+ assert .Equal (t , rate .NewLimiter (5.0 , 10 ), cl .limiter )
96+ }
97+
1598func Test_throttler_InjectHandlers (t * testing.T ) {
1699throttler := & throttler {}
17100handlers := request.Handlers {}
0 commit comments