Skip to content

Commit

Permalink
add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Neng Wan committed Oct 25, 2021
1 parent 782d49d commit b240aea
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 8 deletions.
8 changes: 4 additions & 4 deletions extensions/cachext/cached.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ func (c *CachedConfig) GetResult(ctx context.Context, out interface{}, strArgs [
c.cache.requestCounter.With(labels).Inc()
}
if data != nil {
if c.cacheNil && decodeIsNil(data) {
// 无法直接把out设置为nil,这里通过返回特殊的错误来表示nil。调用方需要判断
return Nil
}
if c.cache.hitCounter != nil {
// Increment hit counter.
c.cache.hitCounter.With(labels).Inc()
}
if c.cacheNil && decodeIsNil(data) {
// 无法直接把out设置为nil,这里通过返回特殊的错误来表示nil。调用方需要判断
return Nil
}
return decode(data, out)
}
res, err := c.getResult(ctx, strArgs, intArgs)
Expand Down
8 changes: 4 additions & 4 deletions extensions/cachext/ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ func (c *CacheExt) Init(app *gobay.Application) error {
return errors.New("No backend found for cache_backend:" + backendConfig)
}
if config.GetBool("monitor_enable") {
c.requestCounter = initCacheRequestCounter()
c.hitCounter = initCacheHitCounter()
c.requestCounter = newCacheRequestCounter()
c.hitCounter = newCacheHitCounter()
}

c.initialized = true
Expand Down Expand Up @@ -257,7 +257,7 @@ func decodeIsNil(data interface{}) bool {
}

// Create a collector for total cache request counter
func initCacheRequestCounter() *prometheus.CounterVec {
func newCacheRequestCounter() *prometheus.CounterVec {
return promauto.NewCounterVec(
prometheus.CounterOpts{
Name: "cache_request_counter",
Expand All @@ -268,7 +268,7 @@ func initCacheRequestCounter() *prometheus.CounterVec {
}

// Create a collector for cache hit counter
func initCacheHitCounter() *prometheus.CounterVec {
func newCacheHitCounter() *prometheus.CounterVec {
return promauto.NewCounterVec(
prometheus.CounterOpts{
Name: "cache_hit_counter",
Expand Down
55 changes: 55 additions & 0 deletions extensions/cachext/ext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@ package cachext_test
import (
"context"
"fmt"
"io/ioutil"
"log"
"strings"
"testing"
"time"

"net/http"

"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/shanbay/gobay"
"github.com/shanbay/gobay/extensions/cachext"
_ "github.com/shanbay/gobay/extensions/cachext/backend/memory"
"github.com/stretchr/testify/assert"
)

func ExampleCacheExt_Set() {
Expand Down Expand Up @@ -577,3 +583,52 @@ func Benchmark_Cached(b *testing.B) {
}
}
}

func TestCacheExt_Cached_Monitor(t *testing.T) {
// 准备数据
cache := &cachext.CacheExt{NS: "cache_"}
exts := map[gobay.Key]gobay.Extension{
"cache": cache,
}
_, err := gobay.CreateApp("../../testdata/", "cachemonitored", exts)
assert.Nil(t, err)

fetchMetricData := func() string {
resp, err := http.Get("http://localhost:2112/metrics")
if err != nil {
t.Error(err)
}
defer resp.Body.Close()
rawData, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Error(err)
}
return string(rawData)
}

go func() {
http.Handle("/metrics", promhttp.Handler())
if err := http.ListenAndServe(":2112", nil); err != nil {
log.Fatalf("error when start prometheus server: %v\n", err)
}
}()

// Cache method
f_str := func(_ context.Context, keys []string, args []int64) (interface{}, error) {
return keys[0], nil
}
c_f_str := cache.Cached("f_str", f_str, cachext.WithTTL(10*time.Second))
str := ""

// Get result from function
c_f_str.GetResult(context.Background(), &str, []string{"hello"}, []int64{})
data := fetchMetricData()
assert.Contains(t, data, `cache_request_counter{func_name="f_str",prefix_name="github"} 1`)
assert.NotContains(t, data, `cache_hit_counter`)

// Get result from cache
c_f_str.GetResult(context.Background(), &str, []string{"hello"}, []int64{})
data = fetchMetricData()
assert.Contains(t, data, `cache_request_counter{func_name="f_str",prefix_name="github"} 2`)
assert.Contains(t, data, `cache_hit_counter{func_name="f_str",prefix_name="github"} 1`)
}
3 changes: 3 additions & 0 deletions testdata/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ grpcnoretry:
grpcmocked:
<<: *defaults
stub_health_mocked: true
cachemonitored:
<<: *defaults
cache_monitor_enable: true
development:
<<: *defaults
production:
Expand Down

0 comments on commit b240aea

Please sign in to comment.