mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-23 16:58:31 +02:00
merge: main branch
This commit is contained in:
65
internal/utils/pool/pool.go
Normal file
65
internal/utils/pool/pool.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package pool
|
||||
|
||||
import (
|
||||
"sort"
|
||||
|
||||
"github.com/puzpuzpuz/xsync/v3"
|
||||
"github.com/yusing/go-proxy/internal/logging"
|
||||
)
|
||||
|
||||
type (
|
||||
Pool[T Object] struct {
|
||||
m *xsync.MapOf[string, T]
|
||||
name string
|
||||
}
|
||||
Object interface {
|
||||
Key() string
|
||||
Name() string
|
||||
}
|
||||
)
|
||||
|
||||
func New[T Object](name string) Pool[T] {
|
||||
return Pool[T]{xsync.NewMapOf[string, T](), name}
|
||||
}
|
||||
|
||||
func (p Pool[T]) Name() string {
|
||||
return p.name
|
||||
}
|
||||
|
||||
func (p Pool[T]) Add(obj T) {
|
||||
p.checkExists(obj.Key())
|
||||
p.m.Store(obj.Key(), obj)
|
||||
logging.Info().Msgf("%s: added %s", p.name, obj.Name())
|
||||
}
|
||||
|
||||
func (p Pool[T]) Del(obj T) {
|
||||
p.m.Delete(obj.Key())
|
||||
logging.Info().Msgf("%s: removed %s", p.name, obj.Name())
|
||||
}
|
||||
|
||||
func (p Pool[T]) Get(key string) (T, bool) {
|
||||
return p.m.Load(key)
|
||||
}
|
||||
|
||||
func (p Pool[T]) Size() int {
|
||||
return p.m.Size()
|
||||
}
|
||||
|
||||
func (p Pool[T]) Clear() {
|
||||
p.m.Clear()
|
||||
}
|
||||
|
||||
func (p Pool[T]) Iter(fn func(k string, v T) bool) {
|
||||
p.m.Range(fn)
|
||||
}
|
||||
|
||||
func (p Pool[T]) Slice() []T {
|
||||
slice := make([]T, 0, p.m.Size())
|
||||
for _, v := range p.m.Range {
|
||||
slice = append(slice, v)
|
||||
}
|
||||
sort.Slice(slice, func(i, j int) bool {
|
||||
return slice[i].Name() < slice[j].Name()
|
||||
})
|
||||
return slice
|
||||
}
|
||||
15
internal/utils/pool/pool_debug.go
Normal file
15
internal/utils/pool/pool_debug.go
Normal file
@@ -0,0 +1,15 @@
|
||||
//go:build debug
|
||||
|
||||
package pool
|
||||
|
||||
import (
|
||||
"runtime/debug"
|
||||
|
||||
"github.com/yusing/go-proxy/internal/logging"
|
||||
)
|
||||
|
||||
func (p Pool[T]) checkExists(key string) {
|
||||
if _, ok := p.m.Load(key); ok {
|
||||
logging.Warn().Msgf("%s: key %s already exists\nstacktrace: %s", p.name, key, string(debug.Stack()))
|
||||
}
|
||||
}
|
||||
7
internal/utils/pool/pool_prod.go
Normal file
7
internal/utils/pool/pool_prod.go
Normal file
@@ -0,0 +1,7 @@
|
||||
//go:build !debug
|
||||
|
||||
package pool
|
||||
|
||||
func (p Pool[T]) checkExists(key string) {
|
||||
// no-op in production
|
||||
}
|
||||
71
internal/utils/testing/expect.go
Normal file
71
internal/utils/testing/expect.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package expect
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/yusing/go-proxy/internal/common"
|
||||
)
|
||||
|
||||
func init() {
|
||||
if common.IsTest {
|
||||
// force verbose output
|
||||
os.Args = append([]string{os.Args[0], "-test.v"}, os.Args[1:]...)
|
||||
}
|
||||
}
|
||||
|
||||
func Must[Result any](r Result, err error) Result {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
var (
|
||||
NoError = require.NoError
|
||||
HasError = require.Error
|
||||
True = require.True
|
||||
False = require.False
|
||||
Nil = require.Nil
|
||||
NotNil = require.NotNil
|
||||
ErrorContains = require.ErrorContains
|
||||
Panics = require.Panics
|
||||
Greater = require.Greater
|
||||
Less = require.Less
|
||||
GreaterOrEqual = require.GreaterOrEqual
|
||||
LessOrEqual = require.LessOrEqual
|
||||
)
|
||||
|
||||
func ErrorIs(t *testing.T, expected error, err error, msgAndArgs ...any) {
|
||||
t.Helper()
|
||||
require.ErrorIs(t, err, expected, msgAndArgs...)
|
||||
}
|
||||
|
||||
func ErrorT[T error](t *testing.T, err error, msgAndArgs ...any) {
|
||||
t.Helper()
|
||||
var errAs T
|
||||
require.ErrorAs(t, err, &errAs, msgAndArgs...)
|
||||
}
|
||||
|
||||
func Equal[T any](t *testing.T, got T, want T, msgAndArgs ...any) {
|
||||
t.Helper()
|
||||
require.EqualValues(t, want, got, msgAndArgs...)
|
||||
}
|
||||
|
||||
func NotEqual[T any](t *testing.T, got T, want T, msgAndArgs ...any) {
|
||||
t.Helper()
|
||||
require.NotEqual(t, want, got, msgAndArgs...)
|
||||
}
|
||||
|
||||
func Contains[T any](t *testing.T, got T, wants []T, msgAndArgs ...any) {
|
||||
t.Helper()
|
||||
require.Contains(t, wants, got, msgAndArgs...)
|
||||
}
|
||||
|
||||
func Type[T any](t *testing.T, got any, msgAndArgs ...any) (_ T) {
|
||||
t.Helper()
|
||||
_, ok := got.(T)
|
||||
require.True(t, ok, msgAndArgs...)
|
||||
return got.(T)
|
||||
}
|
||||
14
internal/utils/testing/sonic.go
Normal file
14
internal/utils/testing/sonic.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package expect
|
||||
|
||||
import (
|
||||
"github.com/bytedance/sonic"
|
||||
"github.com/yusing/go-proxy/internal/common"
|
||||
)
|
||||
|
||||
func init() {
|
||||
if common.IsTest {
|
||||
sonic.ConfigDefault = sonic.Config{
|
||||
SortMapKeys: true,
|
||||
}.Froze()
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package utils
|
||||
package expect
|
||||
|
||||
import (
|
||||
"os"
|
||||
@@ -14,13 +14,6 @@ func init() {
|
||||
}
|
||||
}
|
||||
|
||||
func Must[Result any](r Result, err error) Result {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
func ExpectNoError(t *testing.T, err error) {
|
||||
t.Helper()
|
||||
require.NoError(t, err)
|
||||
|
||||
Reference in New Issue
Block a user