diff options
author | Rick Arnold <rickarnoldjr@gmail.com> | 2014-03-18 13:03:03 +1100 |
---|---|---|
committer | Andrew Gerrand <adg@golang.org> | 2014-03-18 13:03:03 +1100 |
commit | 1f1f69e389a30fd8941789fd04bfd946c9aa39fc (patch) | |
tree | ec9664a0389fd75b4f3431ed9844ff707fec17b9 /doc/articles/wiki/final.go | |
parent | e45f5cd5f17c88f979adcbebaa3bead9b0d7cc1f (diff) | |
download | go-git-1f1f69e389a30fd8941789fd04bfd946c9aa39fc.tar.gz |
build: fix race in doc/articles/wiki test
The original test would open a local port and then immediately close it
and use the port number in subsequent tests. Between the port being closed
and reused by the later process, it could be opened by some other program
on the machine.
Changed the test to run the server process directly and have it save the
assigned port to a text file to be used by client processes.
Fixes #5564.
LGTM=adg
R=golang-codereviews, gobot, adg
CC=golang-codereviews
https://golang.org/cl/72290043
Diffstat (limited to 'doc/articles/wiki/final.go')
-rw-r--r-- | doc/articles/wiki/final.go | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/doc/articles/wiki/final.go b/doc/articles/wiki/final.go index f15794d660..d84c1ffb26 100644 --- a/doc/articles/wiki/final.go +++ b/doc/articles/wiki/final.go @@ -5,12 +5,19 @@ package main import ( + "flag" "html/template" "io/ioutil" + "log" + "net" "net/http" "regexp" ) +var ( + addr = flag.Bool("addr", false, "find open address and print to final-port.txt") +) + type Page struct { Title string Body []byte @@ -81,8 +88,24 @@ func makeHandler(fn func(http.ResponseWriter, *http.Request, string)) http.Handl } func main() { + flag.Parse() http.HandleFunc("/view/", makeHandler(viewHandler)) http.HandleFunc("/edit/", makeHandler(editHandler)) http.HandleFunc("/save/", makeHandler(saveHandler)) + + if *addr { + l, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + log.Fatal(err) + } + err = ioutil.WriteFile("final-port.txt", []byte(l.Addr().String()), 0644) + if err != nil { + log.Fatal(err) + } + s := &http.Server{} + s.Serve(l) + return + } + http.ListenAndServe(":8080", nil) } |