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
This commit is contained in:
Kevin Wan
2023-01-24 13:43:13 +08:00
committed by GitHub
parent ceab564429
commit 696da4efee
12 changed files with 49 additions and 3 deletions

View File

@@ -53,10 +53,11 @@ func TestChunkExecutorFlushInterval(t *testing.T) {
} }
func TestChunkExecutorEmpty(t *testing.T) { func TestChunkExecutorEmpty(t *testing.T) {
NewChunkExecutor(func(items []interface{}) { executor := NewChunkExecutor(func(items []interface{}) {
assert.Fail(t, "should not called") assert.Fail(t, "should not called")
}, WithChunkBytes(10), WithFlushInterval(time.Millisecond)) }, WithChunkBytes(10), WithFlushInterval(time.Millisecond))
time.Sleep(time.Millisecond * 100) time.Sleep(time.Millisecond * 100)
executor.Wait()
} }
func TestChunkExecutorFlush(t *testing.T) { func TestChunkExecutorFlush(t *testing.T) {

View File

@@ -8,6 +8,7 @@ import (
"time" "time"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/proc"
"github.com/zeromicro/go-zero/core/timex" "github.com/zeromicro/go-zero/core/timex"
) )
@@ -67,6 +68,7 @@ func TestPeriodicalExecutor_QuitGoroutine(t *testing.T) {
ticker.Tick() ticker.Tick()
ticker.Wait(time.Millisecond * idleRound) ticker.Wait(time.Millisecond * idleRound)
assert.Equal(t, routines, runtime.NumGoroutine()) assert.Equal(t, routines, runtime.NumGoroutine())
proc.Shutdown()
} }
func TestPeriodicalExecutor_Bulk(t *testing.T) { func TestPeriodicalExecutor_Bulk(t *testing.T) {

View File

@@ -5,6 +5,7 @@ import (
"github.com/prometheus/client_golang/prometheus/testutil" "github.com/prometheus/client_golang/prometheus/testutil"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/proc"
"github.com/zeromicro/go-zero/core/prometheus" "github.com/zeromicro/go-zero/core/prometheus"
) )
@@ -17,6 +18,9 @@ func TestNewCounterVec(t *testing.T) {
}) })
defer counterVec.close() defer counterVec.close()
counterVecNil := NewCounterVec(nil) counterVecNil := NewCounterVec(nil)
counterVec.Inc("path", "code")
counterVec.Add(1, "path", "code")
proc.Shutdown()
assert.NotNil(t, counterVec) assert.NotNil(t, counterVec)
assert.Nil(t, counterVecNil) assert.Nil(t, counterVecNil)
} }

View File

@@ -5,6 +5,7 @@ import (
"github.com/prometheus/client_golang/prometheus/testutil" "github.com/prometheus/client_golang/prometheus/testutil"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/proc"
) )
func TestNewGaugeVec(t *testing.T) { func TestNewGaugeVec(t *testing.T) {
@@ -18,6 +19,8 @@ func TestNewGaugeVec(t *testing.T) {
gaugeVecNil := NewGaugeVec(nil) gaugeVecNil := NewGaugeVec(nil)
assert.NotNil(t, gaugeVec) assert.NotNil(t, gaugeVec)
assert.Nil(t, gaugeVecNil) assert.Nil(t, gaugeVecNil)
proc.Shutdown()
} }
func TestGaugeInc(t *testing.T) { func TestGaugeInc(t *testing.T) {

View File

@@ -6,6 +6,7 @@ import (
"github.com/prometheus/client_golang/prometheus/testutil" "github.com/prometheus/client_golang/prometheus/testutil"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/proc"
) )
func TestNewHistogramVec(t *testing.T) { func TestNewHistogramVec(t *testing.T) {
@@ -47,4 +48,6 @@ func TestHistogramObserve(t *testing.T) {
err := testutil.CollectAndCompare(hv.histogram, strings.NewReader(metadata+val)) err := testutil.CollectAndCompare(hv.histogram, strings.NewReader(metadata+val))
assert.Nil(t, err) assert.Nil(t, err)
proc.Shutdown()
} }

View File

@@ -43,6 +43,16 @@ func SetTimeToForceQuit(duration time.Duration) {
delayTimeBeforeForceQuit = 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) { func gracefulStop(signals chan os.Signal) {
signal.Stop(signals) signal.Stop(signals)

View File

@@ -18,14 +18,14 @@ func TestShutdown(t *testing.T) {
called := AddWrapUpListener(func() { called := AddWrapUpListener(func() {
val++ val++
}) })
wrapUpListeners.notifyListeners() WrapUp()
called() called()
assert.Equal(t, 1, val) assert.Equal(t, 1, val)
called = AddShutdownListener(func() { called = AddShutdownListener(func() {
val += 2 val += 2
}) })
shutdownListeners.notifyListeners() Shutdown()
called() called()
assert.Equal(t, 3, val) assert.Equal(t, 3, val)
} }

View File

@@ -3,6 +3,7 @@ package service
import ( import (
"testing" "testing"
"github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/logx"
) )
@@ -16,3 +17,15 @@ func TestServiceConf(t *testing.T) {
} }
c.MustSetUp() 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())
}

View File

@@ -5,6 +5,7 @@ import (
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/proc"
) )
var ( var (
@@ -55,6 +56,7 @@ func TestServiceGroup(t *testing.T) {
} }
group.Stop() group.Stop()
proc.Shutdown()
mutex.Lock() mutex.Lock()
defer mutex.Unlock() defer mutex.Unlock()

View File

@@ -5,6 +5,7 @@ import (
"time" "time"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/proc"
) )
func TestNextDelay(t *testing.T) { func TestNextDelay(t *testing.T) {
@@ -51,6 +52,7 @@ func TestNextDelay(t *testing.T) {
next, ok := nextDelay(test.input) next, ok := nextDelay(test.input)
assert.Equal(t, test.ok, ok) assert.Equal(t, test.ok, ok)
assert.Equal(t, test.output, next) assert.Equal(t, test.output, next)
proc.Shutdown()
}) })
} }
} }

View File

@@ -8,6 +8,7 @@ import (
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/proc"
) )
func TestStartHttp(t *testing.T) { func TestStartHttp(t *testing.T) {
@@ -19,6 +20,7 @@ func TestStartHttp(t *testing.T) {
svr.IdleTimeout = 0 svr.IdleTimeout = 0
}) })
assert.NotNil(t, err) assert.NotNil(t, err)
proc.WrapUp()
} }
func TestStartHttps(t *testing.T) { func TestStartHttps(t *testing.T) {
@@ -30,4 +32,5 @@ func TestStartHttps(t *testing.T) {
svr.IdleTimeout = 0 svr.IdleTimeout = 0
}) })
assert.NotNil(t, err) assert.NotNil(t, err)
proc.WrapUp()
} }

View File

@@ -5,6 +5,7 @@ import (
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/proc"
"github.com/zeromicro/go-zero/core/stat" "github.com/zeromicro/go-zero/core/stat"
"github.com/zeromicro/go-zero/zrpc/internal/mock" "github.com/zeromicro/go-zero/zrpc/internal/mock"
"google.golang.org/grpc" "google.golang.org/grpc"
@@ -36,6 +37,8 @@ func TestRpcServer(t *testing.T) {
}() }()
wg.Wait() wg.Wait()
proc.WrapUp()
lock.Lock() lock.Lock()
grpcServer.GracefulStop() grpcServer.GracefulStop()
lock.Unlock() lock.Unlock()