package web
import (
"net/http"
"net/http/httptest"
"testing"
"time"
"craftdesign.group/quarry/internal/drain"
)
// Every web stream opens through liveStream, so it is the one place a
// stream declares itself to a stopping server.
func TestAWebStreamIsEndedByTheStop(t *testing.T) {
d := drain.New()
d.StopStreams()
ended := make(chan bool, 1)
h := d.Wrap(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
liveStream(w)
select {
case <-req.Context().Done():
ended <- true
case <-time.After(2 * time.Second):
ended <- false
}
}))
h.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest("GET", "/stream", nil))
if !<-ended {
t.Fatal("FAIL: a stream opened through liveStream outlived the stop")
}
t.Log("ok: liveStream marks its request live")
}