Server in Docker Container connection refused, should I add time.Sleep(100 * time.Millisecond) to my tests?

I am currently working on a server that is designed to be run in a Docker container.

Here is my setup method for my tests:

func TestMain(m *testing.M) {
    schedulerName := "scheduler1"
    IP, err := container.StartNewScheduler(schedulerName)
    if err != nil {
        log.Println("Could not create container.")
    serverIP = IP
    code := m.Run()

The line container.StartNewScheduler(schedulername) boots up a new docker container called "scheduler1" and tells it to run the server inside of it.

Next I run my tests with the container running in the background, right now I only have one test.

func TestNewScheduler(t *testing.T) {
    testCodeInput := "THIS IS A TEST"
    requestBody, err := json.Marshal(map[string]string{
        "Code": fmt.Sprintf("print("%s")", testCodeInput),

    if err != nil {

    url := fmt.Sprintf("http://%s:%d/execute/python", serverIP, 3000)
    contentType := "application/json"
    body := bytes.NewBuffer(requestBody)

    response := post(url, contentType, body, t)
    actual := parseOutput(response.Body, t)

    expected := fmt.Sprintf("{"Stdout":"%sn"}", testCodeInput)
    if actual != expected {
        t.Fatalf("Expected %s, but got %s", expected, actual)

The problem that I am running into is sometimes I get a connection refused and sometimes I don’t.

server_container_test.go:51: Post dial tcp connect: connection refused

I noticed that whenever I try and debug the issue everything seems to work fine. My running theory is because when I step through my code the container has more time to start up and get the server running inside it.

In order to test my Hypothesis I added a second post call in my post method with a timer set before I call it.

func post(url string, contentType string, body io.Reader, t *testing.T) *http.Response {
    response, err := http.Post(url, contentType, body)
    if err != nil {
        //There is an error where the container takes a second to boot up and so
        //the scheduler isn't running when the first request is sent, so we try
        //one more time here and  check again.
        time.Sleep(100 * time.Millisecond) <--- Right here
        response, err = http.Post(url, contentType, body)
        if err != nil {
    return response

Does anyone else have any guesses as to what could be causing me this issue?

If my hypothesis is correct is this the best way to fix this? Is it a bad idea to add a time.Sleep to your tests?


Source: Docker Questions