lundi 15 février 2021

Error while running tests on a single Go directory file

I'm following a tutorial on Golang, and I have the actual file tree:

.
├── arrays
├── concurrency
├── di
├── hello-world
├── integers
├── iteration
├── maps
├── mocking
├── pointers
├── racer
└── structs

If I run the tests in all of the folders files, they work, except for the folder called racer, where running the tests gives me the following error:

# runtime/cgo
cgo: exec /missing-cc: fork/exec /missing-cc: no such file or directory
FAIL    github.com/Gabriel2233/go-with-tests/racer [build failed]

Is it related to something I'm doing in the files? This is the test file

package racer

import (
    "net/http"
    "net/http/httptest"
    "testing"
    "time"
)

func TestRacer(t *testing.T) {
    slowServer := makeDelayedServer(20 * time.Millisecond)
    fastServer := makeDelayedServer(0 * time.Millisecond)

    defer slowServer.Close()
    defer fastServer.Close()

    slowURL := slowServer.URL
    fastURL := fastServer.URL

    want := fastURL
    got := Racer(slowURL, fastURL)

    if got != want {
        t.Errorf("got %q, want %q", got, want)
    }

    t.Run("returns error if race takes more than 10s", func(t *testing.T) {
        serverA := makeDelayedServer(time.Second * 11)
        serverB := makeDelayedServer(time.Second * 12)

        defer serverA.Close()
        defer serverB.Close()

        _, err := Racer(serverA.URL, serverB.URL)

        if err == nil {
            t.Error("expected an error but didn't get one")
        }
    })
}

func makeDelayedServer(delay time.Duration) *httptest.Server {
    handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        time.Sleep(delay * time.Millisecond)
        w.WriteHeader(http.StatusOK)
    })
    server := httptest.NewServer(handler)

    return server
}

This is the racer.go file

package racer

import (
    "net/http"
    "time"
)

func Racer(a, b string) (winner string) {
    select {
    case <-ping(a):
        return a
    case <-ping(b):
        return b
    }
}

func ping(url string) chan struct{} {
    ch := make(chan struct{})
    go func() {
        http.Get(url)
        close(ch)
    }()

    return ch
}

func measureResponseTime(url string) time.Duration {
    start := time.Now()
    http.Get(url)
    return time.Since(start)
}

Aucun commentaire:

Enregistrer un commentaire