github.com/cloudwego/kitex@v0.9.0/pkg/remote/remotesvr/server_test.go (about)

     1  /*
     2   * Copyright 2021 CloudWeGo Authors
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package remotesvr
    18  
    19  import (
    20  	"context"
    21  	"errors"
    22  	"net"
    23  	"testing"
    24  
    25  	"github.com/cloudwego/kitex/internal/mocks"
    26  	"github.com/cloudwego/kitex/internal/test"
    27  	"github.com/cloudwego/kitex/pkg/remote"
    28  	"github.com/cloudwego/kitex/pkg/utils"
    29  )
    30  
    31  func TestServerStart(t *testing.T) {
    32  	isCreateListener := false
    33  	isBootstrapped := false
    34  	var ln net.Listener
    35  	transSvr := &mocks.MockTransServer{
    36  		CreateListenerFunc: func(addr net.Addr) (listener net.Listener, err error) {
    37  			isCreateListener = true
    38  			ln, err = net.Listen("tcp", ":8888")
    39  			return ln, err
    40  		},
    41  		BootstrapServerFunc: func(net.Listener) (err error) {
    42  			isBootstrapped = true
    43  			return nil
    44  		},
    45  		ShutdownFunc: func() (err error) {
    46  			if ln != nil {
    47  				ln.Close()
    48  			}
    49  			return nil
    50  		},
    51  	}
    52  	opt := &remote.ServerOption{
    53  		Address:            utils.NewNetAddr("tcp", "test"),
    54  		TransServerFactory: mocks.NewMockTransServerFactory(transSvr),
    55  	}
    56  	inkHdlrFunc := func(ctx context.Context, req, resp interface{}) (err error) {
    57  		return nil
    58  	}
    59  	transHdrl := &mocks.MockSvrTransHandler{}
    60  	svr, err := NewServer(opt, inkHdlrFunc, transHdrl)
    61  	test.Assert(t, err == nil, err)
    62  
    63  	err = <-svr.Start()
    64  	test.Assert(t, err == nil, err)
    65  	test.Assert(t, isBootstrapped)
    66  	test.Assert(t, isCreateListener)
    67  
    68  	err = svr.Stop()
    69  	test.Assert(t, err == nil, err)
    70  }
    71  
    72  func TestServerStartListenErr(t *testing.T) {
    73  	mockErr := errors.New("test")
    74  	transSvr := &mocks.MockTransServer{
    75  		CreateListenerFunc: func(addr net.Addr) (listener net.Listener, err error) {
    76  			return nil, mockErr
    77  		},
    78  	}
    79  	opt := &remote.ServerOption{
    80  		Address:            utils.NewNetAddr("tcp", "test"),
    81  		TransServerFactory: mocks.NewMockTransServerFactory(transSvr),
    82  	}
    83  	inkHdlrFunc := func(ctx context.Context, req, resp interface{}) (err error) {
    84  		return nil
    85  	}
    86  	transHdrl := &mocks.MockSvrTransHandler{}
    87  	svr, err := NewServer(opt, inkHdlrFunc, transHdrl)
    88  	test.Assert(t, err == nil, err)
    89  
    90  	err = <-svr.Start()
    91  	test.Assert(t, err != nil, err)
    92  	test.Assert(t, err == mockErr)
    93  
    94  	err = svr.Stop()
    95  	test.Assert(t, err == nil, err)
    96  }