栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何测试不太可能的并发场景?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何测试不太可能的并发场景?

I.模拟
pool.cacheLock.Lock()

覆盖该分支的一种方法是模拟

pool.cacheLock.Lock()
,模拟版本可以将插入
url
到地图中。因此,在此调用之后再次检查,将找到并执行将进入第二条
if
语句的主体。

使用界面模拟

模拟的一种方法

pool.cacheLock.Lock()
是创建
pool.cacheLock
一个接口,在测试中,您可以设置一个模拟值,该
Lock()
方法的方法将“脏插入”到映射中。

这是使用以下接口的代码的简化版本

pool.cacheLock

type rwmutex interface {    Lock()    RLock()    RUnlock()    Unlock()}type fPool struct {    cache     map[string]string    cacheLock rwmutex}func (pool *fPool) fetch(url string) string {    pool.cacheLock.RLock()    if rp, pres := pool.cache[url]; pres {        pool.cacheLock.RUnlock()        return rp    }    pool.cacheLock.RUnlock()    pool.cacheLock.Lock()    if rp, pres := pool.cache[url]; pres {        pool.cacheLock.Unlock()        // Skip adding url if someone snuck it in between RUnlock an Lock        return rp    }    rp := url + "~data"    pool.cache[url] = rp    pool.cacheLock.Unlock()    return rp}

它的正常用法是:

pool := fPool{    cache:     map[string]string{},    cacheLock: &sync.RWMutex{},}fmt.Println(pool.fetch("http://google.com"))

还有一个测试用例将触发第二个主体

if

type testRwmutex struct {    sync.RWMutex // Embed RWMutex so we don't have to implement everything    customLock   func()}func (trw *testRwmutex) Lock() {    trw.RWMutex.Lock()    if trw.customLock != nil {        trw.customLock()    }}func TestFPoolFetch(t *testing.T) {    trw := &testRwmutex{RWMutex: sync.RWMutex{}}    pool := &fPool{        cache:     map[string]string{},        cacheLock: trw,    }    exp := "http://google.com~test"    trw.customLock = func() {        pool.cache["http://google.com"] = exp    }    if got := pool.fetch("http://google.com"); got != exp {        t.Errorf("Expected: %s, got: %s", exp, got)    }}

通过使用功能字段进行模拟

模拟的另一种方法

pool.cacheLock.Lock()
是将该功能“外包”给一个函数类型的字段,该测试可以替换为一个函数,该函数除了调用此函数外,还进行“脏插入”。

再次简化示例:

func NewFPool() *fPool {    mu := &sync.RWMutex{}    return &fPool{        cache:     map[string]string{},        cacheLock: mu,        lock:      mu.Lock,    }}type fPool struct {    cache     map[string]string    cacheLock *sync.RWMutex    lock      func()}func (pool *fPool) fetch(url string) string {    pool.cacheLock.RLock()    if rp, pres := pool.cache[url]; pres {        pool.cacheLock.RUnlock()        return rp    }    pool.cacheLock.RUnlock()    pool.lock()    if rp, pres := pool.cache[url]; pres {        pool.cacheLock.Unlock()        // Skip adding url if someone snuck it in between RUnlock an Lock        return rp    }    rp := url + "~data"    pool.cache[url] = rp    pool.cacheLock.Unlock()    return rp}

正常用法是:

pool := NewFPool()fmt.Println(pool.fetch("http://google.com"))

还有一个测试用例将触发第二个主体

if

func TestFPoolFetch(t *testing.T) {    pool := NewFPool()    oldLock := pool.lock    exp := "http://google.com~test"    pool.lock = func() {        oldLock()        pool.cache["http://google.com"] = exp    }    if got := pool.fetch("http://google.com"); got != exp {        t.Errorf("Expected: %s, got: %s", exp, got)    }}

二。使用一个简单的
test
标志

这里的想法是,为了支持简单测试,您可以

test
在实现中构建一个简单标志
fPool
(例如可以是的字段
fPool
),并且您要测试的代码会故意检查该标志:

type fPool struct {    cache     map[string]string    cacheLock *sync.RWMutex    test      bool}func (pool *fPool) fetch(url string) string {    pool.cacheLock.RLock()    if rp, pres := pool.cache[url]; pres {        pool.cacheLock.RUnlock()        return rp    }    pool.cacheLock.RUnlock()    pool.cacheLock.Lock()    if rp, pres := pool.cache[url]; pres || pool.test {        pool.cacheLock.Unlock()        // Skip adding url if someone snuck it in between RUnlock an Lock        return rp    }    rp := url + "~data"    pool.cache[url] = rp    pool.cacheLock.Unlock()    return rp}

现在,如果您想测试2nd的主体

if
,您要做的就是:

func TestFPoolFetch(t *testing.T) {    pool := NewFPool()    pool.test = true    exp := ""    if got := pool.fetch("http://google.com"); got != exp {        t.Errorf("Expected: %s, got: %s", exp, got)    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/403816.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号