Repository: zeromicro/go-zero
Pull request #3187 (2023-04-29 14:41:35)

chore: add more tests

codecov[bot]: ## [Codecov](https://codecov.io/gh/zeromicro/go-zero/pull/3187?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) Report
> Merging [#3187](https://codecov.io/gh/zeromicro/go-zero/pull/3187?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (f06a3a5) into [master](https://codecov.io/gh/zeromicro/go-zero/commit/14caf5c7995ac5f39a5a027870c14515422d2cc6?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (14caf5c) will **increase** coverage by `0.08%`.
> The diff coverage is `100.00%`.


| [Impacted Files](https://codecov.io/gh/zeromicro/go-zero/pull/3187?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) | Coverage Δ | |
|---|---|---|
| [core/syncx/atomicbool.go](https://codecov.io/gh/zeromicro/go-zero/pull/3187?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Y29yZS9zeW5jeC9hdG9taWNib29sLmdv) | `100.00% <100.00%> (ø)` | |

... and [2 files with indirect coverage changes](https://codecov.io/gh/zeromicro/go-zero/pull/3187/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None)


Patch:
diff --git a/core/syncx/atomicbool.go b/core/syncx/atomicbool.go
index 7e489d9b..751b8306 100644
--- a/core/syncx/atomicbool.go
+++ b/core/syncx/atomicbool.go
@@ -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)
 }
 


Test patch:
diff --git a/core/logx/logtest/logtest.go b/core/logx/logtest/logtest.go
index 53c2db43..6e583840 100644
--- a/core/logx/logtest/logtest.go
+++ b/core/logx/logtest/logtest.go
@@ -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)
+	})
+}
diff --git a/core/logx/logtest/logtest_test.go b/core/logx/logtest/logtest_test.go
index 1a61e07a..da7c3f79 100644
--- a/core/logx/logtest/logtest_test.go
+++ b/core/logx/logtest/logtest_test.go
@@ -1,6 +1,7 @@
 package logtest
 
 import (
+	"errors"
 	"testing"
 
 	"github.com/stretchr/testify/assert"
@@ -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"))
+	})
 }
diff --git a/gateway/server_test.go b/gateway/server_test.go
index 6d21c2f1..74168559 100644
--- a/gateway/server_test.go
+++ b/gateway/server_test.go
@@ -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"
@@ -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{
 		{
@@ -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)
 
@@ -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()
+	})
+}