/*
 * Copyright 2023 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 loadbalance

import (
	"context"
	"runtime"
	"sync"
	"testing"

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

func TestRoundRobinPicker(t *testing.T) {
	ctx := context.Background()
	insList := []discovery.Instance{
		discovery.NewInstance("tcp", "addr1", 10, nil),
		discovery.NewInstance("tcp", "addr2", 10, nil),
		discovery.NewInstance("tcp", "addr3", 10, nil),
	}
	roundRobinPickers := []Picker{
		newRoundRobinPicker(insList),
		newWeightedRoundRobinPicker(insList),
	}
	for _, picker := range roundRobinPickers {
		accessMap := map[string]int{}
		for i := 0; i < len(insList); i++ {
			node := picker.Next(ctx, nil)
			accessMap[node.Address().String()] += node.Weight()
			test.Assert(t, len(accessMap) == i+1, accessMap)
		}
		test.Assert(t, len(accessMap) == len(insList), accessMap)
		for i := 0; i < len(insList)*9; i++ {
			node := picker.Next(ctx, nil)
			accessMap[node.Address().String()] += node.Weight()
		}
		test.Assert(t, len(accessMap) == len(insList), accessMap)
		for _, v := range accessMap {
			test.Assert(t, v == 10*10, v)
		}
	}
}

func TestWeightedRoundRobinPicker(t *testing.T) {
	ctx := context.Background()
	insList := []discovery.Instance{
		discovery.NewInstance("tcp", "addr1", 10, nil),
		discovery.NewInstance("tcp", "addr2", 20, nil),
		discovery.NewInstance("tcp", "addr3", 30, nil),
	}
	picker := newWeightedRoundRobinPicker(insList)
	accessMap := map[string]int{}
	round := len(insList) * 1000
	doubleAccess := 0
	var lastNode discovery.Instance
	for i := 0; i < round; i++ {
		node := picker.Next(ctx, nil)
		accessMap[node.Address().String()] += 1
		if lastNode != nil && lastNode.Address() == node.Address() {
			doubleAccess++
		}
		lastNode = node
	}
	t.Logf("doubleAccess: %d", doubleAccess)
	test.Assert(t, len(accessMap) == len(insList), accessMap)
	test.Assert(t, accessMap["addr1"] == round/6, accessMap)
	test.Assert(t, accessMap["addr2"] == round/6*2, accessMap)
	test.Assert(t, accessMap["addr3"] == round/6*3, accessMap)

	wrrp := picker.(*WeightedRoundRobinPicker)
	test.Assert(t, wrrp.vcapacity == 6)
	test.Assert(t, len(wrrp.vnodes) == 6)
}

func TestWeightedRoundRobinPickerLargeInstances(t *testing.T) {
	ctx := context.Background()
	var insList []discovery.Instance
	insList = append(insList, makeNInstances(wrrVNodesBatchSize*5, 10)...)
	insList = append(insList, discovery.NewInstance("tcp", "nbalance", 100, nil))

	picker := newWeightedRoundRobinPicker(insList)
	concurrency := 2 + runtime.GOMAXPROCS(0)
	round := len(insList)
	var wg sync.WaitGroup
	for c := 0; c < concurrency; c++ {
		wg.Add(1)
		go func() {
			defer wg.Done()
			for i := 0; i < round; i++ {
				ins := picker.Next(ctx, nil)
				test.Assert(t, ins != nil, ins)
			}
		}()
	}
	wg.Wait()
}

func TestNextWrrNode(t *testing.T) {
	wrrNodes := []*wrrNode{
		{Instance: discovery.NewInstance("tcp", "addr1", 5, nil)},
		{Instance: discovery.NewInstance("tcp", "addr2", 1, nil)},
		{Instance: discovery.NewInstance("tcp", "addr3", 1, nil)},
	}

	// round 1
	node := nextWrrNode(wrrNodes)
	test.Assert(t, node.Address() == wrrNodes[0].Address())
	test.Assert(t, wrrNodes[0].current == -2, wrrNodes[0])
	test.Assert(t, wrrNodes[1].current == 1, wrrNodes[1])
	test.Assert(t, wrrNodes[2].current == 1, wrrNodes[2])

	// round 2
	node = nextWrrNode(wrrNodes)
	test.Assert(t, node.Address() == wrrNodes[0].Address())
	test.Assert(t, wrrNodes[0].current == -4, wrrNodes[0])
	test.Assert(t, wrrNodes[1].current == 2, wrrNodes[1])
	test.Assert(t, wrrNodes[2].current == 2, wrrNodes[2])

	// round 3
	node = nextWrrNode(wrrNodes)
	test.Assert(t, node.Address() == wrrNodes[1].Address())
	test.Assert(t, wrrNodes[0].current == 1, wrrNodes[0])
	test.Assert(t, wrrNodes[1].current == -4, wrrNodes[1])
	test.Assert(t, wrrNodes[2].current == 3, wrrNodes[2])

	// round 4
	node = nextWrrNode(wrrNodes)
	test.Assert(t, node.Address() == wrrNodes[0].Address())
	test.Assert(t, wrrNodes[0].current == -1, wrrNodes[0])
	test.Assert(t, wrrNodes[1].current == -3, wrrNodes[1])
	test.Assert(t, wrrNodes[2].current == 4, wrrNodes[2])
}
