首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Go goroutine测试未达到预期呼叫次数

Go goroutine测试未达到预期呼叫次数
EN

Stack Overflow用户
提问于 2021-05-06 00:25:23
回答 1查看 1.3K关注 0票数 0

我是新来的。我正在尝试测试Go例程中的函数调用,但是错误消息失败了。

预期的呼叫数(8)与实际呼叫数(0)不匹配。

我的测试代码如下:

代码语言:javascript
运行
复制
package executor

import (
    "testing"
  "sync"
    "github.com/stretchr/testify/mock"
)

type MockExecutor struct {
    mock.Mock
  wg sync.WaitGroup
}

func (m *MockExecutor) Execute() {
  defer m.wg.Done()
}

func TestScheduleWorksAsExpected(t *testing.T) {

    scheduler := GetScheduler()
    executor := &MockExecutor{}
    scheduler.AddExecutor(executor)

    // Mock exptectations
    executor.On("Execute").Return()
    

    // Function Call
  executor.wg.Add(8)
    scheduler.Schedule(2, 1, 4)
  executor.wg.Wait()

  executor.AssertNumberOfCalls(t, "Execute", 8)

}

我的应用程序代码是:

代码语言:javascript
运行
复制
package executor

import (
    "sync"
    "time"
)

type Scheduler interface {
    Schedule(repeatRuns uint16, coolDown uint8, parallelRuns uint64)
    AddExecutor(executor Executor)
}

type RepeatScheduler struct {
    executor  Executor
    waitGroup sync.WaitGroup
}

func GetScheduler() Scheduler {
    return &RepeatScheduler{}
}

func (r *RepeatScheduler) singleRun() {
    defer r.waitGroup.Done()
    r.executor.Execute()
}

func (r *RepeatScheduler) AddExecutor(executor Executor) {
    r.executor = executor
}

func (r *RepeatScheduler) repeatRuns(parallelRuns uint64) {
    for count := 0; count < int(parallelRuns); count += 1 {
        r.waitGroup.Add(1)
        go r.singleRun()
    }

    r.waitGroup.Wait()
}

func (r *RepeatScheduler) Schedule(repeatRuns uint16, coolDown uint8, parallelRuns uint64) {

    for repeats := 0; repeats < int(repeatRuns); repeats += 1 {
        r.repeatRuns(parallelRuns)
        time.Sleep(time.Duration(coolDown))
    }

}

你能告诉我我在这里可能做错了什么吗?我用的是Go 1.16.3。调试代码时,可以看到调用了Execute()函数,但不能注册函数调用

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-06 02:06:30

您需要调用Called(),以便mock.Mock记录Execute()已被调用的事实。由于您不担心参数或返回值,以下内容应该解决您的问题:

代码语言:javascript
运行
复制
func (m *MockExecutor) Execute() {
    defer m.wg.Done()
    m.Called()
}

但是,我注意到,当前编写测试的方式--这个测试可能无法完成您想要的任务。这是因为:

在调用forever).

  • After之前,您正在调用executor.wg.Wait() (这将等待函数被调用的预期次数),因此,如果没有至少调用Execute()的预期次数(wg.Wait()将阻塞executor.AssertNumberOfCalls的预期次数),那么测试将永远不会完成(wg.Wait()将阻塞executor.AssertNumberOfCalls的预期次数,即存在竞争条件的预期次数(如果executor仍在运行,则在executor.AssertNumberOfCalls和下一个m.Called()之间进行竞争)。如果wg.Done()确实被调用了额外的时间,您就会感到恐慌(我想您可能会认为失败了!)但我可能会简化一些测试:

代码语言:javascript
运行
复制
scheduler.Schedule(2, 1, 4)
time.Sleep(time.Millisecond) // Wait long enough that all executions are guaranteed to have completed (should be quick as Schedule waits for go routines to end)
executor.AssertNumberOfCalls(t, "Execute", 8)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67410367

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档