github.com/unionj-cloud/go-doudou@v1.3.8-0.20221011095552-0088008e5b31/framework/ratelimit/limit_test.go (about)

     1  package ratelimit
     2  
     3  import (
     4  	. "github.com/smartystreets/goconvey/convey"
     5  	"reflect"
     6  	"testing"
     7  	"time"
     8  )
     9  
    10  func TestParse(t *testing.T) {
    11  	type args struct {
    12  		value string
    13  	}
    14  	tests := []struct {
    15  		name    string
    16  		args    args
    17  		want    Limit
    18  		wantErr bool
    19  	}{
    20  		{
    21  			name: "",
    22  			args: args{
    23  				value: "0.0055-S-20",
    24  			},
    25  			want: Limit{
    26  				Rate:   0.0055,
    27  				Burst:  20,
    28  				Period: time.Second,
    29  			},
    30  			wantErr: false,
    31  		},
    32  		{
    33  			name: "",
    34  			args: args{
    35  				value: "1000-H",
    36  			},
    37  			want: Limit{
    38  				Rate:   1000,
    39  				Burst:  1,
    40  				Period: time.Hour,
    41  			},
    42  			wantErr: false,
    43  		},
    44  		{
    45  			name: "",
    46  			args: args{
    47  				value: "1000-H-1-1-1",
    48  			},
    49  			want:    Limit{},
    50  			wantErr: true,
    51  		},
    52  		{
    53  			name: "",
    54  			args: args{
    55  				value: "1000-Y",
    56  			},
    57  			want:    Limit{},
    58  			wantErr: true,
    59  		},
    60  		{
    61  			name: "",
    62  			args: args{
    63  				value: "a-H",
    64  			},
    65  			want:    Limit{},
    66  			wantErr: true,
    67  		},
    68  		{
    69  			name: "",
    70  			args: args{
    71  				value: "1000-Y-100",
    72  			},
    73  			want:    Limit{},
    74  			wantErr: true,
    75  		},
    76  		{
    77  			name: "",
    78  			args: args{
    79  				value: "a-H-100",
    80  			},
    81  			want:    Limit{},
    82  			wantErr: true,
    83  		},
    84  		{
    85  			name: "",
    86  			args: args{
    87  				value: "10-H-abc",
    88  			},
    89  			want:    Limit{},
    90  			wantErr: true,
    91  		},
    92  	}
    93  	for _, tt := range tests {
    94  		t.Run(tt.name, func(t *testing.T) {
    95  			got, err := Parse(tt.args.value)
    96  			if (err != nil) != tt.wantErr {
    97  				t.Errorf("Parse() error = %v, wantErr %v", err, tt.wantErr)
    98  				return
    99  			}
   100  			if !reflect.DeepEqual(got, tt.want) {
   101  				t.Errorf("Parse() got = %v, want %v", got, tt.want)
   102  			}
   103  		})
   104  	}
   105  }
   106  
   107  func TestPerSecond(t *testing.T) {
   108  	Convey("Should equal to 10 in 1s", t, func() {
   109  		So(PerSecond(10), ShouldResemble, Limit{
   110  			Rate:   10,
   111  			Period: time.Second,
   112  			Burst:  1,
   113  		})
   114  	})
   115  
   116  	Convey("Should equal to 10 in 1s with 100 burst", t, func() {
   117  		So(PerSecondBurst(10, 100), ShouldResemble, Limit{
   118  			Rate:   10,
   119  			Period: time.Second,
   120  			Burst:  100,
   121  		})
   122  	})
   123  
   124  	Convey("Should equal to 10 in 1m", t, func() {
   125  		So(PerMinute(10), ShouldResemble, Limit{
   126  			Rate:   10,
   127  			Period: time.Minute,
   128  			Burst:  1,
   129  		})
   130  	})
   131  
   132  	Convey("Should equal to 10 in 1m with 100 burst", t, func() {
   133  		So(PerMinuteBurst(10, 100), ShouldResemble, Limit{
   134  			Rate:   10,
   135  			Period: time.Minute,
   136  			Burst:  100,
   137  		})
   138  	})
   139  
   140  	Convey("Should equal to 10 in 1h", t, func() {
   141  		So(PerHour(10), ShouldResemble, Limit{
   142  			Rate:   10,
   143  			Period: time.Hour,
   144  			Burst:  1,
   145  		})
   146  	})
   147  
   148  	Convey("Should equal to 10 in 1h with 100 burst", t, func() {
   149  		So(PerHourBurst(10, 100), ShouldResemble, Limit{
   150  			Rate:   10,
   151  			Period: time.Hour,
   152  			Burst:  100,
   153  		})
   154  	})
   155  
   156  	Convey("Should equal to 10 in 1d", t, func() {
   157  		So(PerDay(10), ShouldResemble, Limit{
   158  			Rate:   10,
   159  			Period: time.Hour * 24,
   160  			Burst:  1,
   161  		})
   162  	})
   163  
   164  	Convey("Should equal to 10 in 1d with 100 burst", t, func() {
   165  		So(PerDayBurst(10, 100), ShouldResemble, Limit{
   166  			Rate:   10,
   167  			Period: time.Hour * 24,
   168  			Burst:  100,
   169  		})
   170  	})
   171  }