소스 검색

chore: add more tests (#2815)

* chore: add more tests

* chore: add more tests

* chore: add more tests

* chore: add more tests

* chore: add more tests

* chore: add more tests
Kevin Wan 2 년 전
부모
커밋
696da4efee

+ 2 - 1
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) {

+ 2 - 0
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) {

+ 4 - 0
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)
 }

+ 3 - 0
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) {

+ 3 - 0
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()
 }

+ 10 - 0
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)
 

+ 2 - 2
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)
 }

+ 13 - 0
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())
+}

+ 2 - 0
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()

+ 2 - 0
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()
 		})
 	}
 }

+ 3 - 0
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()
 }

+ 3 - 0
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()