如何在Golang中进行单元测试时测试是否已调用goroutine?

saz*_*ary 1 unit-testing go goroutine

假设我们有一个这样的方法:

func method(intr MyInterface) {
    go intr.exec()
} 
Run Code Online (Sandbox Code Playgroud)

在单元测试中method,我们要断言inter.exec已被调用过一次,只有一次。因此我们可以在测试中使用另一个模拟结构对其进行模拟,这将为我们提供检查其是否已被调用的功能:

type mockInterface struct{
    CallCount int
}

func (m *mockInterface) exec() {
    m.CallCount += 1
}
Run Code Online (Sandbox Code Playgroud)

在单元测试中:

func TestMethod(t *testing.T) {
    var mock mockInterface{}
    method(mock)
    if mock.CallCount != 1 {
        t.Errorf("Expected exec to be called only once but it ran %d times", mock.CallCount)
    }
}
Run Code Online (Sandbox Code Playgroud)

现在的问题是,由于intr.exec是通过go关键字调用的,因此无法确定在测试中到达断言时是否已调用它。

可能的解决方案1:

在参数上添加一个通道intr.exec可能会解决此问题:我们可以在测试中等待从该对象接收任何对象,并且在从该对象接收到对象之后,我们可以继续断言它正在被调用。此通道将在生产(非测试)代码中完全不使用。这可以工作,但是会给非测试代码增加不必要的复杂性,并且可能会使大型代码库变得难以理解。

可能的解决方案2:

在断言之前在测试中添加相对较小的睡眠可以使我们确信在睡眠完成之前将调用goroutine:

func TestMethod(t *testing.T) {
    var mock mockInterface{}
    method(mock)

    time.sleep(100 * time.Millisecond)

    if mock.CallCount != 1 {
        t.Errorf("Expected exec to be called only once but it ran %d times", mock.CallCount)
    }
}
Run Code Online (Sandbox Code Playgroud)

这将保留当前的非测试代码。
问题在于,这会使测试速度变慢,并使它们变得片状,因为在某些随机情况下它们可能会损坏。

可能的解决方案3:

创建这样的实用程序功能:

var Go = func(function func()) {
    go function()
} 
Run Code Online (Sandbox Code Playgroud)

并method像这样重写:

func method(intr MyInterface) {
    Go(intr.exec())
} 
Run Code Online (Sandbox Code Playgroud)

在测试中,我们可以更改Go为:

var Go = func(function func()) {
    function()
} 
Run Code Online (Sandbox Code Playgroud)

因此,当我们运行测试时,intr.exec将被同步调用,并且可以确定在断言之前调用了我们的模拟方法。
这种解决方案的唯一问题是,它覆盖了golang的基本结构,这是不正确的。


这些是我可以找到的解决方案,但就我所知,还不能令人满意。什么是最佳解决方案?

Zak*_*Zak 6

使用sync.WaitGroup内部模拟

您可以扩展mockInterface以使其等待其他goroutine完成

type mockInterface struct{
    wg sync.WaitGroup // create a wait group, this will allow you to block later
    CallCount int
}

func (m *mockInterface) exec() {
    m.wg.Done() // record the fact that you've got a call to exec
    m.CallCount += 1
}

func (m *mockInterface) currentCount() int {
    m.wg.Wait() // wait for all the call to happen. This will block until wg.Done() is called.
    return m.CallCount
}
Run Code Online (Sandbox Code Playgroud)

在测试中,您可以执行以下操作:

mock := &mockInterface{}
mock.wg.Add(1) // set up the fact that you want it to block until Done is called once.

method(mock)

if mock.currentCount() != 1 {  // this line with block
    // trimmed
}
Run Code Online (Sandbox Code Playgroud)

  • 从使用同步技术的意义上来说,这是相似的。也许我的回答有些不清楚,但是上面描述的方法将所有同步封装在模拟中,并且您不会污染生产代码。 (2认同)

小智 5

这个测试不会像上面提出的 sync.WaitGroup 解决方案那样永远挂起。如果没有调用 mock.exec,它将挂起一秒钟(在这个特定示例中):

package main

import (
    "testing"
    "time"
)

type mockInterface struct {
    closeCh chan struct{}
}

func (m *mockInterface) exec() {
    close(closeCh)
}

func TestMethod(t *testing.T) {
    mock := mockInterface{
        closeCh: make(chan struct{}),
    }

    method(mock)

    select {
    case <-closeCh:
    case <-time.After(time.Second):
        t.Fatalf("expected call to mock.exec method")
    }
}
Run Code Online (Sandbox Code Playgroud)

这基本上就是我上面的回答中的 mc.Wait(time.Second) 。