package routinepool

import (
	"math/rand"
	"testing"

	"git.rosy.net.cn/baseapi"
	"go.uber.org/zap"

	"git.rosy.net.cn/baseapi/utils"
)

var (
	sugarLogger *zap.SugaredLogger
)

func init() {
	logger, _ := zap.NewDevelopment()
	sugarLogger = logger.Sugar()
	baseapi.Init(sugarLogger)
	rand.Seed(utils.GetCurTimestamp())
}

func TestCallFun(t *testing.T) {
	pool := New(1000, 1000)
	for i := 0; i < 200; i++ {
		x := rand.Int()
		pool.CallFun(func() {
			sugarLogger.Debug(x)
		}, utils.Int2Str(x))
	}
	pool.CallFun(func() {
		sugarLogger.Debug(15)
	}, utils.Int2Str(15))
	pool.CallFun(func() {
		sugarLogger.Debug(15)
	}, utils.Int2Str(15))
	// time.Sleep(2 * time.Second)
}

func getClosure(b int) func() {
	a := b
	return func() {
		sugarLogger.Debug(a)
	}
}
func TestClosure(t *testing.T) {
	closures := make([]func(), 10)
	for i := 0; i < 10; i++ {
		closures[i] = getClosure(i)
	}
	for i := 0; i < 10; i++ {
		handler := closures[rand.Intn(10)]
		sugarLogger.Debug(handler)
		handler()
	}
}
