From fe3739b7f3bf913ad7cb6b86ab709c82b189962d Mon Sep 17 00:00:00 2001 From: kevin Date: Fri, 16 Oct 2020 11:13:55 +0800 Subject: [PATCH] fix golint issues --- core/breaker/googlebreaker_test.go | 3 +-- core/collection/cache.go | 21 +++++++++------------ core/contextx/valueonlycontext_test.go | 6 ++++-- core/executors/bulkexecutor_test.go | 4 +--- core/limit/tokenlimit.go | 11 ++++------- core/logx/logs_test.go | 6 ++++-- core/logx/syslog_test.go | 2 +- core/stores/cache/cachestat.go | 25 +++++++++++-------------- core/trace/propagator_test.go | 12 ++++++------ 9 files changed, 41 insertions(+), 49 deletions(-) diff --git a/core/breaker/googlebreaker_test.go b/core/breaker/googlebreaker_test.go index 242fed87..a584f931 100644 --- a/core/breaker/googlebreaker_test.go +++ b/core/breaker/googlebreaker_test.go @@ -2,7 +2,6 @@ package breaker import ( "errors" - "math" "math/rand" "testing" "time" @@ -157,7 +156,7 @@ func TestGoogleBreakerSelfProtection(t *testing.T) { t.Run("total request > 100, total < 2 * success", func(t *testing.T) { b := getGoogleBreaker() size := rand.Intn(10000) - accepts := int(math.Ceil(float64(size))) + 1 + accepts := size + 1 markSuccess(b, accepts) markFailed(b, size-accepts) assert.Nil(t, b.accept()) diff --git a/core/collection/cache.go b/core/collection/cache.go index e95a844e..368e79a3 100644 --- a/core/collection/cache.go +++ b/core/collection/cache.go @@ -277,18 +277,15 @@ func (cs *cacheStat) statLoop() { ticker := time.NewTicker(statInterval) defer ticker.Stop() - for { - select { - case <-ticker.C: - hit := atomic.SwapUint64(&cs.hit, 0) - miss := atomic.SwapUint64(&cs.miss, 0) - total := hit + miss - if total == 0 { - continue - } - percent := 100 * float32(hit) / float32(total) - logx.Statf("cache(%s) - qpm: %d, hit_ratio: %.1f%%, elements: %d, hit: %d, miss: %d", - cs.name, total, percent, cs.sizeCallback(), hit, miss) + for range ticker.C { + hit := atomic.SwapUint64(&cs.hit, 0) + miss := atomic.SwapUint64(&cs.miss, 0) + total := hit + miss + if total == 0 { + continue } + percent := 100 * float32(hit) / float32(total) + logx.Statf("cache(%s) - qpm: %d, hit_ratio: %.1f%%, elements: %d, hit: %d, miss: %d", + cs.name, total, percent, cs.sizeCallback(), hit, miss) } } diff --git a/core/contextx/valueonlycontext_test.go b/core/contextx/valueonlycontext_test.go index a2fecc05..d3f1ddad 100644 --- a/core/contextx/valueonlycontext_test.go +++ b/core/contextx/valueonlycontext_test.go @@ -45,9 +45,11 @@ func TestContextDeadline(t *testing.T) { t.Fatal("ValueOnlyContext: context should not have timed out") } - c, _ = context.WithDeadline(context.Background(), time.Now().Add(10*time.Millisecond)) + c, cancel = context.WithDeadline(context.Background(), time.Now().Add(10*time.Millisecond)) + cancel() o = ValueOnlyFrom(c) - c, _ = context.WithDeadline(o, time.Now().Add(20*time.Millisecond)) + c, cancel = context.WithDeadline(o, time.Now().Add(20*time.Millisecond)) + defer cancel() select { case <-time.After(100 * time.Millisecond): t.Fatal("ValueOnlyContext+Deadline: context should have timed out") diff --git a/core/executors/bulkexecutor_test.go b/core/executors/bulkexecutor_test.go index bc43b23c..f8a33d9a 100644 --- a/core/executors/bulkexecutor_test.go +++ b/core/executors/bulkexecutor_test.go @@ -86,9 +86,7 @@ func TestBuldExecutorFlushSlowTasks(t *testing.T) { time.Sleep(time.Millisecond * 100) lock.Lock() defer lock.Unlock() - for _, i := range tasks { - result = append(result, i) - } + result = append(result, tasks...) }, WithBulkTasks(1000)) for i := 0; i < total; i++ { assert.Nil(t, exec.Add(i)) diff --git a/core/limit/tokenlimit.go b/core/limit/tokenlimit.go index 59dc23d4..0ec92f12 100644 --- a/core/limit/tokenlimit.go +++ b/core/limit/tokenlimit.go @@ -153,13 +153,10 @@ func (lim *TokenLimiter) waitForRedis() { lim.rescueLock.Unlock() }() - for { - select { - case <-ticker.C: - if lim.store.Ping() { - atomic.StoreUint32(&lim.redisAlive, 1) - return - } + for range ticker.C { + if lim.store.Ping() { + atomic.StoreUint32(&lim.redisAlive, 1) + return } } } diff --git a/core/logx/logs_test.go b/core/logx/logs_test.go index f5a37e23..e1330673 100644 --- a/core/logx/logs_test.go +++ b/core/logx/logs_test.go @@ -229,8 +229,10 @@ func TestSetup(t *testing.T) { func TestDisable(t *testing.T) { Disable() - WithKeepDays(1) - WithGzip() + + var opt logOptions + WithKeepDays(1)(&opt) + WithGzip()(&opt) assert.Nil(t, Close()) writeConsole = false assert.Nil(t, Close()) diff --git a/core/logx/syslog_test.go b/core/logx/syslog_test.go index 78903548..2b934a97 100644 --- a/core/logx/syslog_test.go +++ b/core/logx/syslog_test.go @@ -15,7 +15,7 @@ const testlog = "Stay hungry, stay foolish." func TestCollectSysLog(t *testing.T) { CollectSysLog() content := getContent(captureOutput(func() { - log.Printf(testlog) + log.Print(testlog) })) assert.True(t, strings.Contains(content, testlog)) } diff --git a/core/stores/cache/cachestat.go b/core/stores/cache/cachestat.go index a089f8fe..88de7e56 100644 --- a/core/stores/cache/cachestat.go +++ b/core/stores/cache/cachestat.go @@ -48,20 +48,17 @@ func (cs *CacheStat) statLoop() { ticker := time.NewTicker(statInterval) defer ticker.Stop() - for { - select { - case <-ticker.C: - total := atomic.SwapUint64(&cs.Total, 0) - if total == 0 { - continue - } - - hit := atomic.SwapUint64(&cs.Hit, 0) - percent := 100 * float32(hit) / float32(total) - miss := atomic.SwapUint64(&cs.Miss, 0) - dbf := atomic.SwapUint64(&cs.DbFails, 0) - logx.Statf("dbcache(%s) - qpm: %d, hit_ratio: %.1f%%, hit: %d, miss: %d, db_fails: %d", - cs.name, total, percent, hit, miss, dbf) + for range ticker.C { + total := atomic.SwapUint64(&cs.Total, 0) + if total == 0 { + continue } + + hit := atomic.SwapUint64(&cs.Hit, 0) + percent := 100 * float32(hit) / float32(total) + miss := atomic.SwapUint64(&cs.Miss, 0) + dbf := atomic.SwapUint64(&cs.DbFails, 0) + logx.Statf("dbcache(%s) - qpm: %d, hit_ratio: %.1f%%, hit: %d, miss: %d, db_fails: %d", + cs.name, total, percent, hit, miss, dbf) } } diff --git a/core/trace/propagator_test.go b/core/trace/propagator_test.go index a814e1b4..92c140c5 100644 --- a/core/trace/propagator_test.go +++ b/core/trace/propagator_test.go @@ -18,7 +18,7 @@ func TestHttpPropagator_Extract(t *testing.T) { assert.Equal(t, "trace", carrier.Get(traceIdKey)) assert.Equal(t, "span", carrier.Get(spanIdKey)) - carrier, err = Extract(HttpFormat, req) + _, err = Extract(HttpFormat, req) assert.Equal(t, ErrInvalidCarrier, err) } @@ -31,7 +31,7 @@ func TestHttpPropagator_Inject(t *testing.T) { assert.Equal(t, "trace", carrier.Get(traceIdKey)) assert.Equal(t, "span", carrier.Get(spanIdKey)) - carrier, err = Inject(HttpFormat, req) + _, err = Inject(HttpFormat, req) assert.Equal(t, ErrInvalidCarrier, err) } @@ -45,9 +45,9 @@ func TestGrpcPropagator_Extract(t *testing.T) { assert.Equal(t, "trace", carrier.Get(traceIdKey)) assert.Equal(t, "span", carrier.Get(spanIdKey)) - carrier, err = Extract(GrpcFormat, 1) + _, err = Extract(GrpcFormat, 1) assert.Equal(t, ErrInvalidCarrier, err) - carrier, err = Extract(nil, 1) + _, err = Extract(nil, 1) assert.Equal(t, ErrInvalidCarrier, err) } @@ -61,8 +61,8 @@ func TestGrpcPropagator_Inject(t *testing.T) { assert.Equal(t, "trace", carrier.Get(traceIdKey)) assert.Equal(t, "span", carrier.Get(spanIdKey)) - carrier, err = Inject(GrpcFormat, 1) + _, err = Inject(GrpcFormat, 1) assert.Equal(t, ErrInvalidCarrier, err) - carrier, err = Inject(nil, 1) + _, err = Inject(nil, 1) assert.Equal(t, ErrInvalidCarrier, err) }