github.com/cloudwego/kitex@v0.9.0/server/genericserver/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 genericserver 18 19 import ( 20 "context" 21 "net" 22 "strings" 23 "testing" 24 "time" 25 26 "github.com/cloudwego/kitex/internal/test" 27 "github.com/cloudwego/kitex/pkg/generic" 28 "github.com/cloudwego/kitex/server" 29 ) 30 31 // TestNewServer tests the creation of a new server 32 func TestNewServer(t *testing.T) { 33 hostPort := test.GetLocalAddress() 34 addr, _ := net.ResolveTCPAddr("tcp", hostPort) 35 g := generic.BinaryThriftGeneric() 36 svr := NewServer(new(mockImpl), g, server.WithServiceAddr(addr), server.WithExitWaitTime(time.Microsecond*10)) 37 time.AfterFunc(time.Millisecond*500, func() { 38 err := svr.Stop() 39 test.Assert(t, err == nil, err) 40 }) 41 err := svr.Run() 42 test.Assert(t, err == nil, err) 43 } 44 45 // TestNewServerWithServiceInfo tests the creation of a new server with service info 46 func TestNewServerWithServiceInfo(t *testing.T) { 47 g := generic.BinaryThriftGeneric() 48 test.PanicAt(t, func() { 49 NewServerWithServiceInfo(new(mockImpl), g, nil) 50 }, func(err interface{}) bool { 51 if errMsg, ok := err.(error); ok { 52 return strings.Contains(errMsg.Error(), "svcInfo is nil.") 53 } 54 return true 55 }) 56 57 test.PanicAt(t, func() { 58 NewServerWithServiceInfo(nil, g, generic.ServiceInfo(g.PayloadCodecType())) 59 }, func(err interface{}) bool { 60 if errMsg, ok := err.(error); ok { 61 return strings.Contains(errMsg.Error(), "handler is nil.") 62 } 63 return true 64 }) 65 66 test.PanicAt(t, func() { 67 NewServerWithServiceInfo(nil, nil, nil) 68 }, func(err interface{}) bool { 69 if errMsg, ok := err.(string); ok { 70 return strings.Contains(errMsg, "invalid Generic") 71 } 72 return true 73 }) 74 } 75 76 // GenericServiceImpl ... 77 type mockImpl struct{} 78 79 // GenericCall ... 80 func (g *mockImpl) GenericCall(ctx context.Context, method string, request interface{}) (response interface{}, err error) { 81 return nil, nil 82 }