新的去。我正在使用 1.5.1。我正在尝试根据传入 channel 积累单词列表。但是,我的输入 channel (wdCh) 在测试期间有时会得到空字符串 ("")。我很困惑。在将空字符串的累积计数添加到我的 map 之前,我宁愿不对它进行测试。对我来说感觉像是一个 hack。
package accumulator
import (
"fmt"
"github.com/stretchr/testify/assert"
"testing"
)
var words map[string]int
func Accumulate(wdCh chan string, closeCh chan bool) {
words = make(map[string]int)
for {
select {
case word := <-wdCh:
fmt.Printf("word = %s\n", word)
words[word]++
case <-closeCh:
return
}
}
}
func pushWords(w []string, wdCh chan string) {
for _, value := range w {
fmt.Printf("sending word = %s\n", value)
wdCh <- value
}
close(wdCh)
}
func TestAccumulate(t *testing.T) {
sendWords := []string{"one", "two", "three", "two"}
wMap := make(map[string]int)
wMap["one"] = 1
wMap["two"] = 2
wMap["three"] = 1
wdCh := make(chan string)
closeCh := make(chan bool)
go Accumulate(wdCh, closeCh)
pushWords(sendWords, wdCh)
closeCh <- true
close(closeCh)
assert.Equal(t, wMap, words)
}
最佳答案
查看这篇关于 channel-axioms 的文章.看起来关闭之间有一场比赛 wdCh
并在 closeCh
上发送 true channel 。
因此结果取决于在 pushWords
之间首先安排的内容返回和 Accumulate
.
如果TestAccumulate
首先运行,在 closeCh
上发送 true , 然后当 Accumulate
运行它会选择两个 channel 中的任何一个,因为它们都可以运行,因为 pushWords
关闭wdCh
.
A receive from a closed channel returns the zero value immediately.
直到 closedCh
发出信号,Accumulate
会在 map 中随机放置一个或多个空的“”字。
如果Accumulate
首先运行然后它可能会在单词映射中放置许多空字符串,因为它循环直到 TestAccumulate
运行并最终在 closeCh
上发送信号.
一个简单的解决办法是移动
close(wdCh)
发送后true
在 closeCh
上.那样wdCh
直到您在 closeCh
上发出信号后才能返回零值.此外,closeCh <- true
block 因为 closeCh
没有缓冲区大小,所以 wdCh
在你保证 Accumulate
之前不会关闭已经完成了永远的循环。
关于unit-testing - golang 字符串 channel 发送/接收不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32928843/