Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

测试用例优化 #380

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 40 additions & 4 deletions client/egrpc/component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import (
"net"
"testing"

"github.com/gotomicro/ego/core/elog"
"github.com/gotomicro/ego/internal/test/errcode"
"github.com/gotomicro/ego/internal/test/helloworld"
"github.com/gotomicro/ego/server/egrpc"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc"
"google.golang.org/grpc/test/bufconn"
"google.golang.org/protobuf/proto"

"github.com/gotomicro/ego/core/elog"
"github.com/gotomicro/ego/internal/test/errcode"
"github.com/gotomicro/ego/internal/test/helloworld"
"github.com/gotomicro/ego/server/egrpc"
)

var svc *egrpc.Component
Expand Down Expand Up @@ -82,3 +83,38 @@ func (g Greeter) SayHello(context context.Context, request *helloworld.HelloRequ
func bufDialer(context.Context, string) (net.Conn, error) {
return svc.Listener().(*bufconn.Listener).Dial()
}

// func Test_newComponent1(t *testing.T) {
// type args struct {
// name string
// config *Config
// logger *elog.Component
// }
// // logger := &elog.Component{}
// tests := []struct {
// name string
// args args
// want *Component
// }{
// {
// // TODO: Add test cases.
// name: "",
// args: args{
// // name: "",
// // config: &Config{
// // EnableOfficialGrpcLog: false,
// // EnableBlock: false,
// // EnableWithInsecure: true,
// // },
// // logger: logger,
// },
// want: &Component{}, // name: "Case1", config: DefaultConfig(), logger: logger, err: nil
// },
// }
//
// for _, tt := range tests {
// t.Run(tt.name, func(t *testing.T) {
// assert.Equalf(t, tt.want, newComponent(tt.args.name, tt.args.config, tt.args.logger), "newComponent(%v, %v, %v)", tt.args.name, tt.args.config, tt.args.logger)
// })
// }
// }
15 changes: 8 additions & 7 deletions client/egrpc/interceptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@ package egrpc
import (
"bytes"
"context"
"io/ioutil"
"io"
"log"
"net/http/httptest"
"os"
"testing"
"time"

"github.com/gotomicro/ego/core/util/xtime"
"github.com/gotomicro/ego/internal/test/helloworld"
"github.com/gotomicro/ego/internal/tools"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/test/bufconn"

"github.com/gotomicro/ego/core/util/xtime"
"github.com/gotomicro/ego/internal/test/helloworld"
"github.com/gotomicro/ego/internal/tools"
)

func Test_customHeader(t *testing.T) {
Expand Down Expand Up @@ -138,7 +139,7 @@ func TestPrometheusUnary(t *testing.T) {
if err != nil {
t.Fatal(err)
}
text, err := ioutil.ReadAll(res.Body)
text, err := io.ReadAll(res.Body)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -184,8 +185,8 @@ type GreeterHeader struct {
// SayHello ...
func (g GreeterHeader) SayHello(context context.Context, request *helloworld.HelloRequest) (*helloworld.HelloResponse, error) {
appName := tools.GrpcHeaderValue(context, "app")
//cpu := tools.GrpcHeaderValue(context, "enable-cpu-usage")
//assert.Equal(g.t, "true", cpu)
// cpu := tools.GrpcHeaderValue(context, "enable-cpu-usage")
// assert.Equal(g.t, "true", cpu)
assert.Equal(g.t, "egrpc.test", appName)

return &helloworld.HelloResponse{
Expand Down
5 changes: 3 additions & 2 deletions client/egrpc/resolver/resolver_registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (
"net/url"
"testing"

"github.com/gotomicro/ego/core/eregistry"
"github.com/gotomicro/ego/server"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc/resolver"

"github.com/gotomicro/ego/core/eregistry"
"github.com/gotomicro/ego/server"
)

func TestResolver(t *testing.T) {
Expand Down
21 changes: 21 additions & 0 deletions client/ehttp/component_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package ehttp

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/gotomicro/ego/core/elog"
)

func TestNewComponent(t *testing.T) {
// Normal case
t.Run("Normal case", func(t *testing.T) {
config := &Config{Addr: "http://hello.com"}
logger := elog.DefaultLogger
component := newComponent("hello", config, logger)
assert.Equal(t, "hello", component.name)
})

// Other case...
}
33 changes: 33 additions & 0 deletions client/ehttp/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package ehttp

import (
"reflect"
"runtime"
"testing"
"time"

"github.com/stretchr/testify/assert"

"github.com/gotomicro/ego/core/util/xtime"
)

func Test_DefaultConfig(t *testing.T) {
assert.True(t, reflect.DeepEqual(&Config{
Addr: "",
Debug: false,
RawDebug: false,
ReadTimeout: xtime.Duration("2s"),
SlowLogThreshold: xtime.Duration("500ms"),
IdleConnTimeout: 90 * time.Second,
MaxIdleConns: 100,
MaxIdleConnsPerHost: runtime.GOMAXPROCS(0) + 1,
EnableTraceInterceptor: true,
EnableKeepAlives: true,
EnableAccessInterceptor: false,
EnableAccessInterceptorRes: false,
EnableMetricInterceptor: false,
PathRelabel: nil,
cookieJar: nil,
httpClient: nil,
}, DefaultConfig()))
}
5 changes: 5 additions & 0 deletions client/ehttp/config_test/conf.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[test]
"name" = "hello"

[test1]
"name" = "world"
20 changes: 20 additions & 0 deletions client/ehttp/container_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package ehttp

import (
"os"
"testing"

"github.com/BurntSushi/toml"
"github.com/stretchr/testify/assert"

"github.com/gotomicro/ego/core/econf"
)

func TestLoad(t *testing.T) {
file, err := os.Open("./config_test/conf.toml")
assert.NoError(t, err)
err = econf.LoadFromReader(file, toml.Unmarshal)
assert.NoError(t, err)
container := Load("test").Build().name
assert.Equal(t, "test", container)
}
53 changes: 53 additions & 0 deletions client/ehttp/options_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package ehttp

import (
"reflect"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestOptions(t *testing.T) {
c := &Container{
config: &Config{},
}
expectedAddr := "127.0.0.1:8080"
expectedReadTimeOut := time.Duration(5)
expectedSlowLogThreshold := time.Duration(5)
expectedIdleConnTimeOut := time.Duration(5)

WithAddr(expectedAddr)(c)
WithDebug(true)(c)
WithRawDebug(false)(c)
WithReadTimeout(expectedReadTimeOut)(c)
WithSlowLogThreshold(expectedSlowLogThreshold)(c)
WithIdleConnTimeout(expectedIdleConnTimeOut)(c)
WithMaxIdleConns(3)(c)
WithMaxIdleConnsPerHost(3)(c)
WithEnableTraceInterceptor(true)(c)
WithEnableKeepAlives(true)(c)
WithEnableMetricInterceptor(true)(c)
WithEnableAccessInterceptor(true)(c)
WithEnableAccessInterceptorRes(true)(c)
WithPathRelabel("hello", "test")(c)
WithJar(nil)(c)
WithHTTPClient(nil)(c)

assert.Equal(t, expectedAddr, c.config.Addr)
assert.Equal(t, true, c.config.Debug)
assert.Equal(t, false, c.config.RawDebug)
assert.Equal(t, expectedReadTimeOut, c.config.ReadTimeout)
assert.Equal(t, expectedSlowLogThreshold, c.config.SlowLogThreshold)
assert.Equal(t, expectedIdleConnTimeOut, c.config.IdleConnTimeout)
assert.Equal(t, 3, c.config.MaxIdleConns)
assert.Equal(t, 3, c.config.MaxIdleConnsPerHost)
assert.Equal(t, true, c.config.EnableTraceInterceptor)
assert.Equal(t, true, c.config.EnableKeepAlives)
assert.Equal(t, true, c.config.EnableMetricInterceptor)
assert.Equal(t, true, c.config.EnableAccessInterceptor)
assert.Equal(t, true, c.config.EnableAccessInterceptorRes)
reflect.DeepEqual(Relabel{Match: "hello", Replacement: "test"}, c.config.PathRelabel)
assert.Equal(t, nil, c.config.cookieJar)
reflect.DeepEqual(nil, c.config.httpClient)
}
3 changes: 2 additions & 1 deletion core/econf/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import (
"os"

"github.com/BurntSushi/toml"
"github.com/gotomicro/ego/core/econf"
"gopkg.in/yaml.v3"

"github.com/gotomicro/ego/core/econf"
)

var defaultScheme = "file"
Expand Down
61 changes: 61 additions & 0 deletions core/econf/manager/manager_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package manager

import (
"reflect"
"testing"

"github.com/gotomicro/ego/core/econf"
)

func TestNewDataSource(t *testing.T) {
type args struct {
configAddr string
watch bool
}
tests := []struct {
name string
args args
want econf.DataSource
want1 econf.Unmarshaller
want2 econf.ConfigType
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, got1, got2, err := NewDataSource(tt.args.configAddr, tt.args.watch)
if (err != nil) != tt.wantErr {
t.Errorf("NewDataSource() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewDataSource() got = %v, want %v", got, tt.want)
}
if !reflect.DeepEqual(got1, tt.want1) {
t.Errorf("NewDataSource() got1 = %v, want %v", got1, tt.want1)

Check failure on line 36 in core/econf/manager/manager_test.go

View workflow job for this annotation

GitHub Actions / build

(*testing.common).Errorf format %v arg got1 is a func value, not called

Check failure on line 36 in core/econf/manager/manager_test.go

View workflow job for this annotation

GitHub Actions / lint

printf: (*testing.common).Errorf format %v arg got1 is a func value, not called (govet)
}
if got2 != tt.want2 {
t.Errorf("NewDataSource() got2 = %v, want %v", got2, tt.want2)
}
})
}
}

func TestRegister(t *testing.T) {
type args struct {
scheme string
creator econf.DataSource
}
tests := []struct {
name string
args args
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Register(tt.args.scheme, tt.args.creator)
})
}
}
4 changes: 4 additions & 0 deletions core/econf/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@ func TestWithTagName(t *testing.T) {
require.Nil(t, err)
assert.Equal(t, "yaml", econf.GetOptionTagName())
assert.Equal(t, true, econf.GetOptionWeaklyTypedInput())

err = v.LoadFromDataSource(provider, parser, econf.WithSquash(true))
require.Nil(t, err)
assert.Equal(t, true, econf.WithSquash(true))
}
24 changes: 21 additions & 3 deletions core/eregistry/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@ import (
"reflect"
"testing"

"github.com/gotomicro/ego/server"
"github.com/stretchr/testify/assert"

"github.com/gotomicro/ego/server"
)

func TestEndpoints_DeepCopy(t *testing.T) {
in := newEndpoints()
if in == nil {
return
}
in.DeepCopy()
assert.True(t, reflect.DeepEqual(in, in.DeepCopy()))
// assert.True(t, reflect.DeepEqual(in, in.DeepCopy()))
assert.Equal(t, in, in.DeepCopy())
var in2 *Endpoints
assert.Nil(t, in2.DeepCopy())

Expand All @@ -20,8 +25,21 @@ func TestEndpoints_DeepCopy(t *testing.T) {
func TestEndpoints_DeepCopyInfo(t *testing.T) {
in := newEndpoints()
out := newEndpoints()
for key, info := range in.Nodes {
out.Nodes[key] = info
}
for key, config := range in.RouteConfigs {
out.RouteConfigs[key] = config
}
for key, config := range in.ConsumerConfigs {
out.ConsumerConfigs[key] = config
}
for key, config := range in.ProviderConfigs {
out.ProviderConfigs[key] = config
}
in.deepCopyInfo(out)
assert.True(t, reflect.DeepEqual(in, out))
// assert.True(t, reflect.DeepEqual(in, out))
assert.Equal(t, in, out)
}

func Test_newEndpoints(t *testing.T) {
Expand Down
Loading
Loading