Skip to content

Commit

Permalink
chore: add more tests (zeromicro#2815)
Browse files Browse the repository at this point in the history
* chore: add more tests

* chore: add more tests

* chore: add more tests

* chore: add more tests

* chore: add more tests

* chore: add more tests
  • Loading branch information
kevwan authored and byops committed Jan 31, 2023
1 parent 72b3115 commit e613c9b
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 3 deletions.
3 changes: 2 additions & 1 deletion core/executors/chunkexecutor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions core/executors/periodicalexecutor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions core/metric/counter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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)
}
Expand Down
3 changes: 3 additions & 0 deletions core/metric/gauge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions core/metric/histogram_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 changes: 10 additions & 0 deletions core/proc/shutdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions core/proc/shutdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 13 additions & 0 deletions core/service/serviceconf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package service
import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/logx"
)

Expand All @@ -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 changes: 2 additions & 0 deletions core/service/servicegroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/proc"
)

var (
Expand Down Expand Up @@ -55,6 +56,7 @@ func TestServiceGroup(t *testing.T) {
}

group.Stop()
proc.Shutdown()

mutex.Lock()
defer mutex.Unlock()
Expand Down
2 changes: 2 additions & 0 deletions core/stores/cache/cleaner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

"github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/proc"
)

func TestNextDelay(t *testing.T) {
Expand Down Expand Up @@ -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 changes: 3 additions & 0 deletions rest/internal/starter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/proc"
)

func TestStartHttp(t *testing.T) {
Expand All @@ -19,6 +20,7 @@ func TestStartHttp(t *testing.T) {
svr.IdleTimeout = 0
})
assert.NotNil(t, err)
proc.WrapUp()
}

func TestStartHttps(t *testing.T) {
Expand All @@ -30,4 +32,5 @@ func TestStartHttps(t *testing.T) {
svr.IdleTimeout = 0
})
assert.NotNil(t, err)
proc.WrapUp()
}
3 changes: 3 additions & 0 deletions zrpc/internal/rpcserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -36,6 +37,8 @@ func TestRpcServer(t *testing.T) {
}()

wg.Wait()

proc.WrapUp()
lock.Lock()
grpcServer.GracefulStop()
lock.Unlock()
Expand Down

0 comments on commit e613c9b

Please sign in to comment.