Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

coordinate successful and failing fetches #6699

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 47 additions & 6 deletions sourcemap/fleet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ import (
"context"
"net/http"
"net/http/httptest"
"sync"
"sync/atomic"
"testing"

"github.com/go-sourcemap/sourcemap"

"github.com/elastic/apm-server/beater/config"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -83,27 +86,53 @@ func TestFleetFetch(t *testing.T) {
}

func TestFailedAndSuccessfulFleetHostsFetch(t *testing.T) {
type response struct {
consumer *sourcemap.Consumer
err error
}
var (
apikey = "supersecret"
name = "webapp"
version = "1.0.0"
path = "/my/path/to/bundle.js.map"
c = http.DefaultClient
sourceMapPath = "/api/fleet/artifact"
successc = make(chan struct{})
errc = make(chan struct{})
lastc = make(chan struct{})
waitc = make(chan struct{})
resc = make(chan response)
wg sync.WaitGroup
)
wg.Add(3)
defer func() {
close(successc)
close(errc)
close(lastc)
close(resc)
}()

hError := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
wg.Done()
errc <- struct{}{}
http.Error(w, "err", http.StatusInternalServerError)
})
ts0 := httptest.NewServer(hError)

h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
h1 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
wg.Done()
successc <- struct{}{}
wr := zlib.NewWriter(w)
defer wr.Close()
wr.Write([]byte(resp))
})
ts1 := httptest.NewServer(h)
ts2 := httptest.NewServer(h)
ts1 := httptest.NewServer(h1)
h2 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
wg.Done()
lastc <- struct{}{}
close(waitc)
})
ts2 := httptest.NewServer(h2)

fleetCfg := &config.Fleet{
Hosts: []string{ts0.URL[7:], ts1.URL[7:], ts2.URL[7:]},
Expand All @@ -123,9 +152,21 @@ func TestFailedAndSuccessfulFleetHostsFetch(t *testing.T) {
f, err := NewFleetFetcher(c, fleetCfg, cfgs)
assert.NoError(t, err)

consumer, err := f.Fetch(context.Background(), name, version, path)
assert.NoError(t, err)
assert.NotNil(t, consumer)
go func() {
consumer, err := f.Fetch(context.Background(), name, version, path)
resc <- response{consumer, err}
}()
// Make sure every server has received a request
wg.Wait()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we remove this wait group? It seems to me that the channels are already making the test wait until we've received all the signals. What is this wait group adding on top of that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The waitgroup is making sure that each handler has received a request before we attempt to read from any of the channels. Without that, we could potentially read from errc and successc without the third handler ever being invoked.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it, feel free to merge, thanks for the explanation

<-errc
<-successc
res := <-resc
assert.NoError(t, res.err)
assert.NotNil(t, res.consumer)

// Wait for h2
<-lastc
<-waitc
}

func TestAllFailedFleetHostsFetch(t *testing.T) {
Expand Down