github.com/songzhibin97/gkit@v1.2.13/window/leap_array_test.go (about)

     1  package window
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/songzhibin97/gkit/internal/sys/mutex"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestGetTimeIndex(t *testing.T) {
    12  	type fields struct {
    13  		bucketSize   uint64
    14  		n            uint64
    15  		intervalSize uint64
    16  		array        *AtomicArray
    17  	}
    18  	type args struct {
    19  		timeMillis uint64
    20  	}
    21  	tests := []struct {
    22  		name   string
    23  		fields fields
    24  		args   args
    25  		want   uint64
    26  	}{
    27  		{
    28  			name: "TestGetTimeIndex",
    29  			fields: fields{
    30  				bucketSize:   BucketSize,
    31  				n:            N,
    32  				intervalSize: IntervalSize,
    33  				array:        NewAtomicArray(N, BucketSize, &Mock{}),
    34  			},
    35  			args: args{
    36  				timeMillis: 1576296044907,
    37  			},
    38  			want: 9,
    39  		},
    40  	}
    41  	for _, tt := range tests {
    42  		t.Run(tt.name, func(t *testing.T) {
    43  			s := &LeapArray{
    44  				bucketSize:   tt.fields.bucketSize,
    45  				n:            tt.fields.n,
    46  				intervalSize: tt.fields.intervalSize,
    47  				array:        tt.fields.array,
    48  				mu:           mutex.Mutex{},
    49  			}
    50  			if got := s.getTimeIndex(tt.args.timeMillis); got != tt.want {
    51  				t.Errorf("getTimeIndex() = %v, want %v", got, tt.want)
    52  			}
    53  		})
    54  	}
    55  }
    56  
    57  func TestCalculateStartTime(t *testing.T) {
    58  	type fields struct{}
    59  	type args struct {
    60  		timeMillis uint64
    61  		bucketSize uint64
    62  	}
    63  	tests := []struct {
    64  		name   string
    65  		fields fields
    66  		args   args
    67  		want   uint64
    68  	}{
    69  		{
    70  			name:   "TestCalculateStartTime",
    71  			fields: fields{},
    72  			args: args{
    73  				timeMillis: 1576296044907,
    74  				bucketSize: BucketSize,
    75  			},
    76  			want: 1576296044500,
    77  		},
    78  	}
    79  	for _, tt := range tests {
    80  		t.Run(tt.name, func(t *testing.T) {
    81  			if got := calculateStartTime(tt.args.timeMillis, tt.args.bucketSize); got != tt.want {
    82  				t.Errorf("calculateStartTime() = %v, want %v", got, tt.want)
    83  			}
    84  		})
    85  	}
    86  }
    87  
    88  func TestGetBucketOfTime(t *testing.T) {
    89  	now := uint64(1596199310000)
    90  	s := &LeapArray{
    91  		bucketSize:   BucketSize,
    92  		n:            N,
    93  		intervalSize: IntervalSize,
    94  		array:        NewAtomicArrayWithTime(N, BucketSize, now, &Mock{}),
    95  		mu:           mutex.Mutex{},
    96  	}
    97  	got, err := s.getBucketOfTime(now+801, new(Mock))
    98  	if err != nil {
    99  		t.Errorf("getBucketOfTime() error = %v\n", err)
   100  		return
   101  	}
   102  	if got.Start != now+500 {
   103  		t.Errorf("BucketStart = %v, want %v", got.Start, now+500)
   104  	}
   105  	if !reflect.DeepEqual(got, s.array.getBucket(1)) {
   106  		t.Errorf("getBucketOfTime() = %v, want %v", got, s.array.getBucket(1))
   107  	}
   108  }
   109  
   110  func TestGetValueOfTime(t *testing.T) {
   111  	type fields struct {
   112  		bucketSize   uint64
   113  		n            uint64
   114  		intervalSize uint64
   115  		array        *AtomicArray
   116  	}
   117  	type args struct {
   118  		timeMillis uint64
   119  	}
   120  	tests := []struct {
   121  		name    string
   122  		fields  fields
   123  		args    args
   124  		want    *Bucket
   125  		wantErr bool
   126  	}{
   127  		{
   128  			name: "TestGetValueOfTime",
   129  			fields: fields{
   130  				bucketSize:   BucketSize,
   131  				n:            N,
   132  				intervalSize: IntervalSize,
   133  				array:        NewAtomicArrayWithTime(N, BucketSize, uint64(1596199310000), &Mock{}),
   134  			},
   135  			args: args{
   136  				timeMillis: 1576296049907,
   137  			},
   138  			want:    nil,
   139  			wantErr: false,
   140  		},
   141  	}
   142  	// override start time
   143  	start := uint64(1576296040000)
   144  	for idx := (uint64)(0); idx < tests[0].fields.array.length; idx++ {
   145  		ww := tests[0].fields.array.getBucket(idx)
   146  		ww.Start = start
   147  		start += 500
   148  	}
   149  
   150  	for _, tt := range tests {
   151  		t.Run(tt.name, func(t *testing.T) {
   152  			s := &LeapArray{
   153  				bucketSize:   tt.fields.bucketSize,
   154  				n:            tt.fields.n,
   155  				intervalSize: tt.fields.intervalSize,
   156  				array:        tt.fields.array,
   157  				mu:           mutex.Mutex{},
   158  			}
   159  			got := s.getValueOfTime(tt.args.timeMillis)
   160  			for _, g := range got {
   161  				find := false
   162  				for i := (uint64)(0); i < tests[0].fields.array.length; i++ {
   163  					w := tests[0].fields.array.getBucket(i)
   164  					if w.Start == g.Start {
   165  						find = true
   166  						break
   167  					}
   168  				}
   169  				if !find {
   170  					t.Errorf("getValueOfTime() fail")
   171  				}
   172  			}
   173  		})
   174  	}
   175  }
   176  
   177  func TestIsDisable(t *testing.T) {
   178  	type fields struct {
   179  		bucketSize   uint64
   180  		n            uint64
   181  		intervalSize uint64
   182  		array        *AtomicArray
   183  	}
   184  	type args struct {
   185  		startTime uint64
   186  		b         *Bucket
   187  	}
   188  	tests := []struct {
   189  		name   string
   190  		fields fields
   191  		args   args
   192  		want   bool
   193  	}{
   194  		{
   195  			name: "TestIsDisable",
   196  			fields: fields{
   197  				bucketSize:   BucketSize,
   198  				n:            N,
   199  				intervalSize: IntervalSize,
   200  				array:        NewAtomicArrayWithTime(N, BucketSize, uint64(1596199310000), &Mock{}),
   201  			},
   202  			args: args{
   203  				startTime: 1576296044907,
   204  				b: &Bucket{
   205  					Start: 1576296004907,
   206  				},
   207  			},
   208  			want: true,
   209  		},
   210  	}
   211  
   212  	for _, tt := range tests {
   213  		t.Run(tt.name, func(t *testing.T) {
   214  			la := &LeapArray{
   215  				bucketSize:   tt.fields.bucketSize,
   216  				n:            tt.fields.n,
   217  				intervalSize: tt.fields.intervalSize,
   218  				array:        tt.fields.array,
   219  				mu:           mutex.Mutex{},
   220  			}
   221  			if got := la.isDisable(tt.args.startTime, tt.args.b); got != tt.want {
   222  				t.Errorf("isDisable() = %v, want %v", got, tt.want)
   223  			}
   224  		})
   225  	}
   226  }
   227  
   228  func TestNewLeapArray(t *testing.T) {
   229  	t.Run("TestNewLeapArray", func(t *testing.T) {
   230  		_, err := NewLeapArray(N, IntervalSize, &Mock{})
   231  		assert.Nil(t, err)
   232  	})
   233  
   234  	t.Run("TestNewLeapArrayNil", func(t *testing.T) {
   235  		leapArray, err := NewLeapArray(N, IntervalSize, nil)
   236  		assert.Nil(t, leapArray)
   237  		assert.Error(t, err, ErrBucketBuilderIsNil)
   238  	})
   239  
   240  	t.Run("TestNewLeapArrayInvalidParameters", func(t *testing.T) {
   241  		leapArray, err := NewLeapArray(30, IntervalSize, nil)
   242  		assert.Nil(t, leapArray)
   243  		assert.Error(t, err, ErrWindowNotSegmentation)
   244  	})
   245  }