Repository: zeromicro/go-zero
Pull request #2815 (2023-01-24 04:27:03)

chore: add more tests

codecov-commenter: # [Codecov](https://codecov.io/gh/zeromicro/go-zero/pull/2815?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) Report
> Merging [#2815](https://codecov.io/gh/zeromicro/go-zero/pull/2815?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (1a8c846) into [master](https://codecov.io/gh/zeromicro/go-zero/commit/ceab56442987e6ec61707faa276ba360aca90fa9?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (ceab564) will **increase** coverage by `0.02%`.
> The diff coverage is `100.00%`.


| [Impacted Files](https://codecov.io/gh/zeromicro/go-zero/pull/2815?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) | Coverage Δ | |
|---|---|---|
| [core/proc/shutdown.go](https://codecov.io/gh/zeromicro/go-zero/pull/2815?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Y29yZS9wcm9jL3NodXRkb3duLmdv) | `71.42% <100.00%> (+1.42%)` | :arrow_up: |
| [core/logx/rotatelogger.go](https://codecov.io/gh/zeromicro/go-zero/pull/2815?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Y29yZS9sb2d4L3JvdGF0ZWxvZ2dlci5nbw==) | `72.69% <0.00%> (-1.11%)` | :arrow_down: |
| [zrpc/internal/rpcserver.go](https://codecov.io/gh/zeromicro/go-zero/pull/2815?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-enJwYy9pbnRlcm5hbC9ycGNzZXJ2ZXIuZ28=) | `100.00% <0.00%> (+5.68%)` | :arrow_up: |
| [rest/internal/starter.go](https://codecov.io/gh/zeromicro/go-zero/pull/2815?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-cmVzdC9pbnRlcm5hbC9zdGFydGVyLmdv) | `80.64% <0.00%> (+6.45%)` | :arrow_up: |


Patch:
diff --git a/core/proc/shutdown.go b/core/proc/shutdown.go
index 11372b45..5a97ff27 100644
--- a/core/proc/shutdown.go
+++ b/core/proc/shutdown.go
@@ -43,6 +43,16 @@ func SetTimeToForceQuit(duration time.Duration) {
 	delayTimeBeforeForceQuit = duration
 }
 
+// Shutdown calls the registered shutdown listeners, only for test purpose.
+func Shutdown() {
+	shutdownListeners.notifyListeners()
+}
+
+// WrapUp wraps up the process, only for test purpose.
+func WrapUp() {
+	wrapUpListeners.notifyListeners()
+}
+
 func gracefulStop(signals chan os.Signal) {
 	signal.Stop(signals)
 


Test patch:
diff --git a/core/executors/chunkexecutor_test.go b/core/executors/chunkexecutor_test.go
index b78430d7..cd013b82 100644
--- a/core/executors/chunkexecutor_test.go
+++ b/core/executors/chunkexecutor_test.go
@@ -53,10 +53,11 @@ func TestChunkExecutorFlushInterval(t *testing.T) {
 }
 
 func TestChunkExecutorEmpty(t *testing.T) {
-	NewChunkExecutor(func(items []interface{}) {
+	executor := NewChunkExecutor(func(items []interface{}) {
 		assert.Fail(t, "should not called")
 	}, WithChunkBytes(10), WithFlushInterval(time.Millisecond))
 	time.Sleep(time.Millisecond * 100)
+	executor.Wait()
 }
 
 func TestChunkExecutorFlush(t *testing.T) {
diff --git a/core/executors/periodicalexecutor_test.go b/core/executors/periodicalexecutor_test.go
index 46197003..8815484b 100644
--- a/core/executors/periodicalexecutor_test.go
+++ b/core/executors/periodicalexecutor_test.go
@@ -8,6 +8,7 @@ import (
 	"time"
 
 	"github.com/stretchr/testify/assert"
+	"github.com/zeromicro/go-zero/core/proc"
 	"github.com/zeromicro/go-zero/core/timex"
 )
 
@@ -67,6 +68,7 @@ func TestPeriodicalExecutor_QuitGoroutine(t *testing.T) {
 	ticker.Tick()
 	ticker.Wait(time.Millisecond * idleRound)
 	assert.Equal(t, routines, runtime.NumGoroutine())
+	proc.Shutdown()
 }
 
 func TestPeriodicalExecutor_Bulk(t *testing.T) {
diff --git a/core/metric/counter_test.go b/core/metric/counter_test.go
index 145d1b20..832bdc69 100644
--- a/core/metric/counter_test.go
+++ b/core/metric/counter_test.go
@@ -5,6 +5,7 @@ import (
 
 	"github.com/prometheus/client_golang/prometheus/testutil"
 	"github.com/stretchr/testify/assert"
+	"github.com/zeromicro/go-zero/core/proc"
 	"github.com/zeromicro/go-zero/core/prometheus"
 )
 
@@ -17,6 +18,9 @@ func TestNewCounterVec(t *testing.T) {
 	})
 	defer counterVec.close()
 	counterVecNil := NewCounterVec(nil)
+	counterVec.Inc("path", "code")
+	counterVec.Add(1, "path", "code")
+	proc.Shutdown()
 	assert.NotNil(t, counterVec)
 	assert.Nil(t, counterVecNil)
 }
diff --git a/core/metric/gauge_test.go b/core/metric/gauge_test.go
index ad3ea549..fb070260 100644
--- a/core/metric/gauge_test.go
+++ b/core/metric/gauge_test.go
@@ -5,6 +5,7 @@ import (
 
 	"github.com/prometheus/client_golang/prometheus/testutil"
 	"github.com/stretchr/testify/assert"
+	"github.com/zeromicro/go-zero/core/proc"
 )
 
 func TestNewGaugeVec(t *testing.T) {
@@ -18,6 +19,8 @@ func TestNewGaugeVec(t *testing.T) {
 	gaugeVecNil := NewGaugeVec(nil)
 	assert.NotNil(t, gaugeVec)
 	assert.Nil(t, gaugeVecNil)
+
+	proc.Shutdown()
 }
 
 func TestGaugeInc(t *testing.T) {
diff --git a/core/metric/histogram_test.go b/core/metric/histogram_test.go
index 4874617b..4c2f8933 100644
--- a/core/metric/histogram_test.go
+++ b/core/metric/histogram_test.go
@@ -6,6 +6,7 @@ import (
 
 	"github.com/prometheus/client_golang/prometheus/testutil"
 	"github.com/stretchr/testify/assert"
+	"github.com/zeromicro/go-zero/core/proc"
 )
 
 func TestNewHistogramVec(t *testing.T) {
@@ -47,4 +48,6 @@ func TestHistogramObserve(t *testing.T) {
 
 	err := testutil.CollectAndCompare(hv.histogram, strings.NewReader(metadata+val))
 	assert.Nil(t, err)
+
+	proc.Shutdown()
 }
diff --git a/core/proc/shutdown_test.go b/core/proc/shutdown_test.go
index 415e09e5..3ab80b6a 100644
--- a/core/proc/shutdown_test.go
+++ b/core/proc/shutdown_test.go
@@ -18,14 +18,14 @@ func TestShutdown(t *testing.T) {
 	called := AddWrapUpListener(func() {
 		val++
 	})
-	wrapUpListeners.notifyListeners()
+	WrapUp()
 	called()
 	assert.Equal(t, 1, val)
 
 	called = AddShutdownListener(func() {
 		val += 2
 	})
-	shutdownListeners.notifyListeners()
+	Shutdown()
 	called()
 	assert.Equal(t, 3, val)
 }
diff --git a/core/service/serviceconf_test.go b/core/service/serviceconf_test.go
index 5af0d06f..badf249f 100644
--- a/core/service/serviceconf_test.go
+++ b/core/service/serviceconf_test.go
@@ -3,6 +3,7 @@ package service
 import (
 	"testing"
 
+	"github.com/stretchr/testify/assert"
 	"github.com/zeromicro/go-zero/core/logx"
 )
 
@@ -16,3 +17,15 @@ func TestServiceConf(t *testing.T) {
 	}
 	c.MustSetUp()
 }
+
+func TestServiceConfWithMetricsUrl(t *testing.T) {
+	c := ServiceConf{
+		Name: "foo",
+		Log: logx.LogConf{
+			Mode: "volume",
+		},
+		Mode:       "dev",
+		MetricsUrl: "http://localhost:8080",
+	}
+	assert.NoError(t, c.SetUp())
+}
diff --git a/core/service/servicegroup_test.go b/core/service/servicegroup_test.go
index b7a52ba3..e2957368 100644
--- a/core/service/servicegroup_test.go
+++ b/core/service/servicegroup_test.go
@@ -5,6 +5,7 @@ import (
 	"testing"
 
 	"github.com/stretchr/testify/assert"
+	"github.com/zeromicro/go-zero/core/proc"
 )
 
 var (
@@ -55,6 +56,7 @@ func TestServiceGroup(t *testing.T) {
 	}
 
 	group.Stop()
+	proc.Shutdown()
 
 	mutex.Lock()
 	defer mutex.Unlock()
diff --git a/core/stores/cache/cleaner_test.go b/core/stores/cache/cleaner_test.go
index b4e69a60..73f6b3f5 100644
--- a/core/stores/cache/cleaner_test.go
+++ b/core/stores/cache/cleaner_test.go
@@ -5,6 +5,7 @@ import (
 	"time"
 
 	"github.com/stretchr/testify/assert"
+	"github.com/zeromicro/go-zero/core/proc"
 )
 
 func TestNextDelay(t *testing.T) {
@@ -51,6 +52,7 @@ func TestNextDelay(t *testing.T) {
 			next, ok := nextDelay(test.input)
 			assert.Equal(t, test.ok, ok)
 			assert.Equal(t, test.output, next)
+			proc.Shutdown()
 		})
 	}
 }
diff --git a/rest/internal/starter_test.go b/rest/internal/starter_test.go
index 7e672d16..a54c215f 100644
--- a/rest/internal/starter_test.go
+++ b/rest/internal/starter_test.go
@@ -8,6 +8,7 @@ import (
 	"testing"
 
 	"github.com/stretchr/testify/assert"
+	"github.com/zeromicro/go-zero/core/proc"
 )
 
 func TestStartHttp(t *testing.T) {
@@ -19,6 +20,7 @@ func TestStartHttp(t *testing.T) {
 		svr.IdleTimeout = 0
 	})
 	assert.NotNil(t, err)
+	proc.WrapUp()
 }
 
 func TestStartHttps(t *testing.T) {
@@ -30,4 +32,5 @@ func TestStartHttps(t *testing.T) {
 		svr.IdleTimeout = 0
 	})
 	assert.NotNil(t, err)
+	proc.WrapUp()
 }
diff --git a/zrpc/internal/rpcserver_test.go b/zrpc/internal/rpcserver_test.go
index 6de18073..ef088a99 100644
--- a/zrpc/internal/rpcserver_test.go
+++ b/zrpc/internal/rpcserver_test.go
@@ -5,6 +5,7 @@ import (
 	"testing"
 
 	"github.com/stretchr/testify/assert"
+	"github.com/zeromicro/go-zero/core/proc"
 	"github.com/zeromicro/go-zero/core/stat"
 	"github.com/zeromicro/go-zero/zrpc/internal/mock"
 	"google.golang.org/grpc"
@@ -36,6 +37,8 @@ func TestRpcServer(t *testing.T) {
 	}()
 
 	wg.Wait()
+
+	proc.WrapUp()
 	lock.Lock()
 	grpcServer.GracefulStop()
 	lock.Unlock()