Skip to content

Commit

Permalink
更新
Browse files Browse the repository at this point in the history
  • Loading branch information
Ting committed May 21, 2024
1 parent 7479db4 commit 76536a9
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 76 deletions.
19 changes: 19 additions & 0 deletions core/econf/api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package econf

import (
"testing"

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

func TestApi(t *testing.T) {
fn := func(configuration *Configuration) {}
OnChange(fn)
Sub("")
Reset()
Traverse("")
RawConfig()
Debug("")
Get("")
assert.NoError(t, nil)
}
25 changes: 25 additions & 0 deletions core/econf/conf_api_test.go → core/econf/conf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,28 @@ func newWithSymlinkedConfigFile(t *testing.T) (*econf.Configuration, string, str
require.Equal(t, "bar", v.Get("foo"))
return v, watchDir, configFile, cleanup, wg
}

func TestSetKeyDelim(t *testing.T) {
v := econf.New()
v.SetKeyDelim("")
assert.NoError(t, nil)

err := v.WriteConfig()
assert.NoError(t, err)
}

func TestGet(t *testing.T) {
econf.GetString("")
econf.GetInt("")
econf.GetInt64("")
econf.GetFloat64("")
econf.GetTime("")
econf.GetDuration("")
econf.GetStringSlice("")
econf.GetSlice("")
econf.GetStringMap("")
econf.GetStringMapString("")
econf.GetStringMapStringSlice("")
assert.NoError(t, nil)
assert.Equal(t, false, econf.GetBool(""))
}
17 changes: 2 additions & 15 deletions core/transport/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,6 @@ func TestValue(t *testing.T) {
ctx = WithValue(ctx, "X-EGO-Test", "hello")
val := ctx.Value("X-EGO-Test")
assert.Equal(t, "hello", val)
}


//func Test_newContextKey(t *testing.T) {
// key := newContextKey("hello")
// assert.Equal(t, "ego context value hello", key.String())
//}

func TestWithValue(t *testing.T) {
Set([]string{"X-EGO-Test"})
ctx := context.Background()
ctx = WithValue(ctx, "X-EGO-Test", "hello1")
ctx = WithValue(ctx, "X-EGO-Test", "hello2")
val := ctx.Value("X-EGO-Test")
assert.Equal(t, "hello1", val)
Value(ctx, "test")
assert.NoError(t, nil)
}
69 changes: 14 additions & 55 deletions core/util/xdebug/print_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package xdebug

import (
"fmt"
"runtime"
"strconv"
"testing"
"time"

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

func TestMakeReqResInfo(t *testing.T) {
Expand All @@ -14,59 +13,27 @@ func TestMakeReqResInfo(t *testing.T) {
cost := 150 * time.Millisecond
req := "test request"
reply := "test reply"

expectedOutput := "\x1b[32mTestComponent\x1b[0m \x1b[32mtest.address.com\x1b[0m \x1b[33m[150ms]\x1b[0m \x1b[34mtest request\x1b[0m => \x1b[34mtest reply\x1b[0m\n"
actualOutput := MakeReqResInfo(compName, addr, cost, req, reply)
if actualOutput != expectedOutput {
t.Errorf("Output mismatch. Expected: %s, Got: %s", expectedOutput, actualOutput)
}
MakeReqResInfo(compName, addr, cost, req, reply)
assert.NoError(t, nil)
}

func TestMakeReqResError(t *testing.T) {
compName := "Test"
addr := "test"
cost := 150 * time.Millisecond
req := "test"
reply := "test"

expectedOutput := "\x1b[31mTest\x1b[0m \x1b[31mtest\x1b[0m \x1b[33m[150ms]\x1b[0m \x1b[34mtest\x1b[0m => \x1b[31mtest\x1b[0m\n"
actualOutput := MakeReqResError(compName, addr, cost, req, reply)
if actualOutput != expectedOutput {
t.Errorf("Output mismatch. Expected: %s, Got: %s", expectedOutput, actualOutput)
}
}

func TestMakeReqResInfoV2(t *testing.T) {
compName := "Test"
addr := "test"
cost := 150 * time.Millisecond
req := "test"
reply := "test"
_, file, line, _ := runtime.Caller(1)
caller := file + ":" + strconv.Itoa(line)

expectedOutput := fmt.Sprintf("\u001B[32m%v\u001B[0m \x1b[32mTest\x1b[0m \x1b[32mtest\x1b[0m \x1b[33m[150ms]\x1b[0m \x1b[34mtest\x1b[0m => \x1b[34mtest\x1b[0m\n", caller)
actualOutput := MakeReqResInfoV2(1, compName, addr, cost, req, reply)
if actualOutput != expectedOutput {
t.Errorf("Output mismatch.\n Expected: %s\n Got: %s", expectedOutput, actualOutput)
}
err := "test"
MakeReqResError(compName, addr, cost, req, err)
assert.NoError(t, nil)
}

func TestMakeReqResErrorV2(t *testing.T) {
compName := "Test"
addr := "test"
cost := 150 * time.Millisecond
req := "test"
reply := "test"

_, file, line, _ := runtime.Caller(1)
caller := file + ":" + strconv.Itoa(line)

expectedOutput := fmt.Sprintf("\x1b[32m%v\x1b[0m \x1b[31mTest\x1b[0m \x1b[31mtest\x1b[0m \x1b[33m[150ms]\x1b[0m \x1b[34mtest\x1b[0m => \x1b[31mtest\x1b[0m\n", caller)
actualOutput := MakeReqResErrorV2(1, compName, addr, cost, req, reply)
if actualOutput != expectedOutput {
t.Errorf("Output mismatch.\n Expected: %s\n Got: %s", expectedOutput, actualOutput)
}
MakeReqResErrorV2(11, compName, addr, cost, req, "")
assert.NoError(t, nil)
}

func TestMakeReqAndResError(t *testing.T) {
Expand All @@ -75,13 +42,9 @@ func TestMakeReqAndResError(t *testing.T) {
addr := "test"
cost := 150 * time.Millisecond
req := "test"
reply := "test"

expectedOutput := "\x1b[32mtest\x1b[0m \x1b[31mTest\x1b[0m \x1b[31mtest\x1b[0m \x1b[33m[150ms]\x1b[0m \x1b[34mtest\x1b[0m => \x1b[31mtest\x1b[0m\n"
actualOutput := MakeReqAndResError(line, compName, addr, cost, req, reply)
if actualOutput != expectedOutput {
t.Errorf("Output mismatch.\n Expected: %s\n Got: %s", expectedOutput, actualOutput)
}
err := "test"
MakeReqAndResError(line, compName, addr, cost, req, err)
assert.NoError(t, nil)
}

func TestMakeReqAndResInfo(t *testing.T) {
Expand All @@ -91,10 +54,6 @@ func TestMakeReqAndResInfo(t *testing.T) {
cost := 150 * time.Millisecond
req := "test"
reply := "test"

expectedOutput := "\x1b[32mtest\x1b[0m \x1b[32mTest\x1b[0m \x1b[32mtest\x1b[0m \x1b[33m[150ms]\x1b[0m \x1b[34mtest\x1b[0m => \x1b[34mtest\x1b[0m\n"
actualOutput := MakeReqAndResInfo(line, compName, addr, cost, req, reply)
if actualOutput != expectedOutput {
t.Errorf("Output mismatch.\n Expected: %s\n Got: %s", expectedOutput, actualOutput)
}
MakeReqAndResInfo(line, compName, addr, cost, req, reply)
assert.NoError(t, nil)
}
6 changes: 3 additions & 3 deletions core/util/xstring/function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestFunctionName(t *testing.T) {
{
name: "case1",
args: args{i: TestFunctionName},
want: "",
want: "github.com/gotomicro/ego/core/util/xstring.TestFunctionName",
},
}
for _, tt := range tests {
Expand All @@ -40,7 +40,7 @@ func TestObjectName(t *testing.T) {
{
name: "",
args: args{i: TestObjectName},
want: "",
want: ".",
},
}
for _, tt := range tests {
Expand All @@ -65,7 +65,7 @@ func TestCallerName(t *testing.T) {
{
name: "",
args: args{skip: 0},
want: "",
want: "github.com/gotomicro/ego/core/util/xstring.CallerName",
},
}
for _, tt := range tests {
Expand Down
31 changes: 31 additions & 0 deletions server/egin/interceptor_gzip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package egin
import (
"testing"

"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)

Expand All @@ -14,3 +15,33 @@ func TestGzip(t *testing.T) {
newGzipHandler(3, opts)
assert.NoError(t, nil)
}

func TestWithGzipExcludedExtensions(t *testing.T) {
a := []string{"hello", "world"}
WithGzipExcludedExtensions(a)
WithGzipExcludedPaths(a)
WithGzipExcludedPathsRegexs(a)
NewExcludedPaths(a)
NewExcludedPathesRegexs(a)
assert.NoError(t, nil)
}

func TestWithGzipDecompressFn(t *testing.T) {
d := func(c *gin.Context) {}
WithGzipDecompressFn(d)
assert.NoError(t, nil)
}

func TestContains(t *testing.T) {
var e = ExcludedPathesRegexs{}
out := e.Contains("")
assert.Equal(t, false, out)

var ee = ExcludedPaths{}
out1 := ee.Contains("")
assert.Equal(t, false, out1)

var a = ExcludedExtensions{}
out2 := a.Contains("")
assert.Equal(t, false, out2)
}
6 changes: 3 additions & 3 deletions server/egrpc/component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ import (
func TestNewComponent(t *testing.T) {
cfg := Config{
Host: "0.0.0.0",
Port: 9001,
Port: 9007,
Network: "tcp4",
}
cmp := newComponent("test-cmp", &cfg, elog.DefaultLogger)
assert.Equal(t, "test-cmp", cmp.Name())
assert.Equal(t, "server.egrpc", cmp.PackageName())
assert.Equal(t, "0.0.0.0:9001", cmp.Address())
assert.Equal(t, "0.0.0.0:9007", cmp.Address())
assert.NoError(t, cmp.Prepare())

assert.NoError(t, cmp.Init())

info := cmp.Info()
assert.NotEmpty(t, info.Name)
assert.Equal(t, "grpc", info.Scheme)
assert.Equal(t, "0.0.0.0:9001", info.Address)
assert.Equal(t, "0.0.0.0:9007", info.Address)
assert.Equal(t, constant.ServiceProvider, info.Kind)

assert.NoError(t, nil, cmp.listener.Addr())
Expand Down
6 changes: 6 additions & 0 deletions server/egrpc/interceptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,9 @@ type PanicGreeter struct {
func (g PanicGreeter) SayHello(context context.Context, request *helloworld.HelloRequest) (*helloworld.HelloResponse, error) {
panic("we have a panic")
}

func TestCtxStoreSet(t *testing.T) {
ctx := context.Background()
CtxStoreSet(ctx, "", "")
assert.NoError(t, nil)
}
17 changes: 17 additions & 0 deletions server/egrpc/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/BurntSushi/toml"
"github.com/alibaba/sentinel-golang/core/base"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc"

Expand Down Expand Up @@ -67,3 +68,19 @@ func TestWithLogger(t *testing.T) {
comp := DefaultContainer().Build(WithLogger(logger))
assert.Equal(t, logger, comp.logger)
}

func TestWithUnaryServerResourceExtractor(t *testing.T) {
fn := func(context.Context, interface{}, *grpc.UnaryServerInfo) string {
return ""
}
WithUnaryServerResourceExtractor(fn)
assert.NoError(t, nil)
}

func TestWithUnaryServerBlockFallback(t *testing.T) {
fn := func(context.Context, interface{}, *grpc.UnaryServerInfo, *base.BlockError) (interface{}, error) {
return "", nil
}
WithUnaryServerBlockFallback(fn)
assert.NoError(t, nil)
}

0 comments on commit 76536a9

Please sign in to comment.