Commit 1f1f69e3 authored by Rick Arnold's avatar Rick Arnold Committed by Andrew Gerrand

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
parent e45f5cd5
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
all: index.html all: index.html
CLEANFILES=get.bin final-test.bin a.out CLEANFILES=get.bin final.bin a.out
clean: clean:
rm -f $(CLEANFILES) rm -f $(CLEANFILES)
...@@ -5,12 +5,19 @@ ...@@ -5,12 +5,19 @@
package main package main
import ( import (
"flag"
"html/template" "html/template"
"io/ioutil" "io/ioutil"
"log"
"net"
"net/http" "net/http"
"regexp" "regexp"
) )
var (
addr = flag.Bool("addr", false, "find open address and print to final-port.txt")
)
type Page struct { type Page struct {
Title string Title string
Body []byte Body []byte
...@@ -81,8 +88,24 @@ func makeHandler(fn func(http.ResponseWriter, *http.Request, string)) http.Handl ...@@ -81,8 +88,24 @@ func makeHandler(fn func(http.ResponseWriter, *http.Request, string)) http.Handl
} }
func main() { func main() {
flag.Parse()
http.HandleFunc("/view/", makeHandler(viewHandler)) http.HandleFunc("/view/", makeHandler(viewHandler))
http.HandleFunc("/edit/", makeHandler(editHandler)) http.HandleFunc("/edit/", makeHandler(editHandler))
http.HandleFunc("/save/", makeHandler(saveHandler)) 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) http.ListenAndServe(":8080", nil)
} }
...@@ -7,7 +7,7 @@ set -e ...@@ -7,7 +7,7 @@ set -e
wiki_pid= wiki_pid=
cleanup() { cleanup() {
kill $wiki_pid kill $wiki_pid
rm -f test_*.out Test.txt final-test.bin final-test.go a.out get.bin rm -f test_*.out Test.txt final.bin final-port.txt a.out get.bin
} }
trap cleanup 0 INT trap cleanup 0 INT
...@@ -19,13 +19,25 @@ if [ "$1" == "-all" ]; then ...@@ -19,13 +19,25 @@ if [ "$1" == "-all" ]; then
fi fi
go build -o get.bin get.go go build -o get.bin get.go
addr=$(./get.bin -addr) go build -o final.bin final.go
sed s/:8080/$addr/ < final.go > final-test.go (./final.bin --addr) &
go build -o final-test.bin final-test.go
(./final-test.bin) &
wiki_pid=$! wiki_pid=$!
./get.bin --wait_for_port=5s http://$addr/edit/Test > test_edit.out l=0
while [ ! -f ./final-port.txt ]
do
l=$(($l+1))
if [ "$l" -gt 5 ]
then
echo "port not available within 5 seconds"
exit 1
break
fi
sleep 1
done
addr=$(cat final-port.txt)
./get.bin http://$addr/edit/Test > test_edit.out
diff -u test_edit.out test_edit.good diff -u test_edit.out test_edit.good
./get.bin -post=body=some%20content http://$addr/save/Test > test_save.out ./get.bin -post=body=some%20content http://$addr/save/Test > test_save.out
diff -u test_save.out test_view.good # should be the same as viewing diff -u test_save.out test_view.good # should be the same as viewing
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment