From aee497801252e94a2570f3281d257f82add80f32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E5=8F=AF?= Date: Mon, 6 Feb 2023 15:42:15 +0800 Subject: [PATCH] feat: remove unused code and refact test (#701) --- go.mod | 2 +- pkg/util/xcrypto/aes.go | 47 ---- pkg/util/xmap/map.go | 238 ------------------ pkg/util/xmap/util.go | 19 -- pkg/util/xmap/util_test.go | 77 ++++++ pkg/util/xrand/init.go | 26 -- pkg/util/xrand/int.go | 50 ---- pkg/util/xrand/string.go | 52 ---- pkg/util/xregexp/regexp.go | 27 -- pkg/util/xstring/format.go | 25 -- pkg/util/xstring/string.go | 115 --------- pkg/util/xstring/uuid.go | 66 ----- pkg/util/xstring/uuid_test.go | 56 ----- pkg/util/xstruct/clone.go | 34 --- proto/helloworld/v1/helloworld.pb.go | 2 +- .../{echo_test.go => server_test.go} | 2 +- test/e2e/client/redis.go | 2 +- .../core/tests => test/e2e/framework}/etcd.go | 2 +- .../core/tests => test/e2e/framework}/grpc.go | 2 +- .../core/tests => test/e2e/framework}/http.go | 2 +- .../tests => test/e2e/framework}/redis.go | 2 +- test/e2e/go.mod | 4 +- test/e2e/jupiter/grpc.go | 14 +- test/e2e/server/xecho.go | 12 +- test/e2e/server/xfasthttp.go | 8 +- test/e2e/server/xgin.go | 8 +- test/e2e/server/xgoframe.go | 8 +- test/e2e/server/xgrpc.go | 8 +- test/e2e/server/xgrpcgateway.go | 10 +- 29 files changed, 121 insertions(+), 799 deletions(-) delete mode 100644 pkg/util/xcrypto/aes.go delete mode 100644 pkg/util/xmap/map.go delete mode 100644 pkg/util/xrand/init.go delete mode 100644 pkg/util/xrand/int.go delete mode 100644 pkg/util/xrand/string.go delete mode 100644 pkg/util/xregexp/regexp.go delete mode 100644 pkg/util/xstring/format.go delete mode 100644 pkg/util/xstring/uuid.go delete mode 100644 pkg/util/xstring/uuid_test.go delete mode 100644 pkg/util/xstruct/clone.go rename test/benchmark/{echo_test.go => server_test.go} (99%) rename {pkg/core/tests => test/e2e/framework}/etcd.go (98%) rename {pkg/core/tests => test/e2e/framework}/grpc.go (99%) rename {pkg/core/tests => test/e2e/framework}/http.go (99%) rename {pkg/core/tests => test/e2e/framework}/redis.go (98%) diff --git a/go.mod b/go.mod index 657fb8a29c..e2227c4ac7 100644 --- a/go.mod +++ b/go.mod @@ -28,7 +28,6 @@ require ( github.com/grpc-ecosystem/grpc-gateway/v2 v2.15.0 github.com/hnlq715/struct2interface v0.1.4 github.com/iancoleman/strcase v0.2.0 - github.com/imdario/mergo v0.3.13 github.com/json-iterator/go v1.1.12 github.com/juju/ratelimit v1.0.2 github.com/labstack/echo/v4 v4.10.0 @@ -105,6 +104,7 @@ require ( github.com/gopherjs/gopherjs v0.0.0-20200217142428-fce0ec30dd00 // indirect github.com/grokify/html-strip-tags-go v0.0.1 // indirect github.com/hashicorp/golang-lru v0.5.4 // indirect + github.com/imdario/mergo v0.3.13 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect github.com/jtolds/gls v4.20.0+incompatible // indirect diff --git a/pkg/util/xcrypto/aes.go b/pkg/util/xcrypto/aes.go deleted file mode 100644 index 3eb88f0230..0000000000 --- a/pkg/util/xcrypto/aes.go +++ /dev/null @@ -1,47 +0,0 @@ -package xcrypto - -import ( - "bytes" - "crypto/aes" - "crypto/cipher" -) - -func PKCS7Padding(ciphertext []byte, blockSize int) []byte { - padding := blockSize - len(ciphertext)%blockSize - padtext := bytes.Repeat([]byte{byte(padding)}, padding) - return append(ciphertext, padtext...) -} - -func PKCS7UnPadding(origData []byte) []byte { - length := len(origData) - unpadding := int(origData[length-1]) - return origData[:(length - unpadding)] -} - -// AesEncrypt 加密函数 -func AesEncrypt(plaintext []byte, key, iv []byte) ([]byte, error) { - block, err := aes.NewCipher(key) - if err != nil { - return nil, err - } - blockSize := block.BlockSize() - plaintext = PKCS7Padding(plaintext, blockSize) - blockMode := cipher.NewCBCEncrypter(block, iv) - crypted := make([]byte, len(plaintext)) - blockMode.CryptBlocks(crypted, plaintext) - return crypted, nil -} - -// AesDecrypt 解密函数 -func AesDecrypt(ciphertext []byte, key, iv []byte) ([]byte, error) { - block, err := aes.NewCipher(key) - if err != nil { - return nil, err - } - blockSize := block.BlockSize() - blockMode := cipher.NewCBCDecrypter(block, iv[:blockSize]) - origData := make([]byte, len(ciphertext)) - blockMode.CryptBlocks(origData, ciphertext) - origData = PKCS7UnPadding(origData) - return origData, nil -} diff --git a/pkg/util/xmap/map.go b/pkg/util/xmap/map.go deleted file mode 100644 index 26c8d13b60..0000000000 --- a/pkg/util/xmap/map.go +++ /dev/null @@ -1,238 +0,0 @@ -// Copyright 2020 Douyu -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package xmap - -import ( - "fmt" - "reflect" - "strings" - "sync" - "time" - - "github.com/mitchellh/mapstructure" - xcast "github.com/spf13/cast" -) - -// Unmarshaller ... -type Unmarshaller = func([]byte, interface{}) error - -// KeySpliter ... -var KeySpliter = "." - -// FlatMap ... -type FlatMap struct { - data map[string]interface{} - mu sync.RWMutex - keyMap sync.Map -} - -// NewFlatMap ... -func NewFlatMap() *FlatMap { - return &FlatMap{ - data: make(map[string]interface{}), - } -} - -// Load ... -func (flat *FlatMap) Load(content []byte, unmarshal Unmarshaller) error { - data := make(map[string]interface{}) - if err := unmarshal(content, &data); err != nil { - return err - } - return flat.apply(data) -} - -func (flat *FlatMap) apply(data map[string]interface{}) error { - flat.mu.Lock() - defer flat.mu.Unlock() - - MergeStringMap(flat.data, data) - var changes = make(map[string]interface{}) - for k, v := range flat.traverse(KeySpliter) { - orig, ok := flat.keyMap.Load(k) - if ok && !reflect.DeepEqual(orig, v) { - changes[k] = v - } - flat.keyMap.Store(k, v) - } - - return nil -} - -// Set ... -func (flat *FlatMap) Set(key string, val interface{}) error { - paths := strings.Split(key, KeySpliter) - lastKey := paths[len(paths)-1] - m := deepSearch(flat.data, paths[:len(paths)-1]) - m[lastKey] = val - return flat.apply(m) -} - -// Get returns the value associated with the key -func (flat *FlatMap) Get(key string) interface{} { - return flat.find(key) -} - -// GetString returns the value associated with the key as a string. -func (flat *FlatMap) GetString(key string) string { - return xcast.ToString(flat.Get(key)) -} - -// GetBool returns the value associated with the key as a boolean. -func (flat *FlatMap) GetBool(key string) bool { - return xcast.ToBool(flat.Get(key)) -} - -// GetInt returns the value associated with the key as an integer. -func (flat *FlatMap) GetInt(key string) int { - return xcast.ToInt(flat.Get(key)) -} - -// GetInt64 returns the value associated with the key as an integer. -func (flat *FlatMap) GetInt64(key string) int64 { - return xcast.ToInt64(flat.Get(key)) -} - -// GetFloat64 returns the value associated with the key as a float64. -func (flat *FlatMap) GetFloat64(key string) float64 { - return xcast.ToFloat64(flat.Get(key)) -} - -// GetTime returns the value associated with the key as time. -func (flat *FlatMap) GetTime(key string) time.Time { - return xcast.ToTime(flat.Get(key)) -} - -// GetDuration returns the value associated with the key as a duration. -func (flat *FlatMap) GetDuration(key string) time.Duration { - return xcast.ToDuration(flat.Get(key)) -} - -// GetStringSlice returns the value associated with the key as a slice of strings. -func (flat *FlatMap) GetStringSlice(key string) []string { - return xcast.ToStringSlice(flat.Get(key)) -} - -// GetSlice returns the value associated with the key as a slice of strings. -func (flat *FlatMap) GetSlice(key string) []interface{} { - return xcast.ToSlice(flat.Get(key)) -} - -// GetStringMap returns the value associated with the key as a map of interfaces. -func (flat *FlatMap) GetStringMap(key string) map[string]interface{} { - return xcast.ToStringMap(flat.Get(key)) -} - -// GetStringMapString returns the value associated with the key as a map of strings. -func (flat *FlatMap) GetStringMapString(key string) map[string]string { - return xcast.ToStringMapString(flat.Get(key)) -} - -// GetStringMapStringSlice returns the value associated with the key as a map to a slice of strings. -func (flat *FlatMap) GetStringMapStringSlice(key string) map[string][]string { - return xcast.ToStringMapStringSlice(flat.Get(key)) -} - -// UnmarshalKey takes a single key and unmarshal it into a Struct. -func (flat *FlatMap) UnmarshalKey(key string, rawVal interface{}, tagName string) error { - config := mapstructure.DecoderConfig{ - DecodeHook: mapstructure.StringToTimeDurationHookFunc(), - Result: rawVal, - TagName: tagName, - } - decoder, err := mapstructure.NewDecoder(&config) - if err != nil { - return err - } - if key == "" { - flat.mu.RLock() - defer flat.mu.RUnlock() - return decoder.Decode(flat.data) - } - - value := flat.Get(key) - if value == nil { - return fmt.Errorf("invalid key %s, maybe not exist in config", key) - } - - return decoder.Decode(value) -} - -// Reset ... -func (flat *FlatMap) Reset() { - flat.mu.Lock() - defer flat.mu.Unlock() - - flat.data = make(map[string]interface{}) - // erase map - flat.keyMap.Range(func(key interface{}, value interface{}) bool { - flat.keyMap.Delete(key) - return true - }) -} - -func (flat *FlatMap) find(key string) interface{} { - dd, ok := flat.keyMap.Load(key) - if ok { - return dd - } - - paths := strings.Split(key, KeySpliter) - flat.mu.RLock() - defer flat.mu.RUnlock() - m := DeepSearchInMap(flat.data, paths[:len(paths)-1]...) - dd = m[paths[len(paths)-1]] - flat.keyMap.Store(key, dd) - return dd -} - -func lookup(prefix string, target map[string]interface{}, data map[string]interface{}, sep string) { - for k, v := range target { - pp := fmt.Sprintf("%s%s%s", prefix, sep, k) - if prefix == "" { - pp = k - } - if dd, err := xcast.ToStringMapE(v); err == nil { - lookup(pp, dd, data, sep) - } else { - data[pp] = v - } - } -} - -func (flat *FlatMap) traverse(sep string) map[string]interface{} { - data := make(map[string]interface{}) - lookup("", flat.data, data, sep) - return data -} - -func deepSearch(m map[string]interface{}, path []string) map[string]interface{} { - for _, k := range path { - m2, ok := m[k] - if !ok { - m3 := make(map[string]interface{}) - m[k] = m3 - m = m3 - continue - } - m3, ok := m2.(map[string]interface{}) - if !ok { - m3 = make(map[string]interface{}) - m[k] = m3 - } - m = m3 - } - return m -} diff --git a/pkg/util/xmap/util.go b/pkg/util/xmap/util.go index dd344f45f1..deeea905f9 100644 --- a/pkg/util/xmap/util.go +++ b/pkg/util/xmap/util.go @@ -17,7 +17,6 @@ package xmap import ( "fmt" "reflect" - "strings" xcast "github.com/spf13/cast" ) @@ -64,24 +63,6 @@ func ToMapStringInterface(src map[interface{}]interface{}) map[string]interface{ return tgt } -// InsensitiviseMap insensitivise map -func InsensitiviseMap(m map[string]interface{}) { - for key, val := range m { - switch val := val.(type) { - case map[interface{}]interface{}: - InsensitiviseMap(xcast.ToStringMap(val)) - case map[string]interface{}: - InsensitiviseMap(val) - } - - lower := strings.ToLower(key) - if key != lower { - delete(m, key) - } - m[lower] = val - } -} - // DeepSearchInMap deep search in map func DeepSearchInMap(m map[string]interface{}, paths ...string) map[string]interface{} { //深度拷贝 diff --git a/pkg/util/xmap/util_test.go b/pkg/util/xmap/util_test.go index 403876d8bf..ea64cc8796 100644 --- a/pkg/util/xmap/util_test.go +++ b/pkg/util/xmap/util_test.go @@ -111,3 +111,80 @@ func TestMergeStringMap(t *testing.T) { }) } } + +func TestDeepSearchInMap(t *testing.T) { + type args struct { + m map[string]interface{} + paths []string + } + tests := []struct { + name string + args args + want map[string]interface{} + }{ + { + name: "case 1: 二维测试", + args: args{ + m: map[string]interface{}{ + "2w": map[string]interface{}{ + "test": "2wtd", + "test1": "2wtd1", + }, + "2wa": map[string]interface{}{ + "test": "2wtd", + "test1": "2wtd1", + }, + }, + paths: []string{"2w"}, + }, + want: map[string]interface{}{ + "test": "2wtd", + "test1": "2wtd1", + }, + }, + { + name: "case 2: 二维测试", + args: args{ + m: map[string]interface{}{ + "2w": map[string]interface{}{ + "test": "2wtd", + "test1": "2wtd1", + }, + "2wa": map[string]interface{}{ + "testa": "2wtd", + "testa1": "2wtd1", + }, + }, + paths: []string{"2wa"}, + }, + want: map[string]interface{}{ + "testa": "2wtd", + "testa1": "2wtd1", + }, + }, + { + name: "case 2: not found", + args: args{ + m: map[string]interface{}{ + "2w": map[string]interface{}{ + "test": "2wtd", + "test1": "2wtd1", + }, + "2wa": map[string]interface{}{ + "testa": "2wtd", + "testa1": "2wtd1", + }, + }, + paths: []string{"not found"}, + }, + want: map[string]interface{}{}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := DeepSearchInMap(tt.args.m, tt.args.paths...); !reflect.DeepEqual(got, tt.want) { + t.Errorf("DeepSearchInMap() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/pkg/util/xrand/init.go b/pkg/util/xrand/init.go deleted file mode 100644 index f9ae97f324..0000000000 --- a/pkg/util/xrand/init.go +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright 2020 Douyu -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package xrand - -import ( - "math/rand" - "sync" - "time" -) - -var ( - r = rand.New(rand.NewSource(time.Now().UnixNano())) - mu sync.Mutex -) diff --git a/pkg/util/xrand/int.go b/pkg/util/xrand/int.go deleted file mode 100644 index 1e4700ec7c..0000000000 --- a/pkg/util/xrand/int.go +++ /dev/null @@ -1,50 +0,0 @@ -// Copyright 2020 Douyu -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package xrand - -import ( - "math/rand" - "time" -) - -// Int63n implements rand.Int63n on the grpcrand global source. -func Int63n(n int64) int64 { - mu.Lock() - res := r.Int63n(n) - mu.Unlock() - return res -} - -// Intn implements rand.Intn on the grpcrand global source. -func Intn(n int) int { - mu.Lock() - res := r.Intn(n) - mu.Unlock() - return res -} - -// Float64 implements rand.Float64 on the grpcrand global source. -func Float64() float64 { - mu.Lock() - res := r.Float64() - mu.Unlock() - return res -} - -// Shuffle ... -func Shuffle(length int, fn func(i, j int)) { - xr := rand.New(rand.NewSource(time.Now().UnixNano())) - xr.Shuffle(length, fn) -} diff --git a/pkg/util/xrand/string.go b/pkg/util/xrand/string.go deleted file mode 100644 index 3805dfc132..0000000000 --- a/pkg/util/xrand/string.go +++ /dev/null @@ -1,52 +0,0 @@ -// Copyright 2020 Douyu -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package xrand - -import ( - "math/rand" - "strings" -) - -// Charsets -const ( - // Uppercase ... - Uppercase string = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - // Lowercase ... - Lowercase = "abcdefghipqrstuvwxyz" - // Alphabetic ... - Alphabetic = Uppercase + Lowercase - // Numeric ... - Numeric = "0123456789" - // Alphanumeric ... - Alphanumeric = Alphabetic + Numeric - // Symbols ... - Symbols = "`" + `~!@#$%^&*()-_+={}[]|\;:"<>,./?` - // Hex ... - Hex = Numeric + "abcdef" -) - -// String 返回随机字符串,通常用于测试mock数据 -func String(length uint8, charsets ...string) string { - charset := strings.Join(charsets, "") - if charset == "" { - charset = Alphanumeric - } - - b := make([]byte, length) - for i := range b { - b[i] = charset[rand.Int63()%int64(len(charset))] - } - return string(b) -} diff --git a/pkg/util/xregexp/regexp.go b/pkg/util/xregexp/regexp.go deleted file mode 100644 index 6d02589c3f..0000000000 --- a/pkg/util/xregexp/regexp.go +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright 2020 Douyu -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package xregexp - -import "regexp" - -// RegexpReplace ... -func RegexpReplace(reg, src, temp string) string { - result := []byte{} - pattern := regexp.MustCompile(reg) - for _, submatches := range pattern.FindAllStringSubmatchIndex(src, -1) { - result = pattern.ExpandString(result, temp, src, submatches) - } - return string(result) -} diff --git a/pkg/util/xstring/format.go b/pkg/util/xstring/format.go deleted file mode 100644 index e2da6786e8..0000000000 --- a/pkg/util/xstring/format.go +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright 2020 Douyu -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package xstring - -import "fmt" - -// Formatter ... -type Formatter string - -// Format ... -func (fm Formatter) Format(args ...interface{}) string { - return fmt.Sprintf(string(fm), args...) -} diff --git a/pkg/util/xstring/string.go b/pkg/util/xstring/string.go index e4db8c1dcf..5f98396eaa 100644 --- a/pkg/util/xstring/string.go +++ b/pkg/util/xstring/string.go @@ -20,8 +20,6 @@ import ( "fmt" "net" "strconv" - "strings" - "unicode/utf8" ) // Addr2Hex converts address string to hex string, only support ipv4. @@ -90,116 +88,3 @@ func AnyBlank(ss []string) bool { return false } - -func Any(ss []string, match func(item string) bool) bool { - for _, str := range ss { - if match(str) { - return true - } - } - return false -} - -func All(ss []string, match func(item string) bool) bool { - if len(ss) == 0 { - return false - } - for _, str := range ss { - if !match(str) { - return false - } - } - return true -} - -// HeadT ... -func (ss Strings) HeadT() (string, Strings) { - if len(ss) > 0 { - return ss[0], Strings(ss[1:]) - } - - return "", Strings{} -} - -// Head ... -func (ss Strings) Head() string { - if len(ss) > 0 { - return ss[0] - } - return "" -} - -// Head2 ... -func (ss Strings) Head2() (h0, h1 string) { - if len(ss) > 0 { - h0 = ss[0] - } - if len(ss) > 1 { - h1 = ss[1] - } - return -} - -// Head3 ... -func (ss Strings) Head3() (h0, h1, h2 string) { - if len(ss) > 0 { - h0 = ss[0] - } - if len(ss) > 1 { - h1 = ss[1] - } - if len(ss) > 2 { - h2 = ss[2] - } - return -} - -// Head4 ... -func (ss Strings) Head4() (h0, h1, h2, h3 string) { - if len(ss) > 0 { - h0 = ss[0] - } - if len(ss) > 1 { - h1 = ss[1] - } - if len(ss) > 2 { - h2 = ss[2] - } - if len(ss) > 3 { - h3 = ss[3] - } - return -} - -// Split ... -func Split(raw string, sep string) Strings { - return Strings(strings.Split(raw, sep)) -} - -func Pointer(str string) *string { - return &str -} - -func IndexString(ss []string, str string) int { - for ind, s := range ss { - if str == s { - return ind - } - } - return -1 -} - -func HasString(ss []string, str string) bool { - return IndexString(ss, str) > -1 -} - -func Reverse(s string) string { - size := len(s) - buf := make([]byte, size) - for start := 0; start < size; { - r, n := utf8.DecodeRuneInString(s[start:]) - start += n - utf8.EncodeRune(buf[size-start:], r) - } - return string(buf) -} diff --git a/pkg/util/xstring/uuid.go b/pkg/util/xstring/uuid.go deleted file mode 100644 index 581062f522..0000000000 --- a/pkg/util/xstring/uuid.go +++ /dev/null @@ -1,66 +0,0 @@ -// Copyright 2020 Douyu -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package xstring - -import ( - "fmt" - "math/rand" - "sync/atomic" - "time" -) - -var timeBase = time.Date(1582, time.October, 15, 0, 0, 0, 0, time.UTC).Unix() -var hardwareAddr []byte -var clockSeq uint32 -var randInstance *rand.Rand - -func init() { - randInstance = rand.New(rand.NewSource(time.Now().UnixNano())) -} - -// GenerateUUID simply generates an unique UID. -func GenerateUUID(seedTime time.Time) string { - var u [16]byte - utcTime := seedTime.In(time.UTC) - t := uint64(utcTime.Unix()-timeBase)*10000000 + uint64(utcTime.Nanosecond()/100) - - u[0], u[1], u[2], u[3] = byte(t>>24), byte(t>>16), byte(t>>8), byte(t) - u[4], u[5] = byte(t>>40), byte(t>>32) - u[6], u[7] = byte(t>>56)&0x0F, byte(t>>48) - - clock := atomic.AddUint32(&clockSeq, 1) - u[8] = byte(clock >> 8) - u[9] = byte(clock) - - copy(u[10:], hardwareAddr) - - u[6] |= 0x10 // set version to 1 (time based uuid) - u[8] &= 0x3F // clear variant - u[8] |= 0x80 // set to IETF variant - - var offsets = [...]int{0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30} - const hexString = "0123456789abcdef" - r := make([]byte, 32) - for i, b := range u { - r[offsets[i]] = hexString[b>>4] - r[offsets[i]+1] = hexString[b&0xF] - } - return string(r) -} - -// GenerateID simply generates an ID. -func GenerateID() string { - return fmt.Sprintf("%016x", uint64(randInstance.Int63())) -} diff --git a/pkg/util/xstring/uuid_test.go b/pkg/util/xstring/uuid_test.go deleted file mode 100644 index 2747ea5900..0000000000 --- a/pkg/util/xstring/uuid_test.go +++ /dev/null @@ -1,56 +0,0 @@ -// Copyright 2020 Douyu -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package xstring - -import ( - "testing" - "time" -) - -func TestGenerateUUID(t *testing.T) { - type args struct { - seedTime time.Time - } - tests := []struct { - name string - args args - want string - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if got := GenerateUUID(tt.args.seedTime); got != tt.want { - t.Errorf("GenerateUUID() = %v, want %v", got, tt.want) - } - }) - } -} - -func TestGenerateID(t *testing.T) { - tests := []struct { - name string - want string - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if got := GenerateID(); got != tt.want { - t.Errorf("GenerateID() = %v, want %v", got, tt.want) - } - }) - } -} diff --git a/pkg/util/xstruct/clone.go b/pkg/util/xstruct/clone.go deleted file mode 100644 index bc12fa223a..0000000000 --- a/pkg/util/xstruct/clone.go +++ /dev/null @@ -1,34 +0,0 @@ -// Copyright 2020 Douyu -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package xstruct - -import "reflect" - -// CopyStruct ... -func CopyStruct(src, dst interface{}) { - srcVal := reflect.ValueOf(src).Elem() - dstVal := reflect.ValueOf(dst).Elem() - - for i := 0; i < srcVal.NumField(); i++ { - value := srcVal.Field(i) - name := srcVal.Type().Field(i).Name - - dstValue := dstVal.FieldByName(name) - if !dstValue.IsValid() { - continue - } - dstValue.Set(value) - } -} diff --git a/proto/helloworld/v1/helloworld.pb.go b/proto/helloworld/v1/helloworld.pb.go index d8fe11583e..33fac6d9e0 100644 --- a/proto/helloworld/v1/helloworld.pb.go +++ b/proto/helloworld/v1/helloworld.pb.go @@ -1,6 +1,6 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: -// protoc-gen-go v1.28.1 +// protoc-gen-go v1.28.1-devel // protoc (unknown) // source: helloworld/v1/helloworld.proto diff --git a/test/benchmark/echo_test.go b/test/benchmark/server_test.go similarity index 99% rename from test/benchmark/echo_test.go rename to test/benchmark/server_test.go index fd18d39116..77532aba1c 100644 --- a/test/benchmark/echo_test.go +++ b/test/benchmark/server_test.go @@ -19,7 +19,7 @@ import ( "google.golang.org/protobuf/proto" ) -func BenchmarkHTTP(b *testing.B) { +func BenchmarkServer(b *testing.B) { b.Run("gRPC with protobuf", func(b *testing.B) { server := grpc.NewServer() diff --git a/test/e2e/client/redis.go b/test/e2e/client/redis.go index a8d012a65a..95115c256e 100644 --- a/test/e2e/client/redis.go +++ b/test/e2e/client/redis.go @@ -16,7 +16,7 @@ package client import ( "github.com/douyu/jupiter/pkg/client/redis" - "github.com/douyu/jupiter/pkg/core/tests" + tests "github.com/douyu/jupiter/test/e2e/framework" "github.com/onsi/ginkgo/v2" ) diff --git a/pkg/core/tests/etcd.go b/test/e2e/framework/etcd.go similarity index 98% rename from pkg/core/tests/etcd.go rename to test/e2e/framework/etcd.go index 438eb4c509..f64c2b1be8 100644 --- a/pkg/core/tests/etcd.go +++ b/test/e2e/framework/etcd.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package tests +package framework import ( "github.com/douyu/jupiter/pkg/registry" diff --git a/pkg/core/tests/grpc.go b/test/e2e/framework/grpc.go similarity index 99% rename from pkg/core/tests/grpc.go rename to test/e2e/framework/grpc.go index 294d5f7f14..d1c7d53a5d 100644 --- a/pkg/core/tests/grpc.go +++ b/test/e2e/framework/grpc.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package tests +package framework import ( "context" diff --git a/pkg/core/tests/http.go b/test/e2e/framework/http.go similarity index 99% rename from pkg/core/tests/http.go rename to test/e2e/framework/http.go index b80b223273..07cf5b218a 100644 --- a/pkg/core/tests/http.go +++ b/test/e2e/framework/http.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package tests +package framework import ( "bytes" diff --git a/pkg/core/tests/redis.go b/test/e2e/framework/redis.go similarity index 98% rename from pkg/core/tests/redis.go rename to test/e2e/framework/redis.go index 40b0bb4156..3dfd82f627 100644 --- a/pkg/core/tests/redis.go +++ b/test/e2e/framework/redis.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package tests +package framework import ( "context" diff --git a/test/e2e/go.mod b/test/e2e/go.mod index 5b572dcf99..6f7e77cbb1 100644 --- a/test/e2e/go.mod +++ b/test/e2e/go.mod @@ -10,12 +10,14 @@ require ( github.com/gin-gonic/gin v1.8.2 github.com/gogf/gf v1.16.9 github.com/grpc-ecosystem/grpc-gateway/v2 v2.15.0 + github.com/imdario/mergo v0.3.13 github.com/labstack/echo/v4 v4.10.0 github.com/onsi/ginkgo/v2 v2.8.0 github.com/onsi/gomega v1.26.0 github.com/stretchr/testify v1.8.1 github.com/valyala/fasthttp v1.44.0 google.golang.org/grpc v1.52.3 + google.golang.org/protobuf v1.28.1 ) require ( @@ -53,7 +55,6 @@ require ( github.com/gorilla/websocket v1.5.0 // indirect github.com/grokify/html-strip-tags-go v0.0.1 // indirect github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect - github.com/imdario/mergo v0.3.13 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/klauspost/compress v1.15.9 // indirect github.com/labstack/gommon v0.4.0 // indirect @@ -107,7 +108,6 @@ require ( golang.org/x/sys v0.4.0 // indirect golang.org/x/text v0.6.0 // indirect google.golang.org/genproto v0.0.0-20221207170731-23e4bf6bdc37 // indirect - google.golang.org/protobuf v1.28.1 // indirect gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/test/e2e/jupiter/grpc.go b/test/e2e/jupiter/grpc.go index 6021e086ea..852cde49bf 100644 --- a/test/e2e/jupiter/grpc.go +++ b/test/e2e/jupiter/grpc.go @@ -25,12 +25,12 @@ import ( "github.com/douyu/jupiter/pkg/conf" "github.com/douyu/jupiter/pkg/conf/datasource/file" "github.com/douyu/jupiter/pkg/core/application" - "github.com/douyu/jupiter/pkg/core/tests" "github.com/douyu/jupiter/pkg/registry" "github.com/douyu/jupiter/pkg/registry/etcdv3" "github.com/douyu/jupiter/pkg/server" "github.com/douyu/jupiter/pkg/server/xgrpc" helloworldv1 "github.com/douyu/jupiter/proto/helloworld/v1" + "github.com/douyu/jupiter/test/e2e/framework" "github.com/onsi/ginkgo/v2" "github.com/stretchr/testify/assert" "google.golang.org/grpc/metadata" @@ -60,10 +60,10 @@ var _ = ginkgo.Describe("[jupiter] e2e test", ginkgo.Ordered, func() { conf.Reset() }) - ginkgo.DescribeTable("jupiter grpc sayhello", func(gtc tests.GRPCTestCase) { - tests.RunGRPCTestCase(gtc) + ginkgo.DescribeTable("jupiter grpc sayhello", func(gtc framework.GRPCTestCase) { + framework.RunGRPCTestCase(gtc) }, - ginkgo.Entry("normal case", tests.GRPCTestCase{ + ginkgo.Entry("normal case", framework.GRPCTestCase{ Conf: &grpc.Config{ Addr: "localhost:9527", }, @@ -77,10 +77,10 @@ var _ = ginkgo.Describe("[jupiter] e2e test", ginkgo.Ordered, func() { }), ) - ginkgo.DescribeTable("jupiter registry", func(tc tests.ETCDTestCase) { - tests.RunETCDTestCase(tc) + ginkgo.DescribeTable("jupiter registry", func(tc framework.ETCDTestCase) { + framework.RunETCDTestCase(tc) }, - ginkgo.Entry("normal case", tests.ETCDTestCase{ + ginkgo.Entry("normal case", framework.ETCDTestCase{ Conf: &etcdv3.Config{ Config: &cetcdv3.Config{ Endpoints: []string{"http://localhost:2379"}, diff --git a/test/e2e/server/xecho.go b/test/e2e/server/xecho.go index 1b564be082..06200d9ec7 100644 --- a/test/e2e/server/xecho.go +++ b/test/e2e/server/xecho.go @@ -19,9 +19,9 @@ import ( "time" "github.com/douyu/jupiter/pkg/client/resty" - "github.com/douyu/jupiter/pkg/core/tests" "github.com/douyu/jupiter/pkg/server/xecho" helloworldv1 "github.com/douyu/jupiter/proto/helloworld/v1" + "github.com/douyu/jupiter/test/e2e/framework" "github.com/labstack/echo/v4" "github.com/onsi/ginkgo/v2" "github.com/stretchr/testify/assert" @@ -51,10 +51,10 @@ var _ = ginkgo.Describe("[xecho] e2e test", func() { _ = server.Stop() }) - ginkgo.DescribeTable("xecho ", func(htc tests.HTTPTestCase) { - tests.RunHTTPTestCase(htc) + ginkgo.DescribeTable("xecho ", func(htc framework.HTTPTestCase) { + framework.RunHTTPTestCase(htc) }, - ginkgo.Entry("normal case", tests.HTTPTestCase{ + ginkgo.Entry("normal case", framework.HTTPTestCase{ Conf: &resty.Config{ Addr: "http://localhost:9091", }, @@ -64,7 +64,7 @@ var _ = ginkgo.Describe("[xecho] e2e test", func() { ExpectBody: "hello", }), - ginkgo.Entry("grpc proxy get", tests.HTTPTestCase{ + ginkgo.Entry("grpc proxy get", framework.HTTPTestCase{ Conf: &resty.Config{ Addr: "http://localhost:9091", }, @@ -75,7 +75,7 @@ var _ = ginkgo.Describe("[xecho] e2e test", func() { ExpectBody: `{"error":0,"msg":"","data":{"name":"bob","ageNumber":"0","sex":0,"metadata":{}}}`, }), - ginkgo.Entry("grpc proxy post", tests.HTTPTestCase{ + ginkgo.Entry("grpc proxy post", framework.HTTPTestCase{ Conf: &resty.Config{ Addr: "http://localhost:9091", }, diff --git a/test/e2e/server/xfasthttp.go b/test/e2e/server/xfasthttp.go index 3f87dfcb26..3244079b3d 100644 --- a/test/e2e/server/xfasthttp.go +++ b/test/e2e/server/xfasthttp.go @@ -19,8 +19,8 @@ import ( "time" "github.com/douyu/jupiter/pkg/client/resty" - "github.com/douyu/jupiter/pkg/core/tests" "github.com/douyu/jupiter/pkg/server/xfasthttp" + "github.com/douyu/jupiter/test/e2e/framework" "github.com/onsi/ginkgo/v2" "github.com/stretchr/testify/assert" "github.com/valyala/fasthttp" @@ -45,9 +45,9 @@ var _ = ginkgo.Describe("[xfasthttp] e2e test", func() { _ = server.Stop() }) - ginkgo.DescribeTable("xfasthttp", func(htc tests.HTTPTestCase) { - tests.RunHTTPTestCase(htc) - }, ginkgo.Entry("normal case", tests.HTTPTestCase{ + ginkgo.DescribeTable("xfasthttp", func(htc framework.HTTPTestCase) { + framework.RunHTTPTestCase(htc) + }, ginkgo.Entry("normal case", framework.HTTPTestCase{ Conf: &resty.Config{ Addr: "http://localhost:9091", }, diff --git a/test/e2e/server/xgin.go b/test/e2e/server/xgin.go index bf921075d8..a1064388c8 100644 --- a/test/e2e/server/xgin.go +++ b/test/e2e/server/xgin.go @@ -19,8 +19,8 @@ import ( "time" "github.com/douyu/jupiter/pkg/client/resty" - "github.com/douyu/jupiter/pkg/core/tests" "github.com/douyu/jupiter/pkg/server/xgin" + "github.com/douyu/jupiter/test/e2e/framework" "github.com/gin-gonic/gin" "github.com/onsi/ginkgo/v2" "github.com/stretchr/testify/assert" @@ -45,9 +45,9 @@ var _ = ginkgo.Describe("[xgin] e2e test", func() { _ = server.Stop() }) - ginkgo.DescribeTable("xgin", func(htc tests.HTTPTestCase) { - tests.RunHTTPTestCase(htc) - }, ginkgo.Entry("normal case", tests.HTTPTestCase{ + ginkgo.DescribeTable("xgin", func(htc framework.HTTPTestCase) { + framework.RunHTTPTestCase(htc) + }, ginkgo.Entry("normal case", framework.HTTPTestCase{ Conf: &resty.Config{ Addr: "http://localhost:9091", }, diff --git a/test/e2e/server/xgoframe.go b/test/e2e/server/xgoframe.go index 0003893537..885eb49dd2 100644 --- a/test/e2e/server/xgoframe.go +++ b/test/e2e/server/xgoframe.go @@ -19,8 +19,8 @@ import ( "time" "github.com/douyu/jupiter/pkg/client/resty" - "github.com/douyu/jupiter/pkg/core/tests" "github.com/douyu/jupiter/pkg/server/xgoframe" + "github.com/douyu/jupiter/test/e2e/framework" "github.com/gogf/gf/net/ghttp" "github.com/onsi/ginkgo/v2" "github.com/stretchr/testify/assert" @@ -45,9 +45,9 @@ var _ = ginkgo.Describe("[xgoframe] e2e test", func() { _ = server.Stop() }) - ginkgo.DescribeTable("xgoframe", func(htc tests.HTTPTestCase) { - tests.RunHTTPTestCase(htc) - }, ginkgo.Entry("normal case", tests.HTTPTestCase{ + ginkgo.DescribeTable("xgoframe", func(htc framework.HTTPTestCase) { + framework.RunHTTPTestCase(htc) + }, ginkgo.Entry("normal case", framework.HTTPTestCase{ Conf: &resty.Config{ Addr: "http://localhost:8099", }, diff --git a/test/e2e/server/xgrpc.go b/test/e2e/server/xgrpc.go index f133ec2d08..3cb5201c29 100644 --- a/test/e2e/server/xgrpc.go +++ b/test/e2e/server/xgrpc.go @@ -18,9 +18,9 @@ import ( "time" "github.com/douyu/jupiter/pkg/client/grpc" - "github.com/douyu/jupiter/pkg/core/tests" "github.com/douyu/jupiter/pkg/server/xgrpc" helloworldv1 "github.com/douyu/jupiter/proto/helloworld/v1" + "github.com/douyu/jupiter/test/e2e/framework" "github.com/onsi/ginkgo/v2" "github.com/stretchr/testify/assert" "google.golang.org/grpc/metadata" @@ -43,10 +43,10 @@ var _ = ginkgo.Describe("[grpc] e2e test", func() { _ = server.Stop() }) - ginkgo.DescribeTable("xgrpc sayhello", func(gtc tests.GRPCTestCase) { - tests.RunGRPCTestCase(gtc) + ginkgo.DescribeTable("xgrpc sayhello", func(gtc framework.GRPCTestCase) { + framework.RunGRPCTestCase(gtc) }, - ginkgo.Entry("normal case", tests.GRPCTestCase{ + ginkgo.Entry("normal case", framework.GRPCTestCase{ Conf: &grpc.Config{ Addr: "localhost:9092", }, diff --git a/test/e2e/server/xgrpcgateway.go b/test/e2e/server/xgrpcgateway.go index 873a7ad7e8..b5a70a8dce 100644 --- a/test/e2e/server/xgrpcgateway.go +++ b/test/e2e/server/xgrpcgateway.go @@ -19,9 +19,9 @@ import ( "time" "github.com/douyu/jupiter/pkg/client/resty" - "github.com/douyu/jupiter/pkg/core/tests" "github.com/douyu/jupiter/pkg/server/xecho" helloworldv1 "github.com/douyu/jupiter/proto/helloworld/v1" + "github.com/douyu/jupiter/test/e2e/framework" "github.com/grpc-ecosystem/grpc-gateway/v2/runtime" "github.com/labstack/echo/v4" "github.com/onsi/ginkgo/v2" @@ -51,10 +51,10 @@ var _ = ginkgo.Describe("[xgrpcgateway] e2e test", func() { _ = server.Stop() }) - ginkgo.DescribeTable("xgrpcgateway", func(htc tests.HTTPTestCase) { - tests.RunHTTPTestCase(htc) + ginkgo.DescribeTable("xgrpcgateway", func(htc framework.HTTPTestCase) { + framework.RunHTTPTestCase(htc) }, - ginkgo.Entry("normal case", tests.HTTPTestCase{ + ginkgo.Entry("normal case", framework.HTTPTestCase{ Conf: &resty.Config{ Addr: "http://localhost:9091", }, @@ -64,7 +64,7 @@ var _ = ginkgo.Describe("[xgrpcgateway] e2e test", func() { ExpectStatus: http.StatusOK, ExpectBody: `{"error":0,"msg":"","data":{"name":"jupiter","ageNumber":"0","sex":"SEX_UNSPECIFIED","metadata":{}}}`, }), - ginkgo.Entry("404", tests.HTTPTestCase{ + ginkgo.Entry("404", framework.HTTPTestCase{ Conf: &resty.Config{ Addr: "http://localhost:9091", },