/*
 * 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 connpool

import (
	"testing"
	"time"

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

func TestCheckPoolConfig(t *testing.T) {
	cfg := CheckPoolConfig(IdleConfig{MaxIdleTimeout: 0})
	test.Assert(t, cfg.MaxIdleTimeout == defaultMaxIdleTimeout)
	cfg = CheckPoolConfig(IdleConfig{MaxIdleTimeout: time.Millisecond})
	test.Assert(t, cfg.MaxIdleTimeout == minMaxIdleTimeout)

	// minIdle
	cfg = CheckPoolConfig(IdleConfig{MinIdlePerAddress: -1})
	test.Assert(t, cfg.MinIdlePerAddress == 0)
	test.Assert(t, cfg.MaxIdlePerAddress == 1)
	test.Assert(t, cfg.MaxIdleGlobal == defaultMaxIdleGlobal)
	cfg = CheckPoolConfig(IdleConfig{MinIdlePerAddress: 1})
	test.Assert(t, cfg.MinIdlePerAddress == 1)
	cfg = CheckPoolConfig(IdleConfig{MinIdlePerAddress: maxMinIdlePerAddress + 1})
	test.Assert(t, cfg.MinIdlePerAddress == maxMinIdlePerAddress)

	// maxIdle
	cfg = CheckPoolConfig(IdleConfig{MaxIdlePerAddress: 1, MinIdlePerAddress: 2})
	test.Assert(t, cfg.MaxIdlePerAddress == 2)
	cfg = CheckPoolConfig(IdleConfig{MaxIdlePerAddress: -1})
	test.Assert(t, cfg.MaxIdlePerAddress == 1)

	// maxIdleGlobal
	cfg = CheckPoolConfig(IdleConfig{MaxIdleGlobal: 9, MaxIdlePerAddress: 10, MinIdlePerAddress: 1})
	test.Assert(t, cfg.MaxIdleGlobal == 10)
}
