refactor(oohelperd): improve tests implementation (#835)

After this diff has landed, we have addressed all the points
originally published at https://github.com/ooni/probe/issues/2134.
This commit is contained in:
Simone Basso 2022-07-05 20:25:18 +02:00 committed by GitHub
commit d419ed8ac8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 186 additions and 168 deletions

View file

@ -4,6 +4,7 @@ package main
import (
"context"
"flag"
"net"
"net/http"
"sync"
"time"
@ -11,19 +12,21 @@ import (
"github.com/apex/log"
"github.com/ooni/probe-cli/v3/internal/model"
"github.com/ooni/probe-cli/v3/internal/netxlite"
"github.com/ooni/probe-cli/v3/internal/runtimex"
)
const maxAcceptableBody = 1 << 24
var (
endpoint = flag.String("endpoint", ":8080", "Endpoint where to listen")
srvcancel context.CancelFunc
srvctx context.Context
srvwg = new(sync.WaitGroup)
srvAddr = make(chan string, 1) // with buffer
srvCancel context.CancelFunc
srvCtx context.Context
srvWg = new(sync.WaitGroup)
)
func init() {
srvctx, srvcancel = context.WithCancel(context.Background())
srvCtx, srvCancel = context.WithCancel(context.Background())
}
func newResolver() model.Resolver {
@ -48,10 +51,7 @@ func main() {
debug := flag.Bool("debug", false, "Toggle debug mode")
flag.Parse()
log.SetLevel(logmap[*debug])
testableMain()
}
func testableMain() {
defer srvCancel()
mux := http.NewServeMux()
mux.Handle("/", &handler{
MaxAcceptableBody: maxAcceptableBody,
@ -64,9 +64,13 @@ func testableMain() {
NewResolver: newResolver,
})
srv := &http.Server{Addr: *endpoint, Handler: mux}
srvwg.Add(1)
go srv.ListenAndServe()
<-srvctx.Done()
listener, err := net.Listen("tcp", *endpoint)
runtimex.PanicOnError(err, "net.Listen failed")
srvAddr <- listener.Addr().String()
srvWg.Add(1)
go srv.Serve(listener)
<-srvCtx.Done()
shutdown(srv)
srvwg.Done()
listener.Close()
srvWg.Done()
}