Apply .gitignore rules

This commit is contained in:
皮蛋13361098506
2025-01-06 16:21:36 +08:00
parent 1b77f62820
commit ccd2c530cf
580 changed files with 69806 additions and 0 deletions

View File

@@ -0,0 +1,114 @@
package redisUtil
import (
"testing"
"time"
)
var (
redisPoolObj *RedisPool
)
func init() {
redisPoolObj = NewRedisPool("testPool", "10.1.0.21:6379", "redis_pwd", 5, 500, 200, 10*time.Second, 5*time.Second)
}
func TestGetName(t *testing.T) {
expected := "testPool"
got := redisPoolObj.GetName()
if expected != got {
t.Errorf("Expected to get %s, but got %s", expected, got)
return
}
}
func TestGetAddress(t *testing.T) {
expected := "10.1.0.21:6379"
got := redisPoolObj.GetAddress()
if expected != got {
t.Errorf("Expected to get %s, but got %s", expected, got)
return
}
}
func converInterfaceSliceToStringSlice(sourceList []interface{}) []string {
targetList := make([]string, 0, len(sourceList))
for _, item := range sourceList {
if item == nil {
targetList = append(targetList, "")
} else if item_str, ok := item.(string); ok {
targetList = append(targetList, item_str)
} else if item_bytes, ok2 := item.([]byte); ok2 {
targetList = append(targetList, string(item_bytes))
}
}
return targetList
}
func isTwoOrderedSliceEqual(list1, list2 []string) bool {
if list1 == nil && list2 == nil {
return true
}
if list1 == nil || list2 == nil {
return false
}
if len(list1) != len(list2) {
return false
}
for i := 0; i < len(list1); i++ {
if list1[i] != list2[i] {
return false
}
}
return true
}
func isTwoUnorderedSliceEqual(list1, list2 []string) bool {
if list1 == nil && list2 == nil {
return true
}
if list1 == nil || list2 == nil {
return false
}
if len(list1) != len(list2) {
return false
}
map1 := make(map[string]struct{})
map2 := make(map[string]struct{})
for _, item := range list1 {
map1[item] = struct{}{}
}
for _, item := range list2 {
map2[item] = struct{}{}
}
for k := range map1 {
if _, exist := map2[k]; !exist {
return false
}
}
return true
}
func getDistinctKeyList(keyList []string) []string {
distinctKeyList := make([]string, 0, len(keyList))
keyMap := make(map[string]struct{})
for _, key := range keyList {
if _, exist := keyMap[key]; !exist {
distinctKeyList = append(distinctKeyList, key)
keyMap[key] = struct{}{}
}
}
return distinctKeyList
}

View File

@@ -0,0 +1,587 @@
package typeUtil
import (
"testing"
"time"
)
func TestMapDataByte(t *testing.T) {
TestMapDataUint8(t)
}
func TestMapDataInt(t *testing.T) {
data := make(map[string]interface{})
mapData := NewMapData(data)
key := "key"
var expected int = 0
// Test when key doesn't exist
got, err := mapData.Int(key)
if err == nil {
t.Errorf("There should be an error, but now there isn't.")
return
}
// Test when key exist, but type doesn't match
mapData[key] = "abc"
got, err = mapData.Int(key)
if err == nil {
t.Errorf("There should be an error, but now there isn't.")
return
}
// Test when key exist and value matches
mapData[key] = 1
expected = 1
got, err = mapData.Int(key)
if err != nil {
t.Errorf("There should be no error, but now there is:%s", err)
return
}
if got != expected {
t.Errorf("Expected %d, but got %d", expected, got)
return
}
}
func TestMapDataInt8(t *testing.T) {
data := make(map[string]interface{})
mapData := NewMapData(data)
key := "key"
var expected int8 = 0
// Test when key doesn't exist
got, err := mapData.Int8(key)
if err == nil {
t.Errorf("There should be an error, but now there isn't.")
return
}
// Test when key exist, but type doesn't match
mapData[key] = "abc"
got, err = mapData.Int8(key)
if err == nil {
t.Errorf("There should be an error, but now there isn't.")
return
}
// Test when key exist and value matches
mapData[key] = 1
expected = 1
got, err = mapData.Int8(key)
if err != nil {
t.Errorf("There should be no error, but now there is:%s", err)
return
}
if got != expected {
t.Errorf("Expected %d, but got %d", expected, got)
return
}
}
func TestMapDataInt16(t *testing.T) {
data := make(map[string]interface{})
mapData := NewMapData(data)
key := "key"
var expected int16 = 0
// Test when key doesn't exist
got, err := mapData.Int16(key)
if err == nil {
t.Errorf("There should be an error, but now there isn't.")
return
}
// Test when key exist, but type doesn't match
mapData[key] = "abc"
got, err = mapData.Int16(key)
if err == nil {
t.Errorf("There should be an error, but now there isn't.")
return
}
// Test when key exist and value matches
mapData[key] = 1
expected = 1
got, err = mapData.Int16(key)
if err != nil {
t.Errorf("There should be no error, but now there is:%s", err)
return
}
if got != expected {
t.Errorf("Expected %d, but got %d", expected, got)
return
}
}
func TestMapDataInt32(t *testing.T) {
data := make(map[string]interface{})
mapData := NewMapData(data)
key := "key"
var expected int32 = 0
// Test when key doesn't exist
got, err := mapData.Int32(key)
if err == nil {
t.Errorf("There should be an error, but now there isn't.")
return
}
// Test when key exist, but type doesn't match
mapData[key] = "abc"
got, err = mapData.Int32(key)
if err == nil {
t.Errorf("There should be an error, but now there isn't.")
return
}
// Test when key exist and value matches
mapData[key] = 1
expected = 1
got, err = mapData.Int32(key)
if err != nil {
t.Errorf("There should be no error, but now there is:%s", err)
return
}
if got != expected {
t.Errorf("Expected %d, but got %d", expected, got)
return
}
}
func TestMapDataInt64(t *testing.T) {
data := make(map[string]interface{})
mapData := NewMapData(data)
key := "key"
var expected int64 = 0
// Test when key doesn't exist
got, err := mapData.Int64(key)
if err == nil {
t.Errorf("There should be an error, but now there isn't.")
return
}
// Test when key exist, but type doesn't match
mapData[key] = "abc"
got, err = mapData.Int64(key)
if err == nil {
t.Errorf("There should be an error, but now there isn't.")
return
}
// Test when key exist and value matches
mapData[key] = 1
expected = 1
got, err = mapData.Int64(key)
if err != nil {
t.Errorf("There should be no error, but now there is:%s", err)
return
}
if got != expected {
t.Errorf("Expected %d, but got %d", expected, got)
return
}
}
func TestMapDataUint(t *testing.T) {
data := make(map[string]interface{})
mapData := NewMapData(data)
key := "key"
var expected uint = 0
// Test when key doesn't exist
got, err := mapData.Uint(key)
if err == nil {
t.Errorf("There should be an error, but now there isn't.")
return
}
// Test when key exist, but type doesn't match
mapData[key] = "abc"
got, err = mapData.Uint(key)
if err == nil {
t.Errorf("There should be an error, but now there isn't.")
return
}
// Test when key exist and value matches
mapData[key] = 1
expected = 1
got, err = mapData.Uint(key)
if err != nil {
t.Errorf("There should be no error, but now there is:%s", err)
return
}
if got != expected {
t.Errorf("Expected %d, but got %d", expected, got)
return
}
}
func TestMapDataUint8(t *testing.T) {
data := make(map[string]interface{})
mapData := NewMapData(data)
key := "key"
var expected uint8 = 0
// Test when key doesn't exist
got, err := mapData.Uint8(key)
if err == nil {
t.Errorf("There should be an error, but now there isn't.")
return
}
// Test when key exist, but type doesn't match
mapData[key] = "abc"
got, err = mapData.Uint8(key)
if err == nil {
t.Errorf("There should be an error, but now there isn't.")
return
}
// Test when key exist and value matches
mapData[key] = 1
expected = 1
got, err = mapData.Uint8(key)
if err != nil {
t.Errorf("There should be no error, but now there is:%s", err)
return
}
if got != expected {
t.Errorf("Expected %d, but got %d", expected, got)
return
}
}
func TestMapDataUint16(t *testing.T) {
data := make(map[string]interface{})
mapData := NewMapData(data)
key := "key"
var expected uint16 = 0
// Test when key doesn't exist
got, err := mapData.Uint16(key)
if err == nil {
t.Errorf("There should be an error, but now there isn't.")
return
}
// Test when key exist, but type doesn't match
mapData[key] = "abc"
got, err = mapData.Uint16(key)
if err == nil {
t.Errorf("There should be an error, but now there isn't.")
return
}
// Test when key exist and value matches
mapData[key] = 1
expected = 1
got, err = mapData.Uint16(key)
if err != nil {
t.Errorf("There should be no error, but now there is:%s", err)
return
}
if got != expected {
t.Errorf("Expected %d, but got %d", expected, got)
return
}
}
func TestMapDataUint32(t *testing.T) {
data := make(map[string]interface{})
mapData := NewMapData(data)
key := "key"
var expected uint32 = 0
// Test when key doesn't exist
got, err := mapData.Uint32(key)
if err == nil {
t.Errorf("There should be an error, but now there isn't.")
return
}
// Test when key exist, but type doesn't match
mapData[key] = "abc"
got, err = mapData.Uint32(key)
if err == nil {
t.Errorf("There should be an error, but now there isn't.")
return
}
// Test when key exist and value matches
mapData[key] = 1
expected = 1
got, err = mapData.Uint32(key)
if err != nil {
t.Errorf("There should be no error, but now there is:%s", err)
return
}
if got != expected {
t.Errorf("Expected %d, but got %d", expected, got)
return
}
}
func TestMapDataUint64(t *testing.T) {
data := make(map[string]interface{})
mapData := NewMapData(data)
key := "key"
var expected uint64 = 0
// Test when key doesn't exist
got, err := mapData.Uint64(key)
if err == nil {
t.Errorf("There should be an error, but now there isn't.")
return
}
// Test when key exist, but type doesn't match
mapData[key] = "abc"
got, err = mapData.Uint64(key)
if err == nil {
t.Errorf("There should be an error, but now there isn't.")
return
}
// Test when key exist and value matches
mapData[key] = 1
expected = 1
got, err = mapData.Uint64(key)
if err != nil {
t.Errorf("There should be no error, but now there is:%s", err)
return
}
if got != expected {
t.Errorf("Expected %d, but got %d", expected, got)
return
}
}
func TestMapDataFloat32(t *testing.T) {
data := make(map[string]interface{})
mapData := NewMapData(data)
key := "key"
var expected float32 = 0
// Test when key doesn't exist
got, err := mapData.Float32(key)
if err == nil {
t.Errorf("There should be an error, but now there isn't.")
return
}
// Test when key exist, but type doesn't match
mapData[key] = "abc"
got, err = mapData.Float32(key)
if err == nil {
t.Errorf("There should be an error, but now there isn't.")
return
}
// Test when key exist and value matches
mapData[key] = 1
expected = 1
got, err = mapData.Float32(key)
if err != nil {
t.Errorf("There should be no error, but now there is:%s", err)
return
}
if got != expected {
t.Errorf("Expected %f, but got %f", expected, got)
return
}
}
func TestMapDataFloat64(t *testing.T) {
data := make(map[string]interface{})
mapData := NewMapData(data)
key := "key"
var expected float64 = 0
// Test when key doesn't exist
got, err := mapData.Float64(key)
if err == nil {
t.Errorf("There should be an error, but now there isn't.")
return
}
// Test when key exist, but type doesn't match
mapData[key] = "abc"
got, err = mapData.Float64(key)
if err == nil {
t.Errorf("There should be an error, but now there isn't.")
return
}
// Test when key exist and value matches
mapData[key] = 1
expected = 1
got, err = mapData.Float64(key)
if err != nil {
t.Errorf("There should be no error, but now there is:%s", err)
return
}
if got != expected {
t.Errorf("Expected %f, but got %f", expected, got)
return
}
}
func TestMapDataBool(t *testing.T) {
data := make(map[string]interface{})
mapData := NewMapData(data)
key := "key"
var expected bool = true
// Test when key doesn't exist
got, err := mapData.Bool(key)
if err == nil {
t.Errorf("There should be an error, but now there isn't.")
return
}
// Test when key exist, but type doesn't match
mapData[key] = "abc"
got, err = mapData.Bool(key)
if err == nil {
t.Errorf("There should be an error, but now there isn't.")
return
}
// Test when key exist and value matches
mapData[key] = true
expected = true
got, err = mapData.Bool(key)
if err != nil {
t.Errorf("There should be no error, but now there is:%s", err)
return
}
if got != expected {
t.Errorf("Expected %t, but got %t", expected, got)
return
}
}
func TestMapDataString(t *testing.T) {
data := make(map[string]interface{})
mapData := NewMapData(data)
key := "key"
var expected string = ""
// Test when key doesn't exist
got, err := mapData.String(key)
if err == nil {
t.Errorf("There should be an error, but now there isn't.")
return
}
// Test when key exist, but type doesn't match
mapData[key] = 123
expected = "123"
got, err = mapData.String(key)
if err != nil {
t.Errorf("There should be no error, but now there is:%s", err)
return
}
if got != expected {
t.Errorf("Expected %s, but got %s", expected, got)
return
}
// Test when key exist and value matches
mapData[key] = "hello"
expected = "hello"
got, err = mapData.String(key)
if err != nil {
t.Errorf("There should be no error, but now there is:%s", err)
return
}
if got != expected {
t.Errorf("Expected %s, but got %s", expected, got)
return
}
}
// Date(year int, month Month, day, hour, min, sec, nsec int, loc *Location) Time
func TestMapDataDateTime(t *testing.T) {
data := make(map[string]interface{})
mapData := NewMapData(data)
key := "key"
var expected time.Time = time.Date(2019, time.December, 25, 12, 0, 0, 0, time.UTC)
// Test when key doesn't exist
got, err := mapData.DateTime(key)
if err == nil {
t.Errorf("There should be an error, but now there isn't.")
return
}
// Test when key exist but value doesn't match
mapData[key] = "123"
expected = time.Date(2019, time.December, 25, 12, 0, 0, 0, time.UTC)
got, err = mapData.DateTime(key)
if err == nil {
t.Errorf("There should be an error, but now there isn't.")
return
}
// Test when key exist and value matches
mapData[key] = time.Date(2019, time.December, 25, 12, 0, 0, 0, time.UTC)
expected = time.Date(2019, time.December, 25, 12, 0, 0, 0, time.UTC)
got, err = mapData.DateTime(key)
if err != nil {
t.Errorf("There should be no error, but now there is:%s", err)
return
}
if got != expected {
t.Errorf("Expected %s, but got %s", expected, got)
return
}
}
func TestMapDataInterface(t *testing.T) {
data := make(map[string]interface{})
mapData := NewMapData(data)
key := "key"
var expected *Person
// Test when key doesn't exist
got, err := mapData.Interface(key)
if err == nil {
t.Errorf("There should be an error, but now there isn't.")
return
}
// Test when key exist and value matches
mapData[key] = NewPerson("Jordan", 34)
expected = NewPerson("Jordan", 34)
got, err = mapData.Interface(key)
if err != nil {
t.Errorf("There should be no error, but now there is:%s", err)
return
}
if gotPersonObj, ok := got.(*Person); !ok {
t.Errorf("Expected type *Person")
} else if gotPersonObj.SameAs(expected) == false {
t.Errorf("Expected %v, but got %v", expected, got)
return
}
}
type Person struct {
Name string
Age int
}
func (this *Person) SameAs(other *Person) bool {
return this.Name == other.Name && this.Age == other.Age
}
func NewPerson(name string, age int) *Person {
return &Person{
Name: name,
Age: age,
}
}

View File

@@ -0,0 +1,54 @@
package bytesSendUtil
import (
"fmt"
)
/*
实现sender接口
*/
type baseSender struct {
// 待发送的数据channel
waitingDataChan chan dataItem
// 失败数据缓存
cachedDataChan chan dataItem
// 用于停止协程
done chan struct{}
}
func newBaseSender() *baseSender {
return &baseSender{
waitingDataChan: make(chan dataItem, 1024),
cachedDataChan: make(chan dataItem, 1024000),
done: make(chan struct{}),
}
}
// Sender接口
// Send:
func (this *baseSender) Send() error {
// baseSender不实现发送
// 由tcpSender和httpSender实现发送
return fmt.Errorf("baseSender dose not have Send Method")
}
// Sender接口
// Data: 返回待发送的数据channel
func (this *baseSender) Data() <-chan dataItem {
return this.waitingDataChan
}
// Sender接口
// Cache返回失败数据缓存channel
func (this *baseSender) Cache() chan dataItem {
return this.cachedDataChan
}
// Sender接口
// Done返回channel用于判断是否关闭
func (this *baseSender) Done() <-chan struct{} {
return this.done
}