From 696da4efee22a6c181486fc216fe8225c29edf67 Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Tue, 24 Jan 2023 13:43:13 +0800 Subject: [PATCH] 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 --- core/executors/chunkexecutor_test.go | 3 ++- core/executors/periodicalexecutor_test.go | 2 ++ core/metric/counter_test.go | 4 ++++ core/metric/gauge_test.go | 3 +++ core/metric/histogram_test.go | 3 +++ core/proc/shutdown.go | 10 ++++++++++ core/proc/shutdown_test.go | 4 ++-- core/service/serviceconf_test.go | 13 +++++++++++++ core/service/servicegroup_test.go | 2 ++ core/stores/cache/cleaner_test.go | 2 ++ rest/internal/starter_test.go | 3 +++ zrpc/internal/rpcserver_test.go | 3 +++ 12 files changed, 49 insertions(+), 3 deletions(-) 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.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) 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()