我正在编写一个调用其他微服务的微服务,以获取很少更新的数据(一天一次或每月一次)。因此,我决定创建缓存,并实现了此接口:
type StringCache interface {
Get(string) (string, bool)
Put(string, string)
}
内部为map[string]cacheItem
,其中
type cacheItem struct {
data string
expire_at time.Time
}
我的同事说这是不安全的,我需要在我的方法中添加互斥锁,因为它会被HTTP处理程序函数的不同实例并行使用。我对此进行了测试,但是它没有检测到数据争用,因为它在一个goroutine中使用了缓存:
func TestStringCache(t *testing.T) {
testDuration := time.Millisecond * 10
cache := NewStringCache(testDuration / 2)
cache.Put("here", "this")
// Value put in cache should be in cache
res, ok := cache.Get("here")
assert.Equal(t, res, "this")
assert.True(t, ok)
// Values put in cache will eventually expire
time.Sleep(testDuration)
res, ok = cache.Get("here")
assert.Equal(t, res, "")
assert.False(t, ok)
}
所以,我的问题是:当使用go test -race
运行时,如何重写该测试以检测到数据竞争(如果存在)?
首先,Go中的数据竞争检测器不是某种形式的证明者,它使用静态代码分析,而是一种动态工具,它以特殊方式对compiled代码进行检测,以尝试检测数据竞争。运行。这意味着如果种族探测器很幸运并且发现了数据争用,则应确保在报告的位置存在数据争用。但这也意味着,如果实际的程序流程没有使某些现有的数据争用条件达到happen,,则竞争检测器将不会发现并报告它。换句话说,竞速检测器没有误报,而仅仅是尽力而为的工具。因此,为了编写不受种族限制的代码,您实际上必须重新考虑您的方法。最好从Go竞赛检测器作者编写的this classic essay on the topic开始,一旦您了解到没有良性的数据竞赛,您基本上就只是训练自己思考一下,每次运行并发执行访问数据的过程正在设计数据和用于处理数据的算法。
例如,您知道(至少您
应该
知道您是否已阅读文档),使用net/http
实现的HTTP服务器的每个传入请求均由单独的goroutine处理。这意味着,如果您有一个中央(共享)数据结构,例如要由处理客户端请求的代码访问的缓存,则您[[do可能有多个goroutines可能同时访问该共享数据。现在,如果您有另一个goroutine对数据进行[[updates,那么您确实有可能进行经典的数据竞争:当一个goroutine正在更新数据时,另一个goroutine可能会读取它。关于眼前的问题,两件事:首先,从不]]使用计时器来测试内容。这不起作用。
第二,对于像您这样的简单案例,仅使用两个goroutines就足够了:
package main
import (
"testing"
"time"
)
type cacheItem struct {
data string
expire_at time.Time
}
type stringCache struct {
m map[string]cacheItem
exp time.Duration
}
func (sc *stringCache) Get(key string) (string, bool) {
if item, ok := sc.m[key]; !ok {
return "", false
} else {
return item.data, true
}
}
func (sc *stringCache) Put(key, data string) {
sc.m[key] = cacheItem{
data: data,
expire_at: time.Now().Add(sc.exp),
}
}
func NewStringCache(d time.Duration) *stringCache {
return &stringCache{
m: make(map[string]cacheItem),
exp: d,
}
}
func TestStringCache(t *testing.T) {
cache := NewStringCache(time.Minute)
ch := make(chan struct{})
go func() {
cache.Put("here", "this")
close(ch)
}()
_, _ = cache.Get("here")
<-ch
}
sc_test.go
,然后tmp$ go test -race -c -o sc_test ./sc_test.go
tmp$ ./sc_test
==================
WARNING: DATA RACE
Write at 0x00c00009e270 by goroutine 8:
runtime.mapassign_faststr()
/home/kostix/devel/golang-1.13.6/src/runtime/map_faststr.go:202 +0x0
command-line-arguments.(*stringCache).Put()
/home/kostix/tmp/sc_test.go:27 +0x144
command-line-arguments.TestStringCache.func1()
/home/kostix/tmp/sc_test.go:46 +0x62
Previous read at 0x00c00009e270 by goroutine 7:
runtime.mapaccess2_faststr()
/home/kostix/devel/golang-1.13.6/src/runtime/map_faststr.go:107 +0x0
command-line-arguments.TestStringCache()
/home/kostix/tmp/sc_test.go:19 +0x125
testing.tRunner()
/home/kostix/devel/golang-1.13.6/src/testing/testing.go:909 +0x199
Goroutine 8 (running) created at:
command-line-arguments.TestStringCache()
/home/kostix/tmp/sc_test.go:45 +0xe4
testing.tRunner()
/home/kostix/devel/golang-1.13.6/src/testing/testing.go:909 +0x199
Goroutine 7 (running) created at:
testing.(*T).Run()
/home/kostix/devel/golang-1.13.6/src/testing/testing.go:960 +0x651
testing.runTests.func1()
/home/kostix/devel/golang-1.13.6/src/testing/testing.go:1202 +0xa6
testing.tRunner()
/home/kostix/devel/golang-1.13.6/src/testing/testing.go:909 +0x199
testing.runTests()
/home/kostix/devel/golang-1.13.6/src/testing/testing.go:1200 +0x521
testing.(*M).Run()
/home/kostix/devel/golang-1.13.6/src/testing/testing.go:1117 +0x2ff
main.main()
_testmain.go:44 +0x223
==================
--- FAIL: TestStringCache (0.00s)
testing.go:853: race detected during execution of test
FAIL