Goconvey 在 Jenkins 上使用 goroutine 引起恐慌

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


测试可以在这里看到:


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()

    })

}


天涯尽头无女友
浏览 298回答 2
2回答

墨色风雨

因为您正在尝试从另一个 goroutine 执行断言,所以您需要C在func()签名中使用最近添加的上下文结构 ( ) ,然后调用So该上下文。这是您的版本,稍作修改:Convey("TestListApplication", t, func(c C) {&nbsp; &nbsp; s.Comms.MainWaitGroup.Add(1)&nbsp; &nbsp; application := &Application{}&nbsp; &nbsp; err := json.Unmarshal(applicationJSON(), application)&nbsp; &nbsp; So(err, ShouldBeNil)&nbsp; &nbsp; revisions := &Revisions{}&nbsp; &nbsp; err = json.Unmarshal(revisionsJSON(), revisions)&nbsp; &nbsp; So(err, ShouldBeNil)&nbsp; &nbsp; var wg sync.WaitGroup&nbsp; &nbsp; wg.Add(1)&nbsp; &nbsp; go func() {&nbsp; &nbsp; &nbsp; &nbsp; defer wg.Done()&nbsp; &nbsp; &nbsp; &nbsp; line := <-s.Comms.Msg&nbsp; &nbsp; &nbsp; &nbsp; c.So(line, ShouldEqual, "3&nbsp; &nbsp; &nbsp; &nbsp; admin&nbsp; &nbsp; &nbsp; &nbsp;foo\n")&nbsp; &nbsp; }()&nbsp; &nbsp; s.listApplication(application, revisions)&nbsp; &nbsp; wg.Wait()})这是 pull request #264的结果。

繁星点点滴滴

此异常由 启动context.go#mustGetCurrentContext()。你可以看到一个测试触发了相同的异常story_conventions_test.go#TestFailureModeNoContextConvey("Foo", t, func() {&nbsp; &nbsp; done := make(chan int, 1)&nbsp; &nbsp; go func() {&nbsp; &nbsp; &nbsp; &nbsp; defer func() { done <- 1 }()&nbsp; &nbsp; &nbsp; &nbsp; defer expectPanic(t, noStackContext)&nbsp; &nbsp; &nbsp; &nbsp; So(len("I have no context"), ShouldBeGreaterThan, 0)&nbsp; &nbsp; }()&nbsp; &nbsp; <-done})从测试示例来看,如果从 Convey 测试中调用 goroutine 中的 goroutine 似乎会触发无上下文恐慌。由于 Jenkins 从 goroutine 启动测试,这可能解释了为什么您的 GoConvey 测试失败(即使它在您go test直接调用时在 Jenkins 之外运行)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go