3
3
package gofcgisrv
4
4
5
5
import (
6
+ "errors"
7
+ "net"
6
8
"os"
7
9
"os/exec"
8
10
"strings"
9
11
"testing"
10
12
"time"
11
13
)
12
14
15
+ func waitForConn (addr string , timeout time.Duration ) error {
16
+ ticker := time .NewTicker (time .Millisecond * 10 )
17
+ timer := time .NewTimer (timeout )
18
+ defer timer .Stop ()
19
+ defer ticker .Stop ()
20
+ for {
21
+ select {
22
+ case <- ticker .C :
23
+ c , err := net .Dial ("tcp" , addr )
24
+ if err == nil {
25
+ c .Close ()
26
+ return nil
27
+ }
28
+ case <- timer .C :
29
+ return errors .New ("timeout" )
30
+ }
31
+ }
32
+ panic ("Unreachable" )
33
+ }
34
+
13
35
func TestPyServer (t * testing.T ) {
14
36
cmd := exec .Command ("python" , "./testdata/cgi_test.py" , "--port=9001" )
15
37
cmd .Stdout = os .Stdout
@@ -18,10 +40,9 @@ func TestPyServer(t *testing.T) {
18
40
if err != nil {
19
41
t .Fatalf ("Error running cgi_test.py: %v" , err )
20
42
}
21
- defer time .Sleep (time .Millisecond * 10 )
22
43
defer cmd .Process .Kill ()
23
44
24
- time . Sleep ( time . Millisecond * 100 )
45
+ waitForConn ( "127.0.0.1:9001" , time . Second )
25
46
s := NewServer ("127.0.0.1:9001" )
26
47
testRequester (t , httpTestData {
27
48
name : "py fastcgi" ,
@@ -33,7 +54,6 @@ func TestPyServer(t *testing.T) {
33
54
}
34
55
35
56
func TestPyCGI (t * testing.T ) {
36
- time .Sleep (time .Millisecond * 90 )
37
57
s := NewCGI ("python" , "./testdata/cgi_test.py" , "--cgi" )
38
58
testRequester (t , httpTestData {
39
59
name : "py cgi" ,
@@ -43,3 +63,22 @@ func TestPyCGI(t *testing.T) {
43
63
expected : "This is a test" ,
44
64
})
45
65
}
66
+
67
+ func TestPySCGI (t * testing.T ) {
68
+ cmd := exec .Command ("python" , "./testdata/cgi_test.py" , "--scgi" , "--port=9002" )
69
+ // flup barfs some output. Why?? Seems wrong to me.
70
+ err := cmd .Start ()
71
+ if err != nil {
72
+ t .Fatalf ("Error running cgi_test.py: %v" , err )
73
+ }
74
+ defer cmd .Process .Kill ()
75
+ waitForConn ("127.0.0.1:9002" , time .Second )
76
+ s := NewSCGI ("127.0.0.1:9002" )
77
+ testRequester (t , httpTestData {
78
+ name : "py scgi" ,
79
+ f : s ,
80
+ body : strings .NewReader ("This is a test" ),
81
+ status : 200 ,
82
+ expected : "This is a test" ,
83
+ })
84
+ }
0 commit comments