optimize code that fixes issue #317 (#338)

This commit is contained in:
Kevin Wan
2021-01-02 19:01:37 +08:00
committed by GitHub
parent 04059bbf5a
commit 10e3b8ac80
2 changed files with 42 additions and 27 deletions

View File

@@ -140,6 +140,26 @@ func TestPeriodicalExecutor_WaitFast(t *testing.T) {
assert.Equal(t, total, cnt)
}
func TestPeriodicalExecutor_Deadlock(t *testing.T) {
executor := NewBulkExecutor(func(tasks []interface{}) {
}, WithBulkTasks(1), WithBulkInterval(time.Millisecond))
for i := 0; i < 1e5; i++ {
executor.Add(1)
}
}
func TestPeriodicalExecutor_hasTasks(t *testing.T) {
ticker := timex.NewFakeTicker()
defer ticker.Stop()
exec := NewPeriodicalExecutor(time.Millisecond, newContainer(time.Millisecond, nil))
exec.newTicker = func(d time.Duration) timex.Ticker {
return ticker
}
assert.False(t, exec.hasTasks(nil))
assert.True(t, exec.hasTasks(1))
}
// go test -benchtime 10s -bench .
func BenchmarkExecutor(b *testing.B) {
b.ReportAllocs()
@@ -149,11 +169,3 @@ func BenchmarkExecutor(b *testing.B) {
executor.Add(1)
}
}
func TestPeriodicalExecutor_Deadlock(t *testing.T) {
executer := NewBulkExecutor(func(tasks []interface{}) {
}, WithBulkTasks(1), WithBulkInterval(time.Millisecond))
for i := 0; i < 1e6; i++ {
executer.Add(1)
}
}