【发布时间】:2021-12-28 19:24:17
【问题描述】:
我的代码库大致如下
type Service struct {
Repo repo // An interface that contains both FunctionOne and FunctionTwo
GoRoutineWaitgroup *sync.WaitGroup
}
func (impl *Service) MyFunction(s string) bool {
a := impl.Repo.FunctionOne()
b := impl.Repo.FunctionTwo()
fmt.Println("Executed Function One and Function two")
go impl.validateMyFunction(a,b)
return true
}
func (impl *Service) validateMyFunction(a string,b string) {
defer helpers.PanicHandler()
impl.GoRoutineWaitgroup.Add(1)
defer impl.GoRoutineWaitgroup.Done()
fmt.Println("a and b are validated")
}
而且我编写的单元测试与此类似。
func TestMyFunction(t *testing.T) {
ms := &Service{}
test := []struct{
input string
output bool
case string
}{
{"a", true, sample}
}
}
for _, test := range tests {
t.Run(test.case, func(t *testing.T) {
mockRepo := new(mockrepo.Repo) // mockRepo contains mocks of original repo layer methods generated using mockery for testing purposes
mockRepo.On("FunctionOne")
mockRepo.On("FunctionTwo")
ms.Repo = mockRepo
op := ms.MyFunction(test.input)
assert.Equal(t, test.Output, op)
})
}
} // Please keep in mind that this is not my actual code, but just a basic structure.
所有测试都成功。但是在执行命令go test -v时,我看到代码中有多个地方出现程序恐慌并给出invalid memory address or nil pointer dereference。我在调试模式下检查了代码,发现问题出在方法validateMyFunction 中的impl.GoRoutineWaitgroup.Add(1) 上,当我注释掉go validateMyFunction(a,b) 并再次运行测试时,日志中没有出现恐慌。那么我该如何解决这个问题呢?如何处理我们从内部启动 goroutine 的函数的单元测试(如本例所示)?
【问题讨论】:
-
您的
WaitGroup在示例代码中是nil。没有任何东西可以初始化它。
标签: unit-testing go concurrency goroutine waitgroup