jenkins - Goconvey 在 Jenkins 上用 go routine 引起 panic

标签 jenkins go tdd goconvey

我有一组使用 GoConvey 和 Go 1.3.1 的测试,在本地工作得很好。但是当我使用 Jenkins 触发构建时,也使用 Go 1.3.1,我从 Goconvey 那里得到了一个与我在测试中使用的 go 例程相关的 panic 。

测试可以在这里看到:

func TestListApplication(t *testing.T) {
    s := &Session{}
    s.Username = "foo"
    s.Password = "bar"
    ts := serveHTTP(t)
    defer ts.Close()
    s.Baseurl = ts.URL
    s.initialize()

    go func() {
        <-s.Comms.AppCount
    }()

    Convey("TestListApplication", t, func() {
        s.Comms.MainWaitGroup.Add(1)

        application := &Application{}
        err := json.Unmarshal(applicationJSON(), application)
        So(err, ShouldBeNil)

        revisions := &Revisions{}
        err = json.Unmarshal(revisionsJSON(), revisions)
        So(err, ShouldBeNil)

        var wg sync.WaitGroup
        wg.Add(1)
        go func() {
            defer wg.Done()
            line := <-s.Comms.Msg
            So(line, ShouldEqual, "3        admin       foo\n")
        }()
        s.listApplication(application, revisions)
        wg.Wait()
    })
}

这里的错误:

86 assertions thus far

..
88 assertions thus far

panic: Convey operation made without context on goroutine stack.
Hint: Perhaps you meant to use `Convey(..., func(c C){...})` ?

goroutine 115 [running]:
runtime.panic(0x350d80, 0xc208083050)
    /Users/administrator/jenkins/tools/org.jenkinsci.plugins.golang.GolangInstallation/1.3.1/src/pkg/runtime/panic.c:279 +0xf5
github.com/smartystreets/goconvey/convey.conveyPanic(0x4960d0, 0x78, 0x0, 0x0, 0x0)
    /Users/administrator/jenkins/workspace/tropoCLI/golang/src/github.com/smartystreets/goconvey/convey/context.go:20 +0x6a
github.com/smartystreets/goconvey/convey.mustGetCurrentContext(0x5ecea0)
    /Users/administrator/jenkins/workspace/tropoCLI/golang/src/github.com/smartystreets/goconvey/convey/context.go:52 +0x57
github.com/smartystreets/goconvey/convey.So(0x2d78e0, 0xc208001e80, 0x48f210, 0xc208001e60, 0x1, 0x1)
    /Users/administrator/jenkins/workspace/tropoCLI/golang/src/github.com/smartystreets/goconvey/convey/doc.go:123 +0x1e
_/Users/administrator/jenkins/workspace/tropoCLI.func·048()
    /Users/administrator/jenkins/workspace/tropoCLI/tropoCLI_test.go:222 +0x17d
created by _/Users/administrator/jenkins/workspace/tropoCLI.func·049
    /Users/administrator/jenkins/workspace/tropoCLI/tropoCLI_test.go:223 +0x398

goroutine 16 [chan receive]:
testing.RunTests(0x48f340, 0x5ed680, 0x29, 0x29, 0x48f701)
    /Users/administrator/jenkins/tools/org.jenkinsci.plugins.golang.GolangInstallation/1.3.1/src/pkg/testing/testing.go:505 +0x923
testing.Main(0x48f340, 0x5ed680, 0x29, 0x29, 0x5f5b40, 0x0, 0x0, 0x5f5b40, 0x0, 0x0)
    /Users/administrator/jenkins/tools/org.jenkinsci.plugins.golang.GolangInstallation/1.3.1/src/pkg/testing/testing.go:435 +0x84
main.main()
    _/Users/administrator/jenkins/workspace/tropoCLI/_test/_testmain.go:127 +0x9c

最佳答案

因为您正在尝试从另一个 goroutine 执行断言,所以您需要在 func() 中使用最近添加的上下文结构 (C)签名,然后从该上下文中调用 So。这是您稍作修改的版本:

Convey("TestListApplication", t, func(c C) {
    s.Comms.MainWaitGroup.Add(1)

    application := &Application{}
    err := json.Unmarshal(applicationJSON(), application)
    So(err, ShouldBeNil)

    revisions := &Revisions{}
    err = json.Unmarshal(revisionsJSON(), revisions)
    So(err, ShouldBeNil)

    var wg sync.WaitGroup
    wg.Add(1)
    go func() {
        defer wg.Done()
        line := <-s.Comms.Msg
        c.So(line, ShouldEqual, "3        admin       foo\n")
    }()
    s.listApplication(application, revisions)
    wg.Wait()
})

这是拉动的结果 request #264 .

关于jenkins - Goconvey 在 Jenkins 上用 go routine 引起 panic ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26920344/

相关文章:

javascript - 如何使用 mocha.js 测试多个异步流程事件

ruby - Rspec : expect vs expect with block - what's the difference?

go - 如果我使用 go.mod,如何使用 gotext extract?

Golang channel 问题

go - 在编码为 JSON 之前将结构转换为不同的结构

git - Jenkins 也为共享库更改运行轮询或推送触发作业

c# - 对返回匿名类型的 Response RedirectToRoute 进行单元测试

azure - Azure 负载均衡器的 ARM 模板部署失败 - Jenkins 未给出任何错误

email - Jenkins EMail-ext 插件在电子邮件正文中发送 groovy 代码

jenkins - 从正在运行的版本中读取consoleText URL仅返回前10000行