//go:build !race
// +build !race

/*
 * Copyright 2021 CloudWeGo Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package limiter

import (
	"context"
	"math"
	"sync"
	"sync/atomic"
	"testing"
	"time"

	"github.com/cloudwego/kitex/internal/test"
)

func TestQPSLimiter(t *testing.T) {
	qps := 1000
	interval := time.Second / 100
	ctx := context.Background()
	limiter := NewQPSLimiter(interval, qps)

	// case1: init
	max, cur, itv := limiter.Status(ctx)
	test.Assert(t, max == qps)
	test.Assert(t, cur == int(float64(qps)/(time.Second.Seconds()/interval.Seconds())), cur)
	test.Assert(t, itv == interval)

	// case2: get tokens with parallel 3 and QPS is 1000
	concurrent := 3
	var wg sync.WaitGroup
	wg.Add(concurrent)
	var count, stopFlag int32
	now := time.Now()
	for i := 0; i < concurrent; i++ {
		go func() {
			for atomic.LoadInt32(&stopFlag) == 0 {
				if limiter.Acquire(ctx) {
					atomic.AddInt32(&count, 1)
				}
			}
			wg.Done()
		}()
	}
	time.AfterFunc(time.Second*2, func() {
		atomic.StoreInt32(&stopFlag, 1)
	})
	wg.Wait()
	actualQPS := count / int32(time.Since(now).Seconds())
	delta := math.Abs(float64(actualQPS - int32(qps)))
	// the diff range need be < 2%
	test.Assert(t, delta < float64(qps)*0.02, delta)

	// case3: UpdateQPSLimit and get Status
	qps = 10000
	count = 0
	interval = time.Second / 50
	limiter.(*qpsLimiter).UpdateQPSLimit(interval, qps)
	max, _, itv = limiter.Status(ctx)
	test.Assert(t, max == qps)
	test.Assert(t, limiter.(*qpsLimiter).once == int32(float64(qps)/(time.Second.Seconds()/interval.Seconds())), limiter.(*qpsLimiter).once)
	test.Assert(t, itv == interval)
	wg.Add(concurrent)
	stopFlag = 0
	now = time.Now()
	for i := 0; i < concurrent; i++ {
		go func() {
			for atomic.LoadInt32(&stopFlag) == 0 {
				if limiter.Acquire(ctx) {
					atomic.AddInt32(&count, 1)
				}
			}
			wg.Done()
		}()
	}
	time.AfterFunc(time.Second*2, func() {
		atomic.StoreInt32(&stopFlag, 1)
	})
	wg.Wait()
	actualQPS = count / int32(time.Since(now).Seconds())
	delta = math.Abs(float64(actualQPS - int32(qps)))
	// the diff range need be < 5%, the range is larger because the interval config is larger
	test.Assert(t, delta < float64(qps)*0.05, delta, actualQPS)

	// case4: UpdateLimit and get Status
	qps = 300
	count = 0
	limiter.(*qpsLimiter).UpdateLimit(qps)
	time.Sleep(interval)
	max, _, itv = limiter.Status(ctx)
	test.Assert(t, max == qps)
	test.Assert(t, limiter.(*qpsLimiter).once == int32(float64(qps)/(time.Second.Seconds()/interval.Seconds())), limiter.(*qpsLimiter).once)
	test.Assert(t, itv == interval)
	wg.Add(concurrent)
	stopFlag = 0
	now = time.Now()
	for i := 0; i < concurrent; i++ {
		go func() {
			for atomic.LoadInt32(&stopFlag) == 0 {
				if limiter.Acquire(ctx) {
					atomic.AddInt32(&count, 1)
				}
			}
			wg.Done()
		}()
	}
	time.AfterFunc(time.Second*2, func() {
		atomic.StoreInt32(&stopFlag, 1)
	})
	wg.Wait()
	actualQPS = count / int32(time.Since(now).Seconds())
	delta = math.Abs(float64(actualQPS - int32(qps)))
	// the diff range need be < 5%, the range is larger because the interval config is larger
	test.Assert(t, delta < float64(qps)*0.05, delta, actualQPS)

	// case5: UpdateLimit zero
	qps = 0
	count = 0
	limiter.(*qpsLimiter).UpdateLimit(qps)
	time.Sleep(interval)
	max, _, itv = limiter.Status(ctx)
	test.Assert(t, max == qps)
	test.Assert(t, limiter.(*qpsLimiter).once == int32(float64(qps)/(time.Second.Seconds()/interval.Seconds())), limiter.(*qpsLimiter).once)
	test.Assert(t, itv == interval)
	wg.Add(concurrent)
	var failedCount int32
	stopFlag = 0
	for i := 0; i < concurrent; i++ {
		go func() {
			for atomic.LoadInt32(&stopFlag) == 0 {
				if !limiter.Acquire(ctx) {
					atomic.AddInt32(&failedCount, 1)
				}
			}
			wg.Done()
		}()
	}
	time.AfterFunc(time.Second*2, func() {
		atomic.StoreInt32(&stopFlag, 1)
	})
	wg.Wait()
	test.Assert(t, failedCount == 0)
}

func TestCalcOnce(t *testing.T) {
	ret := calcOnce(10*time.Millisecond, 100)
	test.Assert(t, ret > 0)

	// interval > time.Second
	ret = calcOnce(time.Minute, 100)
	test.Assert(t, ret == 100)

	// limit < 0
	ret = calcOnce(time.Second, -100)
	test.Assert(t, ret == 0)
}
