unit-testing - golang 字符串 channel 发送/接收不一致

标签 unit-testing testing go channel

新的去。我正在使用 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)

发送后truecloseCh 上.那样wdCh直到您在 closeCh 上发出信号后才能返回零值.此外,closeCh <- true block 因为 closeCh没有缓冲区大小,所以 wdCh在你保证 Accumulate 之前不会关闭已经完成了永远的循环。

关于unit-testing - golang 字符串 channel 发送/接收不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32928843/

相关文章:

unit-testing - 带有 nock 和 redux-mock-store 错误的 Redux Action 测试

java - 为什么 Mockito @Mock 创建一个非模拟实例?

.net - specflow: - "Ambiguous step definitions found for step"当 1 步的参数多于另一步时

django - 使用 Django JWT token 进行 Ansible uri 调用

json - 如何根据 swagger 架构验证 JSON 架构实例?

java - DexMaker Mockito 是模拟 stub 、 spy 、部分还是什么?

ruby-on-rails - 为什么我应该在 Rails 中使用 RSpec 或 shoulda?

javascript - 如何从 Protractor 中的 promise 中获取嵌套的 JSON

go - 如何在go中删除结构对象?

mongodb - 使用 golang 从 mongo 获取一段 json 字符串