-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathmodel_http_test.go
79 lines (59 loc) · 1.86 KB
/
model_http_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package dev
import (
"io"
"net"
"net/http"
"path/filepath"
"testing"
"github.com/udhos/jazigo/conf"
"github.com/udhos/jazigo/temp"
)
func TestHTTP1(t *testing.T) {
t.Logf("TestHTTP1: starting")
// launch bogus test server
addr := ":2001"
s, listenErr := spawnServerHTTP(t, addr)
if listenErr != nil {
t.Errorf("could not spawn bogus HTTP server: %v", listenErr)
}
t.Logf("TestHTTP1: server running on %s", addr)
// run client test
logger := &testLogger{t}
tab := NewDeviceTable()
opt := conf.NewOptions()
opt.Set(&conf.AppConfig{MaxConcurrency: 3, MaxConfigFiles: 10})
RegisterModels(logger, tab)
CreateDevice(tab, logger, "http", "lab1", "localhost"+addr, "", "", "", "", false, nil)
repo := temp.MakeTempRepo()
defer temp.CleanupTempRepo()
requestCh := make(chan FetchRequest)
errlogPrefix := filepath.Join(repo, "errlog_test.")
go Spawner(tab, logger, requestCh, repo, errlogPrefix, opt, NewFilterTable(logger))
good, bad, skip := Scan(tab, tab.ListDevices(), logger, opt.Get(), requestCh)
if good != 1 || bad != 0 || skip != 0 {
t.Errorf("good=%d bad=%d skip=%d", good, bad, skip)
}
close(requestCh) // shutdown Spawner - we might exit first though
s.close() // shutdown server
<-s.done // wait termination of accept loop goroutine
}
func spawnServerHTTP(t *testing.T, addr string) (*testServer, error) {
t.Logf("spawnServerHTTP: will listen on %s", addr)
ln, err := net.Listen("tcp", addr)
if err != nil {
return nil, err
}
t.Logf("spawnServerHTTP: listening on %s", addr)
s := &testServer{listener: ln, done: make(chan int)}
m := http.NewServeMux()
m.HandleFunc("/", rootHandler) // default handler
go func() {
err := http.Serve(ln, m)
t.Logf("spawnServerHTTP: http.Serve: exited: %v", err)
close(s.done)
}()
return s, nil
}
func rootHandler(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "hello web client\n")
}