github.com/erda-project/erda-infra@v1.0.9/examples/service/server/helloworld/greeter.service_test.go (about) 1 // Copyright (c) 2021 Terminus, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package example 16 17 import ( 18 "context" 19 "reflect" 20 "testing" 21 22 "github.com/erda-project/erda-infra/base/servicehub" 23 "github.com/erda-project/erda-infra/examples/service/protocol/pb" 24 ) 25 26 func Test_greeterService_SayHello(t *testing.T) { 27 type args struct { 28 ctx context.Context 29 req *pb.HelloRequest 30 } 31 tests := []struct { 32 name string 33 service string 34 config string 35 args args 36 wantResp *pb.HelloResponse 37 wantErr bool 38 }{ 39 // TODO: Add test cases. 40 // { 41 // "case 1", 42 // "erda.infra.example.GreeterService", 43 // `erda.infra.example:`, 44 // args{ 45 // context.TODO(), 46 // &pb.HelloRequest{ 47 // // TODO: setup fields 48 // }, 49 // }, 50 // &pb.HelloResponse{ 51 // // TODO: setup fields. 52 // }, 53 // false, 54 // }, 55 } 56 for _, tt := range tests { 57 t.Run(tt.name, func(t *testing.T) { 58 hub := servicehub.New() 59 events := hub.Events() 60 go func() { 61 hub.RunWithOptions(&servicehub.RunOptions{Content: tt.config}) 62 }() 63 err := <-events.Started() 64 if err != nil { 65 t.Error(err) 66 return 67 } 68 srv := hub.Service(tt.service).(pb.GreeterServiceServer) 69 got, err := srv.SayHello(tt.args.ctx, tt.args.req) 70 if (err != nil) != tt.wantErr { 71 t.Errorf("greeterService.SayHello() error = %v, wantErr %v", err, tt.wantErr) 72 return 73 } 74 if !reflect.DeepEqual(got, tt.wantResp) { 75 t.Errorf("greeterService.SayHello() = %v, want %v", got, tt.wantResp) 76 } 77 }) 78 } 79 }