improve codes

This commit is contained in:
John 2019-06-19 23:56:14 +08:00
parent 234d734981
commit e5fa341f39
7 changed files with 61 additions and 57 deletions

View File

@ -9,9 +9,10 @@
package gcrc32_test
import (
"github.com/gogf/gf/g/crypto/gmd5"
"testing"
"github.com/gogf/gf/g/crypto/gmd5"
"github.com/gogf/gf/g/crypto/gcrc32"
"github.com/gogf/gf/g/test/gtest"
)
@ -36,7 +37,7 @@ func TestEncrypt(t *testing.T) {
gtest.AssertEQ(int(encrypt1), result)
gtest.AssertEQ(int(encrypt2), result)
strmd5 := gmd5.Encrypt(s)
strmd5, _ := gmd5.Encrypt(s)
test1 := gcrc32.Encrypt(strmd5)
test2 := gcrc32.Encrypt([]byte(strmd5))
gtest.AssertEQ(test2, test1)

View File

@ -30,11 +30,11 @@ type user struct {
func TestEncrypt(t *testing.T) {
gtest.Case(t, func() {
encryptString := gmd5.Encrypt(s)
encryptString, _ := gmd5.Encrypt(s)
gtest.Assert(encryptString, result)
result := "1427562bb29f88a1161590b76398ab72"
encrypt := gmd5.Encrypt(123456)
encrypt, _ := gmd5.Encrypt(123456)
gtest.AssertEQ(encrypt, result)
})
@ -45,14 +45,14 @@ func TestEncrypt(t *testing.T) {
age: 23,
}
result := "70917ebce8bd2f78c736cda63870fb39"
encrypt := gmd5.Encrypt(user)
encrypt, _ := gmd5.Encrypt(user)
gtest.AssertEQ(encrypt, result)
})
}
func TestEncryptString(t *testing.T) {
gtest.Case(t, func() {
encryptString := gmd5.EncryptString(s)
encryptString, _ := gmd5.EncryptString(s)
gtest.Assert(encryptString, result)
})
}
@ -66,11 +66,11 @@ func TestEncryptFile(t *testing.T) {
defer os.Remove(path)
defer file.Close()
gtest.Assert(err, nil)
file.Write([]byte("Hello Go Frame"))
encryptFile := gmd5.EncryptFile(path)
_, _ = file.Write([]byte("Hello Go Frame"))
encryptFile, _ := gmd5.EncryptFile(path)
gtest.AssertEQ(encryptFile, result)
// when the file is not exist,encrypt will return empty string
errEncrypt := gmd5.EncryptFile(errorPath)
errEncrypt, _ := gmd5.EncryptFile(errorPath)
gtest.AssertEQ(errEncrypt, "")
})

View File

@ -57,11 +57,11 @@ func TestEncryptFile(t *testing.T) {
defer os.Remove(path)
defer file.Close()
gtest.Assert(err, nil)
file.Write([]byte("Hello Go Frame"))
encryptFile := gsha1.EncryptFile(path)
_, _ = file.Write([]byte("Hello Go Frame"))
encryptFile, _ := gsha1.EncryptFile(path)
gtest.AssertEQ(encryptFile, result)
// when the file is not exist,encrypt will return empty string
errEncrypt := gsha1.EncryptFile(errPath)
errEncrypt, _ := gsha1.EncryptFile(errPath)
gtest.AssertEQ(errEncrypt, "")
})
}

View File

@ -7,23 +7,29 @@
package gcompress_test
import (
"testing"
"github.com/gogf/gf/g/encoding/gcompress"
"github.com/gogf/gf/g/test/gtest"
"testing"
)
func TestZlib(t *testing.T) {
gtest.Case(t, func() {
src := "hello, world\n"
dst := []byte{120, 156, 202, 72, 205, 201, 201, 215, 81, 40, 207, 47, 202, 73, 225, 2, 4, 0, 0, 255, 255, 33, 231, 4, 147}
gtest.Assert(gcompress.Zlib([]byte(src)), dst)
data, _ := gcompress.Zlib([]byte(src))
gtest.Assert(data, dst)
gtest.Assert(gcompress.UnZlib(dst), []byte(src))
data, _ = gcompress.UnZlib(dst)
gtest.Assert(data, []byte(src))
gtest.Assert(gcompress.Zlib(nil), nil)
gtest.Assert(gcompress.UnZlib(nil), nil)
data, _ = gcompress.Zlib(nil)
gtest.Assert(data, nil)
data, _ = gcompress.UnZlib(nil)
gtest.Assert(data, nil)
gtest.Assert(gcompress.UnZlib(dst[1:]), nil)
data, _ = gcompress.UnZlib(dst[1:])
gtest.Assert(data, nil)
})
}
@ -43,9 +49,12 @@ func TestGzip(t *testing.T) {
}
arr := []byte(src)
gtest.Assert(gcompress.Gzip(arr), gzip)
data, _ := gcompress.Gzip(arr)
gtest.Assert(data, gzip)
gtest.Assert(gcompress.UnGzip(gzip), arr)
data, _ = gcompress.UnGzip(gzip)
gtest.Assert(data, arr)
gtest.Assert(gcompress.UnGzip(gzip[1:]), nil)
data, _ = gcompress.UnGzip(gzip[1:])
gtest.Assert(data, nil)
}

View File

@ -7,6 +7,7 @@
package gudp
import (
"errors"
"io"
"net"
"time"
@ -14,9 +15,8 @@ import (
// 封装的UDP链接对象
type Conn struct {
conn *net.UDPConn // 底层链接对象
*net.UDPConn // 底层链接对象
raddr *net.UDPAddr // 远程地址
buffer []byte // 读取缓冲区(用于数据读取时的缓冲区处理)
recvDeadline time.Time // 读取超时时间
sendDeadline time.Time // 写入超时时间
recvBufferWait time.Duration // 读取全部缓冲区数据时,读取完毕后的写入等待间隔
@ -45,7 +45,7 @@ func NewConn(raddr string, laddr ...string) (*Conn, error) {
// 将*net.UDPConn对象转换为*Conn对象
func NewConnByNetConn(udp *net.UDPConn) *Conn {
return &Conn{
conn: udp,
UDPConn: udp,
recvDeadline: time.Time{},
sendDeadline: time.Time{},
recvBufferWait: gRECV_ALL_WAIT_TIMEOUT,
@ -56,9 +56,9 @@ func NewConnByNetConn(udp *net.UDPConn) *Conn {
func (c *Conn) Send(data []byte, retry ...Retry) (err error) {
for {
if c.raddr != nil {
_, err = c.conn.WriteToUDP(data, c.raddr)
_, err = c.WriteToUDP(data, c.raddr)
} else {
_, err = c.conn.Write(data)
_, err = c.Write(data)
}
if err != nil {
// 链接已关闭
@ -104,11 +104,11 @@ func (c *Conn) Recv(length int, retry ...Retry) ([]byte, error) {
for {
if length < 0 && index > 0 {
bufferWait = true
if err = c.conn.SetReadDeadline(time.Now().Add(c.recvBufferWait)); err != nil {
if err = c.SetReadDeadline(time.Now().Add(c.recvBufferWait)); err != nil {
return nil, err
}
}
size, raddr, err = c.conn.ReadFromUDP(buffer[index:])
size, raddr, err = c.ReadFromUDP(buffer[index:])
if err == nil {
c.raddr = raddr
}
@ -138,7 +138,7 @@ func (c *Conn) Recv(length int, retry ...Retry) ([]byte, error) {
}
// 判断数据是否全部读取完毕(由于超时机制的存在,获取的数据完整性不可靠)
if bufferWait && isTimeout(err) {
if err = c.conn.SetReadDeadline(c.recvDeadline); err != nil {
if err = c.SetReadDeadline(c.recvDeadline); err != nil {
return nil, err
}
err = nil
@ -176,21 +176,31 @@ func (c *Conn) SendRecv(data []byte, receive int, retry ...Retry) ([]byte, error
}
// 带超时时间的数据获取
func (c *Conn) RecvWithTimeout(length int, timeout time.Duration, retry ...Retry) ([]byte, error) {
func (c *Conn) RecvWithTimeout(length int, timeout time.Duration, retry ...Retry) (data []byte, err error) {
if err := c.SetRecvDeadline(time.Now().Add(timeout)); err != nil {
return nil, err
}
defer c.SetRecvDeadline(time.Time{})
return c.Recv(length, retry...)
defer func() {
if e := c.SetRecvDeadline(time.Time{}); e != nil {
err = errors.New(err.Error() + "; " + e.Error())
}
}()
data, err = c.Recv(length, retry...)
return
}
// 带超时时间的数据发送
func (c *Conn) SendWithTimeout(data []byte, timeout time.Duration, retry ...Retry) error {
func (c *Conn) SendWithTimeout(data []byte, timeout time.Duration, retry ...Retry) (err error) {
if err := c.SetSendDeadline(time.Now().Add(timeout)); err != nil {
return err
}
defer c.SetSendDeadline(time.Time{})
return c.Send(data, retry...)
defer func() {
if e := c.SetSendDeadline(time.Time{}); e != nil {
err = errors.New(err.Error() + "; " + e.Error())
}
}()
err = c.Send(data, retry...)
return
}
// 发送数据并等待接收返回数据(带返回超时等待时间)
@ -203,7 +213,7 @@ func (c *Conn) SendRecvWithTimeout(data []byte, receive int, timeout time.Durati
}
func (c *Conn) SetDeadline(t time.Time) error {
err := c.conn.SetDeadline(t)
err := c.UDPConn.SetDeadline(t)
if err == nil {
c.recvDeadline = t
c.sendDeadline = t
@ -212,7 +222,7 @@ func (c *Conn) SetDeadline(t time.Time) error {
}
func (c *Conn) SetRecvDeadline(t time.Time) error {
err := c.conn.SetReadDeadline(t)
err := c.SetReadDeadline(t)
if err == nil {
c.recvDeadline = t
}
@ -220,7 +230,7 @@ func (c *Conn) SetRecvDeadline(t time.Time) error {
}
func (c *Conn) SetSendDeadline(t time.Time) error {
err := c.conn.SetWriteDeadline(t)
err := c.SetWriteDeadline(t)
if err == nil {
c.sendDeadline = t
}
@ -233,17 +243,9 @@ func (c *Conn) SetRecvBufferWait(d time.Duration) {
c.recvBufferWait = d
}
func (c *Conn) LocalAddr() net.Addr {
return c.conn.LocalAddr()
}
// 不能使用c.conn.RemoteAddr()其返回为nil
// 这里使用c.raddr获取远程连接地址。
func (c *Conn) RemoteAddr() net.Addr {
//return c.conn.RemoteAddr()
return c.raddr
}
func (c *Conn) Close() error {
return c.conn.Close()
}

View File

@ -10,16 +10,6 @@ import (
"net"
)
// Deprecated.
// 常见的二进制数据校验方式,生成校验结果
func Checksum(buffer []byte) uint32 {
var checksum uint32
for _, b := range buffer {
checksum += uint32(b)
}
return checksum
}
// 创建标准库UDP链接操作对象
func NewNetConn(raddr string, laddr ...string) (*net.UDPConn, error) {
var err error

View File

@ -237,7 +237,9 @@ func Test_Instance(t *testing.T) {
path := gcfg.DEFAULT_CONFIG_FILE
err := gfile.PutContents(path, config)
gtest.Assert(err, nil)
defer gtest.Assert(gfile.Remove(path), nil)
defer func() {
gtest.Assert(gfile.Remove(path), nil)
}()
c := gcfg.Instance()
gtest.Assert(c.Get("v1"), 1)