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

Improve Testing Coverage #383

Merged
merged 1 commit into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 5 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
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
6 changes: 4 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 All @@ -18,6 +19,7 @@ func TestResolver(t *testing.T) {
t: t,
},
}
assert.Equal(t, "test", builder.Scheme())
targetName := "test:///hello"
target, err := parseTarget(targetName)
assert.NoError(t, err)
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)
}
74 changes: 74 additions & 0 deletions client/ehttp/interceptor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package ehttp

import (
"context"
"net/url"
"strings"
"testing"
"time"

"github.com/go-resty/resty/v2"
"github.com/stretchr/testify/assert"

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

func TestLogAccess(t *testing.T) {
name := "test"
config := &Config{}
logger := &elog.Component{}
u, err := url.Parse("https://hello.com/xxx")
assert.NoError(t, err)
ctx := context.WithValue(context.Background(), urlKey{}, u)
req := resty.New().R().SetContext(ctx)
res := &resty.Response{}
logAccess(name, config, logger, req, res, err)
assert.NoError(t, err)
}

func TestBeg(t *testing.T) {
ctx := context.Background()
now := time.Now()
ctx = context.WithValue(ctx, begKey{}, now)

result := beg(ctx)
assert.Equal(t, now, result)
}

type CustomResolver struct {
Address string
}

func (r *CustomResolver) GetAddr() string {
return r.Address
}

func TestFixedInterceptor(t *testing.T) {
name := "test"
config := &Config{}
logger := &elog.Component{}
builder := &CustomResolver{Address: "https://test.com"}

client := resty.New()
request := client.R()
request.SetContext(context.Background())
request.URL = "https://hello.com/world"
middleware, _, _ := fixedInterceptor(name, config, logger, builder)

// case 1
config.Addr = ""
err := middleware(client, request)
assert.NoError(t, err)

// case 2
config.Addr = "https://xxxxx.com/xxx"
err = middleware(client, request)
assert.NoError(t, err)
assert.Equal(t, "https://test.com", client.HostURL)
}

func TestFileWithLineNum(t *testing.T) {
file := "/usr/local/go/src/testing/testing.go"
got := fileWithLineNum()
assert.True(t, true, strings.HasPrefix(got, file))
}
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)
}