Skip to content

testing: go test shouldn't terminate if leftover process remain when executed in verbose mode (-v flag) #12538

Closed

Description

@mildred

go version go1.5 linux/amd64

I wrote a test that didn' terminate in normal mode, but did terminate when running in verbose mode (-v flag). I believe the behaviour should be the same in both cases.

The test uses a TestMain function that looked like:

func TestMain(m *testing.M) {
    var psql *database.Postgresql
    var err error

    log.Println("Create test database")

    psql, db, err = database.StartPostgresqlTempConnect()
        if err != nil {
        log.Fatal(err)
        os.Exit(1)
    }
    defer psql.Close()
    defer db.Close()

    os.Exit(m.Run())
}

The problem being that os.Exit was called, and the defer statement were not being run. There were leftover postgresql processes running.

This is obviously a problem with the test, but I still believe that the go test command should behave the same way, whether the -v flag is specified or not.

Activity

  1. changed the title [-]go test shouldn't terminate if leftover process remain when executed in verbose mode (-v flag)[/-] [+]testing: go test shouldn't terminate if leftover process remain when executed in verbose mode (-v flag)[/+] on Sep 8, 2015
  2. ianlancetaylor commented on Sep 8, 2015

    @ianlancetaylor
    Contributor

    I don't understand why the fact that the deferred functions in TestMain were not run caused a change in behaviour when using the -test.v option. Can you provide a small complete test case showing the problem?

  3. added this to the Go1.6 milestone on Sep 8, 2015
  4. rsc commented on Nov 5, 2015

    @rsc
    Contributor

    I don't see how -test.v could affect this. Also package testing did not call os.Exit: your code did. If you don't want your program to exit, don't call os.Exit. If it does call os.Exit, there's nothing package testing can do about that.

    In general it is fine to have background goroutines running at exit. And while we might look for child subprocesses, some child subprocesses might be okay (for example if the test invoked something that kicked off a background daemon as part of its operation). Inserting that kind of check into package testing seems unwise.

  5. locked and limited conversation to collaborators on Nov 4, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions