title: Go hangs when running tests
body: <p>I'm writing a web application in go and it runs just fine. However, when running the tests for a package, the <code>go test</code> command just hangs (it does nothing, not even terminate).</p>
  
  <p>I have a function for testing which starts the server:</p>
  
  <pre><code>func mkroutes(t *testing.T, f func()) {
    handlerRegistry = handlerList([]handler{})
    middlewareRegistry = []middleware{}
    if testListener == nil {
      _testListener, err := net.Listen("tcp", ":8081")
      testListener = _testListener
      if err != nil {
        fmt.Printf("[Fail] could not start tcp server:\n%s\n", err)
      }
    }
    f()
    go func() {
      if err := serve(testListener, nil); err != nil {
        fmt.Printf("[Fail] the server failed to start:\n%s\n", err)
        t.FailNow()
      }
    }()
  }
  </code></pre>
  
  <p>If I change the port that it listens on, everything runs fine (all the tests fail though, since they can't connect to the server). This shows that the code is indeed running, but if I log something in the function, or even in the <code>init</code> function, while the port is correct, it again breaks.</p>
  
  <p>After I force the <code>go test</code> command to terminate manually, it does print whatever I logged, then exit. This leads me to believe that something else is blocking on the main thread before execution reaches the log, but that's impossible since changing the port makes a difference.</p>
  
  <p>The package doesn't have any <code>init</code> functions and the only code that runs on startup is <code>var sessionStore = sessions.NewCookieStore([]byte("test-key"))</code> which is using the package <code>github.com/gorilla/sessions</code>. When I run the program normally, this causes no problems, and I don't see anything in the package's source that would cause it to behave differently in testing.</p>
  
  <p>That's the only package outside the standard library which is imported.</p>
  
  <p>I can provide any other code in the package, but I have no idea what's relevant.</p>