This repository has been archived by the owner on Feb 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
manager_test.go
86 lines (67 loc) · 2.28 KB
/
manager_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
80
81
82
83
84
85
86
package tablecloth
import (
"net/http"
"testing"
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
func TestManager(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Manager")
}
var _ = Describe("Adding servers", func() {
var (
setupCount int
)
BeforeEach(func() {
theManager = &manager{}
setupCount = 0
setupFunc = func() {
theManager.servers = make(map[string]*serverInfo)
setupCount++
}
})
AfterEach(func() {
for _, si := range theManager.servers {
si.server.Close()
}
})
It("Should add the listener using the given ident", func() {
go ListenAndServe("127.0.0.1:8081", http.NotFoundHandler(), "one")
time.Sleep(10 * time.Millisecond)
listener := theManager.servers["one"].listener
Expect(listener.Addr().String()).To(Equal("127.0.0.1:8081"))
})
It("Should use an ident of 'default' if none given", func() {
go ListenAndServe("127.0.0.1:8081", http.NotFoundHandler())
time.Sleep(10 * time.Millisecond)
listener := theManager.servers["default"].listener
Expect(listener.Addr().String()).To(Equal("127.0.0.1:8081"))
})
Context("listening on multiple addresses", func() {
It("Should allow listening on multiple addresses", func() {
go ListenAndServe("127.0.0.1:8081", http.NotFoundHandler(), "one")
go ListenAndServe("127.0.0.1:8082", http.NotFoundHandler(), "two")
time.Sleep(10 * time.Millisecond)
listener := theManager.servers["one"].listener
Expect(listener.Addr().String()).To(Equal("127.0.0.1:8081"))
listener = theManager.servers["two"].listener
Expect(listener.Addr().String()).To(Equal("127.0.0.1:8082"))
})
It("Should only run the setup function once", func() {
go ListenAndServe("127.0.0.1:8081", http.NotFoundHandler(), "one")
go ListenAndServe("127.0.0.1:8081", http.NotFoundHandler(), "two")
time.Sleep(10 * time.Millisecond)
Expect(setupCount).To(Equal(1))
})
It("Should return an error if given duplicate idents", func() {
go ListenAndServe("127.0.0.1:8081", http.NotFoundHandler(), "foo")
time.Sleep(10 * time.Millisecond)
err := ListenAndServe("127.0.0.1:8082", http.NotFoundHandler(), "foo")
Expect(err).To(MatchError("duplicate ident"))
listener := theManager.servers["foo"].listener
Expect(listener.Addr().String()).To(Equal("127.0.0.1:8081"))
})
})
})