本文整理汇总了Golang中net/http/httptest.Server.CloseClientConnections方法的典型用法代码示例。如果您正苦于以下问题:Golang Server.CloseClientConnections方法的具体用法?Golang Server.CloseClientConnections怎么用?Golang Server.CloseClientConnections使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net/http/httptest.Server
的用法示例。
在下文中一共展示了Server.CloseClientConnections方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: doTestProxy
func doTestProxy(t *testing.T, origin *httptest.Server, server *httptest.Server, disableKeepAlives bool) {
u, _ := url.Parse(server.URL)
client := &http.Client{Transport: &http.Transport{
Dial: func(network, addr string) (net.Conn, error) {
conn, err := net.Dial("tcp", u.Host)
if err == nil {
initReq, reqErr := http.NewRequest("GET", fmt.Sprintf("http://%v", addr), nil)
if reqErr != nil {
return nil, fmt.Errorf("Unable to construct initial request: %v", reqErr)
}
initReq.Header.Set(XLanternPersistent, "true")
writeErr := initReq.Write(conn)
if writeErr != nil {
return nil, fmt.Errorf("Unable to write initial request: %v", writeErr)
}
}
return conn, err
},
DisableKeepAlives: disableKeepAlives,
}}
// Do a simple GET
if !testGet(t, client, origin) {
return
}
// Do another GET to test keepalive functionality
if !testGet(t, client, origin) {
return
}
// Forcibly close client connections and make sure we can still proxy
origin.CloseClientConnections()
testGet(t, client, origin)
}
示例2: TestHandleHttpFailures
func (t *WebRuleTest) TestHandleHttpFailures(c *C) {
var s *httptest.Server
s = httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
s.CloseClientConnections()
}))
r := NewWebRule(
"test rule",
s.URL,
"cats",
"dogs")
sane, triggered := r.TestTriggered()
c.Check(sane, Equals, false)
c.Check(triggered, Equals, false)
}
示例3:
logger *lagertest.TestLogger
token *fake_token.FakeToken
server *httptest.Server
)
BeforeEach(func() {
token = &fake_token.FakeToken{}
database = &fake_db.FakeDB{}
logger = lagertest.NewTestLogger("event-handler-test")
handler = *handlers.NewEventStreamHandler(token, database, logger)
})
AfterEach(func(done Done) {
if server != nil {
go func() {
server.CloseClientConnections()
server.Close()
close(done)
}()
} else {
close(done)
}
})
Describe(".EventStream", func() {
var (
response *http.Response
eventStreamDone chan struct{}
)
BeforeEach(func() {
示例4: TestSuperviceRetrieLimit0
func TestSuperviceRetrieLimit0(t *testing.T) {
buf.Reset()
var wg sync.WaitGroup
var server *httptest.Server
var counter int
var h http.HandlerFunc = func(w http.ResponseWriter, r *http.Request) {
if counter > 0 {
server.CloseClientConnections()
}
fmt.Fprintln(w, "Hello")
counter++
}
server = httptest.NewServer(h)
defer server.Close()
log_s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("User-agent") != "epazote" {
t.Error("Expecting User-agent: epazote")
}
decoder := json.NewDecoder(r.Body)
var i map[string]interface{}
err := decoder.Decode(&i)
if err != nil {
t.Error(err)
}
// check exit
if i["exit"].(float64) != 0 {
t.Errorf("Expecting: 0 got: %v", i["exit"])
}
// check retries
if _, ok := i["retries"]; ok {
t.Errorf("retries key found")
}
// check url
if _, ok := i["url"]; !ok {
t.Error("URL key not found")
}
// check output
if i["status"].(float64) != 200 {
t.Errorf("Expecting status: %d got: %v", 200, i["status"])
}
wg.Done()
}))
defer log_s.Close()
s := make(Services)
s["s 1"] = &Service{
Name: "s 1",
URL: server.URL,
RetryLimit: 0,
RetryInterval: 1,
ReadLimit: 1024,
Log: log_s.URL,
Expect: Expect{
Status: 200,
},
}
ez := &Epazote{
Services: s,
}
wg.Add(1)
ez.Supervice(s["s 1"])()
wg.Wait()
rc := s["s 1"].retryCount
if rc != 0 {
t.Errorf("Expecting retryCount = 0 got: %d", rc)
}
}
示例5: TestSuperviceRetrie
// server.CloseClientConnections not workng on golang 1.6
func TestSuperviceRetrie(t *testing.T) {
buf.Reset()
var wg sync.WaitGroup
var server *httptest.Server
var counter int
var h http.HandlerFunc = func(w http.ResponseWriter, r *http.Request) {
if counter <= 1 {
server.CloseClientConnections()
}
w.Header().Set("X-Abc", "xyz")
fmt.Fprintln(w, "Hello, molcajete.org")
counter++
}
server = httptest.NewServer(h)
defer server.Close()
log_s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("User-agent") != "epazote" {
t.Error("Expecting User-agent: epazote")
}
decoder := json.NewDecoder(r.Body)
var i map[string]interface{}
err := decoder.Decode(&i)
if err != nil {
t.Error(err)
}
// check exit
if i["exit"].(float64) != 0 {
t.Errorf("Expecting: 0 got: %v", i["exit"])
}
// check because
e := "Body regex match: molcajete"
if i["because"] != e {
t.Errorf("Expecting: %q, got: %v", e, i["because"])
}
// check retries
if i["retries"].(float64) != 2 {
t.Errorf("Expecting: 2 got: %v", i["retries"])
}
// check url
if _, ok := i["url"]; !ok {
t.Error("URL key not found")
}
// check output
if i["status"].(float64) != 200 {
t.Errorf("Expecting status: %d got: %v", 200, i["status"])
}
wg.Done()
}))
defer log_s.Close()
s := make(Services)
re := regexp.MustCompile(`molcajete`)
s["s 1"] = &Service{
Name: "s 1",
URL: server.URL,
RetryLimit: 3,
ReadLimit: 17,
Log: log_s.URL,
Expect: Expect{
Status: 200,
Header: map[string]string{
"X-Abc": "xyz",
},
Body: "molcajete",
body: re,
},
}
ez := &Epazote{
Services: s,
}
wg.Add(1)
ez.Supervice(s["s 1"])()
wg.Wait()
// 1 try, 2 tries
rc := s["s 1"].retryCount
if rc != 2 {
t.Errorf("Expecting retryCount = 2 got: %d", rc)
}
if counter != 3 {
t.Errorf("Expecting 3 got: %v", counter)
}
}
示例6:
consulClient = consuladapter.NewConsulClient(client)
presenceTTL = 6 * time.Second
})
Context("for longer than the MonitorRetries * MonitorRetryTime", func() {
It("drops its presence", func() {
// Serve 500's to simulate a leader election. We know that we need
// to serve more than lockTTL / 2 500's to lose the lock.
for i := 0; i < 4; i++ {
Eventually(serveFiveHundreds).Should(BeSent(struct{}{}))
}
// Close the existing connection with consul so that the
// lock monitor is forced to retry. This is because consul
// performs a blocking query until the lock index is changed.
fmt.Printf("Closing Blocking Connections")
fakeConsul.CloseClientConnections()
Eventually(logger, 7*time.Second).Should(Say("presence-lost"))
Consistently(presenceProcess.Wait()).ShouldNot(Receive())
})
})
Context("for less than the MonitorRetries * MonitorRetryTime", func() {
It("does not lose the lock", func() {
// Serve 500's to simulate a leader election. We know that if we
// serve less than lockTTL / 2 500's, we will not lose the lock.
for i := 0; i < 2; i++ {
Eventually(serveFiveHundreds).Should(BeSent(struct{}{}))
}
// Close the existing connection with consul so that the
// lock monitor is forced to retry. This is because consul
示例7: serverHost
var matchServer *httptest.Server
var matchServerHost string
var _ = BeforeSuite(func() {
match.PhaseDuration = 50 * time.Millisecond
SetDefaultEventuallyTimeout(2 * time.Second)
})
var _ = BeforeEach(func() {
server.ClearMatches()
matchServer = httptest.NewServer(server.NewMatchHandler())
matchServerHost = serverHost(*matchServer)
})
var _ = AfterEach(func() {
matchServer.CloseClientConnections()
matchServer.Close()
})
func serverHost(s httptest.Server) string {
u, err := url.Parse(s.URL)
if err != nil {
log.Fatalln("Could not get server host from server url:", s.URL, err)
}
return u.Host
}
func matchURL(host, matchID, playerID string) string {
return fmt.Sprintf("ws://%s/match/%s/ws?player=%s", host, matchID, playerID)
}