You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
3.7 KiB
Go

package codec
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRegistry(t *testing.T) {
registry := NewRegistry()
// 测试获取默认编解码器
defaultCodec := registry.Default()
require.NotNil(t, defaultCodec, "Expected default codec to be non-nil")
assert.Equal(t, "binary", defaultCodec.Name(), "Expected default codec to be binary")
}
func TestRegistryGet(t *testing.T) {
registry := NewRegistry()
_ = registry.Register("json", NewJSONCodec())
_ = registry.Register("plain", NewPlainCodec())
// 测试获取JSON编解码器
codec, err := registry.Get("json")
require.NoError(t, err, "Expected no error when getting json codec")
assert.NotNil(t, codec, "Expected codec to be non-nil")
assert.Equal(t, "json", codec.Name(), "Expected codec name to be 'json'")
// 测试获取二进制编解码器
codec, err = registry.Get("binary")
require.NoError(t, err, "Expected no error when getting binary codec")
assert.NotNil(t, codec, "Expected codec to be non-nil")
assert.Equal(t, "binary", codec.Name(), "Expected codec name to be 'binary'")
// 测试获取Plain编解码器
codec, err = registry.Get("plain")
require.NoError(t, err, "Expected no error when getting plain codec")
assert.NotNil(t, codec, "Expected codec to be non-nil")
assert.Equal(t, "plain", codec.Name(), "Expected codec name to be 'plain'")
// 测试获取不存在的编解码器
_, err = registry.Get("nonexistent")
assert.Error(t, err, "Expected error for nonexistent codec")
}
func TestRegistryGetEmptyName(t *testing.T) {
registry := NewRegistry()
// 测试空名称(应该返回默认编解码器)
codec, err := registry.Get("")
require.NoError(t, err, "Expected no error when getting codec with empty name")
assert.NotNil(t, codec, "Expected codec to be non-nil")
assert.Equal(t, "binary", codec.Name(), "Expected default codec to be binary")
}
func TestRegistryRegister(t *testing.T) {
registry := NewRegistry()
// 创建自定义编解码器
customCodec := &testCodec{name: "custom"}
// 注册自定义编解码器
err := registry.Register("custom", customCodec)
require.NoError(t, err, "Expected no error when registering codec")
// 获取自定义编解码器
retrieved, err := registry.Get("custom")
require.NoError(t, err, "Expected no error when getting custom codec")
assert.Equal(t, "custom", retrieved.Name(), "Expected codec name to be 'custom'")
}
func TestRegistryRegisterEmptyName(t *testing.T) {
registry := NewRegistry()
// 测试注册空名称编解码器
customCodec := &testCodec{name: "custom"}
err := registry.Register("", customCodec)
assert.Error(t, err, "Expected error for empty codec name")
}
func TestRegistryRegisterNilCodec(t *testing.T) {
registry := NewRegistry()
// 测试注册nil编解码器
err := registry.Register("nil", nil)
assert.Error(t, err, "Expected error for nil codec")
}
func TestRegistryConcurrent(t *testing.T) {
registry := NewRegistry()
// 并发注册和获取编解码器
done := make(chan bool, 100)
for i := 0; i < 100; i++ {
go func(id int) {
codec := &testCodec{name: string(rune(id))}
registry.Register(string(rune(id)), codec)
_, _ = registry.Get(string(rune(id)))
done <- true
}(i)
}
// 等待所有goroutine完成
for i := 0; i < 100; i++ {
<-done
}
}
// SetDefault is an internal method, tested through behavior
// Default codec is JSON by default, which is tested in TestRegistry
// testCodec 测试用的编解码器
type testCodec struct {
name string
}
func (c *testCodec) Name() string {
return c.name
}
func (c *testCodec) Encode(v interface{}) ([]byte, error) {
return []byte("test"), nil
}
func (c *testCodec) Decode(data []byte, v interface{}) error {
return nil
}