Skip to content

Commit

Permalink
chore: add more tests (zeromicro#3187)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevwan authored Apr 29, 2023
1 parent 14caf5c commit a31256b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
11 changes: 11 additions & 0 deletions core/logx/logtest/logtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,14 @@ func (b *Buffer) Reset() {
func (b *Buffer) String() string {
return b.buf.String()
}

func PanicOnFatal(t *testing.T) {
ok := logx.ExitOnFatal.CompareAndSwap(true, false)
if !ok {
return
}

t.Cleanup(func() {
logx.ExitOnFatal.CompareAndSwap(false, true)
})
}
8 changes: 7 additions & 1 deletion core/logx/logtest/logtest_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package logtest

import (
"errors"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -15,8 +16,13 @@ func TestCollector(t *testing.T) {
assert.Contains(t, c.String(), input)
}

func TestDiscard(t *testing.T) {
func TestPanicOnFatal(t *testing.T) {
const input = "hello"
Discard(t)
logx.Info(input)

PanicOnFatal(t)
assert.Panics(t, func() {
logx.Must(errors.New("foo"))
})
}
2 changes: 2 additions & 0 deletions core/syncx/atomicbool.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ func ForAtomicBool(val bool) *AtomicBool {
// CompareAndSwap compares current value with given old, if equals, set to given val.
func (b *AtomicBool) CompareAndSwap(old, val bool) bool {
var ov, nv uint32

if old {
ov = 1
}
if val {
nv = 1
}

return atomic.CompareAndSwapUint32((*uint32)(b), ov, nv)
}

Expand Down
22 changes: 22 additions & 0 deletions gateway/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import (

"github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/conf"
"github.com/zeromicro/go-zero/core/discov"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/logx/logtest"
"github.com/zeromicro/go-zero/internal/mock"
"github.com/zeromicro/go-zero/rest/httpc"
"github.com/zeromicro/go-zero/zrpc"
Expand Down Expand Up @@ -51,6 +53,8 @@ func TestMustNewServer(t *testing.T) {

s := MustNewServer(c, withDialer(func(conf zrpc.RpcClientConf) zrpc.Client {
return zrpc.MustNewClient(conf, zrpc.WithDialOption(grpc.WithContextDialer(dialer())))
}), WithHeaderProcessor(func(header http.Header) []string {
return []string{"foo"}
}))
s.upstreams = []Upstream{
{
Expand All @@ -77,6 +81,7 @@ func TestMustNewServer(t *testing.T) {

assert.NoError(t, s.build())
go s.Server.Start()
defer s.Stop()

time.Sleep(time.Millisecond * 200)

Expand All @@ -103,3 +108,20 @@ func TestServer_ensureUpstreamNames(t *testing.T) {
assert.NoError(t, s.ensureUpstreamNames())
assert.Equal(t, "target", s.upstreams[0].Name)
}

func TestServer_ensureUpstreamNames_badEtcd(t *testing.T) {
var s = Server{
upstreams: []Upstream{
{
Grpc: zrpc.RpcClientConf{
Etcd: discov.EtcdConf{},
},
},
},
}

logtest.PanicOnFatal(t)
assert.Panics(t, func() {
s.Start()
})
}

0 comments on commit a31256b

Please sign in to comment.