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

import (
	"errors"
	"net"
	"testing"

	"github.com/cloudwego/kitex/internal/mocks"
	"github.com/cloudwego/kitex/internal/test"
	"github.com/cloudwego/kitex/pkg/remote"
	"github.com/cloudwego/kitex/pkg/utils"
)

func TestServerStart(t *testing.T) {
	isCreateListener := false
	isBootstrapped := false
	var ln net.Listener
	transSvr := &mocks.MockTransServer{
		CreateListenerFunc: func(addr net.Addr) (listener net.Listener, err error) {
			isCreateListener = true
			ln, err = net.Listen("tcp", "localhost:8888")
			return ln, err
		},
		BootstrapServerFunc: func(net.Listener) (err error) {
			isBootstrapped = true
			return nil
		},
		ShutdownFunc: func() (err error) {
			if ln != nil {
				ln.Close()
			}
			return nil
		},
	}
	opt := &remote.ServerOption{
		Address:            utils.NewNetAddr("tcp", "test"),
		TransServerFactory: mocks.NewMockTransServerFactory(transSvr),
	}
	transHdrl := &mocks.MockSvrTransHandler{}
	svr, err := NewServer(opt, transHdrl)
	test.Assert(t, err == nil, err)

	err = <-svr.Start()
	test.Assert(t, err == nil, err)
	test.Assert(t, isBootstrapped)
	test.Assert(t, isCreateListener)

	err = svr.Stop()
	test.Assert(t, err == nil, err)
}

func TestServerStartListenErr(t *testing.T) {
	mockErr := errors.New("test")
	transSvr := &mocks.MockTransServer{
		CreateListenerFunc: func(addr net.Addr) (listener net.Listener, err error) {
			return nil, mockErr
		},
	}
	opt := &remote.ServerOption{
		Address:            utils.NewNetAddr("tcp", "test"),
		TransServerFactory: mocks.NewMockTransServerFactory(transSvr),
	}
	transHdrl := &mocks.MockSvrTransHandler{}
	svr, err := NewServer(opt, transHdrl)
	test.Assert(t, err == nil, err)

	err = <-svr.Start()
	test.Assert(t, err != nil, err)
	test.Assert(t, err == mockErr)

	err = svr.Stop()
	test.Assert(t, err == nil, err)
}
