remove dependency on assert lib

pull/1516/head
Darien Raymond 2019-01-18 15:59:39 +01:00
parent c0fe58e4a4
commit 8e131bcd1f
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
5 changed files with 100 additions and 85 deletions

View File

@ -9,33 +9,34 @@ import (
"v2ray.com/core/common" "v2ray.com/core/common"
. "v2ray.com/core/common/buf" . "v2ray.com/core/common/buf"
. "v2ray.com/ext/assert"
) )
func TestMultiBufferRead(t *testing.T) { func TestMultiBufferRead(t *testing.T) {
assert := With(t)
b1 := New() b1 := New()
b1.WriteString("ab") common.Must2(b1.WriteString("ab"))
b2 := New() b2 := New()
b2.WriteString("cd") common.Must2(b2.WriteString("cd"))
mb := MultiBuffer{b1, b2} mb := MultiBuffer{b1, b2}
bs := make([]byte, 32) bs := make([]byte, 32)
_, nBytes := SplitBytes(mb, bs) _, nBytes := SplitBytes(mb, bs)
assert(nBytes, Equals, 4) if nBytes != 4 {
assert(bs[:nBytes], Equals, []byte("abcd")) t.Error("expect 4 bytes split, but got ", nBytes)
}
if r := cmp.Diff(bs[:nBytes], []byte("abcd")); r != "" {
t.Error(r)
}
} }
func TestMultiBufferAppend(t *testing.T) { func TestMultiBufferAppend(t *testing.T) {
assert := With(t)
var mb MultiBuffer var mb MultiBuffer
b := New() b := New()
b.WriteString("ab") common.Must2(b.WriteString("ab"))
mb = append(mb, b) mb = append(mb, b)
assert(mb.Len(), Equals, int32(2)) if mb.Len() != 2 {
t.Error("expected length 2, but got ", mb.Len())
}
} }
func TestMultiBufferSliceBySizeLarge(t *testing.T) { func TestMultiBufferSliceBySizeLarge(t *testing.T) {

View File

@ -5,25 +5,40 @@ import (
"testing" "testing"
. "v2ray.com/core/common" . "v2ray.com/core/common"
. "v2ray.com/ext/assert"
) )
func TestMust(t *testing.T) { func TestMust(t *testing.T) {
assert := With(t) hasPanic := func(f func()) (ret bool) {
defer func() {
f := func() error { if r := recover(); r != nil {
return errors.New("test error") ret = true
}
}()
f()
return false
} }
assert(func() { Must(f()) }, Panics) testCases := []struct {
} Input func()
Panic bool
func TestMust2(t *testing.T) { }{
assert := With(t) {
Panic: true,
f := func() (interface{}, error) { Input: func() { Must(func() error { return errors.New("test error") }()) },
return nil, errors.New("test error") },
{
Panic: true,
Input: func() { Must2(func() (int, error) { return 0, errors.New("test error") }()) },
},
{
Panic: false,
Input: func() { Must(func() error { return nil }()) },
},
} }
assert(func() { Must2(f()) }, Panics) for idx, test := range testCases {
if hasPanic(test.Input) != test.Panic {
t.Error("test case #", idx, " expect panic ", test.Panic, " but actually not")
}
}
} }

View File

@ -4,17 +4,15 @@ import (
"context" "context"
"testing" "testing"
"v2ray.com/core/common"
"v2ray.com/core/common/buf" "v2ray.com/core/common/buf"
. "v2ray.com/core/transport/internet/headers/srtp" . "v2ray.com/core/transport/internet/headers/srtp"
. "v2ray.com/ext/assert"
) )
func TestSRTPWrite(t *testing.T) { func TestSRTPWrite(t *testing.T) {
assert := With(t)
content := []byte{'a', 'b', 'c', 'd', 'e', 'f', 'g'} content := []byte{'a', 'b', 'c', 'd', 'e', 'f', 'g'}
srtpRaw, err := New(context.Background(), &Config{}) srtpRaw, err := New(context.Background(), &Config{})
assert(err, IsNil) common.Must(err)
srtp := srtpRaw.(*SRTP) srtp := srtpRaw.(*SRTP)
@ -22,5 +20,8 @@ func TestSRTPWrite(t *testing.T) {
srtp.Serialize(payload.Extend(srtp.Size())) srtp.Serialize(payload.Extend(srtp.Size()))
payload.Write(content) payload.Write(content)
assert(payload.Len(), Equals, int32(len(content))+srtp.Size()) expectedLen := int32(len(content)) + srtp.Size()
if payload.Len() != expectedLen {
t.Error("expected ", expectedLen, " of bytes, but got ", payload.Len())
}
} }

View File

@ -3,13 +3,13 @@ package kcp_test
import ( import (
"testing" "testing"
"github.com/google/go-cmp/cmp"
"v2ray.com/core/common"
. "v2ray.com/core/transport/internet/kcp" . "v2ray.com/core/transport/internet/kcp"
. "v2ray.com/ext/assert"
) )
func TestSimpleAuthenticator(t *testing.T) { func TestSimpleAuthenticator(t *testing.T) {
assert := With(t)
cache := make([]byte, 512) cache := make([]byte, 512)
payload := []byte{'a', 'b', 'c', 'd', 'e', 'f', 'g'} payload := []byte{'a', 'b', 'c', 'd', 'e', 'f', 'g'}
@ -17,13 +17,13 @@ func TestSimpleAuthenticator(t *testing.T) {
auth := NewSimpleAuthenticator() auth := NewSimpleAuthenticator()
b := auth.Seal(cache[:0], nil, payload, nil) b := auth.Seal(cache[:0], nil, payload, nil)
c, err := auth.Open(cache[:0], nil, b, nil) c, err := auth.Open(cache[:0], nil, b, nil)
assert(err, IsNil) common.Must(err)
assert(c, Equals, payload) if r := cmp.Diff(c, payload); r != "" {
t.Error(r)
}
} }
func TestSimpleAuthenticator2(t *testing.T) { func TestSimpleAuthenticator2(t *testing.T) {
assert := With(t)
cache := make([]byte, 512) cache := make([]byte, 512)
payload := []byte{'a', 'b'} payload := []byte{'a', 'b'}
@ -31,6 +31,8 @@ func TestSimpleAuthenticator2(t *testing.T) {
auth := NewSimpleAuthenticator() auth := NewSimpleAuthenticator()
b := auth.Seal(cache[:0], nil, payload, nil) b := auth.Seal(cache[:0], nil, payload, nil)
c, err := auth.Open(cache[:0], nil, b, nil) c, err := auth.Open(cache[:0], nil, b, nil)
assert(err, IsNil) common.Must(err)
assert(c, Equals, payload) if r := cmp.Diff(c, payload); r != "" {
t.Error(r)
}
} }

View File

@ -1,19 +1,17 @@
package pipe_test package pipe_test
import ( import (
"context" "errors"
"io" "io"
"sync"
"testing" "testing"
"time" "time"
"github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp"
"golang.org/x/sync/errgroup"
"v2ray.com/core/common" "v2ray.com/core/common"
"v2ray.com/core/common/buf" "v2ray.com/core/common/buf"
"v2ray.com/core/common/task"
. "v2ray.com/core/transport/pipe" . "v2ray.com/core/transport/pipe"
. "v2ray.com/ext/assert"
) )
func TestPipeReadWrite(t *testing.T) { func TestPipeReadWrite(t *testing.T) {
@ -52,37 +50,41 @@ func TestPipeInterrupt(t *testing.T) {
} }
func TestPipeClose(t *testing.T) { func TestPipeClose(t *testing.T) {
assert := With(t)
pReader, pWriter := New(WithSizeLimit(1024)) pReader, pWriter := New(WithSizeLimit(1024))
payload := []byte{'a', 'b', 'c', 'd'} payload := []byte{'a', 'b', 'c', 'd'}
b := buf.New() b := buf.New()
b.Write(payload) common.Must2(b.Write(payload))
assert(pWriter.WriteMultiBuffer(buf.MultiBuffer{b}), IsNil) common.Must(pWriter.WriteMultiBuffer(buf.MultiBuffer{b}))
assert(pWriter.Close(), IsNil) common.Must(pWriter.Close())
rb, err := pReader.ReadMultiBuffer() rb, err := pReader.ReadMultiBuffer()
assert(err, IsNil) common.Must(err)
assert(rb.String(), Equals, b.String()) if rb.String() != string(payload) {
t.Fatal("expect content ", string(payload), " but actually ", rb.String())
}
rb, err = pReader.ReadMultiBuffer() rb, err = pReader.ReadMultiBuffer()
assert(err, Equals, io.EOF) if err != io.EOF {
assert(rb.IsEmpty(), IsTrue) t.Fatal("expected EOF, but got ", err)
}
if !rb.IsEmpty() {
t.Fatal("expect empty buffer, but got ", rb.String())
}
} }
func TestPipeLimitZero(t *testing.T) { func TestPipeLimitZero(t *testing.T) {
assert := With(t)
pReader, pWriter := New(WithSizeLimit(0)) pReader, pWriter := New(WithSizeLimit(0))
bb := buf.New() bb := buf.New()
bb.Write([]byte{'a', 'b'}) common.Must2(bb.Write([]byte{'a', 'b'}))
assert(pWriter.WriteMultiBuffer(buf.MultiBuffer{bb}), IsNil) common.Must(pWriter.WriteMultiBuffer(buf.MultiBuffer{bb}))
err := task.Run(context.Background(), func() error { var errg errgroup.Group
errg.Go(func() error {
b := buf.New() b := buf.New()
b.Write([]byte{'c', 'd'}) b.Write([]byte{'c', 'd'})
return pWriter.WriteMultiBuffer(buf.MultiBuffer{b}) return pWriter.WriteMultiBuffer(buf.MultiBuffer{b})
}, func() error { })
errg.Go(func() error {
time.Sleep(time.Second) time.Sleep(time.Second)
var container buf.MultiBufferContainer var container buf.MultiBufferContainer
@ -90,49 +92,43 @@ func TestPipeLimitZero(t *testing.T) {
return err return err
} }
assert(container.String(), Equals, "abcd") if r := cmp.Diff(container.String(), "abcd"); r != "" {
return nil return errors.New(r)
}, func() error { }
time.Sleep(time.Second * 2)
pWriter.Close()
return nil return nil
}) })
errg.Go(func() error {
assert(err, IsNil) time.Sleep(time.Second * 2)
return pWriter.Close()
})
if err := errg.Wait(); err != nil {
t.Error(err)
}
} }
func TestPipeWriteMultiThread(t *testing.T) { func TestPipeWriteMultiThread(t *testing.T) {
assert := With(t)
pReader, pWriter := New(WithSizeLimit(0)) pReader, pWriter := New(WithSizeLimit(0))
var wg sync.WaitGroup var errg errgroup.Group
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
wg.Add(1) errg.Go(func() error {
go func() {
b := buf.New() b := buf.New()
b.WriteString("abcd") b.WriteString("abcd")
pWriter.WriteMultiBuffer(buf.MultiBuffer{b}) return pWriter.WriteMultiBuffer(buf.MultiBuffer{b})
wg.Done() })
}()
} }
errg.Wait()
time.Sleep(time.Millisecond * 100)
pWriter.Close()
wg.Wait()
b, err := pReader.ReadMultiBuffer() b, err := pReader.ReadMultiBuffer()
assert(err, IsNil) common.Must(err)
assert(b[0].Bytes(), Equals, []byte{'a', 'b', 'c', 'd'}) if r := cmp.Diff(b[0].Bytes(), []byte{'a', 'b', 'c', 'd'}); r != "" {
t.Error(r)
}
} }
func TestInterfaces(t *testing.T) { func TestInterfaces(t *testing.T) {
assert := With(t) _ = (buf.Reader)(new(Reader))
_ = (buf.TimeoutReader)(new(Reader))
assert((*Reader)(nil), Implements, (*buf.Reader)(nil))
assert((*Reader)(nil), Implements, (*buf.TimeoutReader)(nil))
} }
func BenchmarkPipeReadWrite(b *testing.B) { func BenchmarkPipeReadWrite(b *testing.B) {