Skip to content

Commit

Permalink
fix random failure of jmx test cases (#290)
Browse files Browse the repository at this point in the history
  • Loading branch information
localvar committed Oct 9, 2021
1 parent 6131427 commit bf95a21
Showing 1 changed file with 53 additions and 27 deletions.
80 changes: 53 additions & 27 deletions pkg/util/jmxtool/agent_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,46 @@ import (
"github.com/megaease/easegress/pkg/object/meshcontroller/spec"
)

func httpServer(finished chan bool, notFoundFlag bool) {
m := http.NewServeMux()
s := http.Server{Addr: ":8181", Handler: m}
m.HandleFunc("/shutdown", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Goobey, %q", html.EscapeString(r.URL.Path))
s.Shutdown(context.Background())
})
if !notFoundFlag {
m.HandleFunc(serviceConfigURL, func(w http.ResponseWriter, r *http.Request) {
func createHTTPServer(finished chan bool, notFoundFlag bool) error {
go func() {
m := http.NewServeMux()
s := http.Server{Addr: ":8181", Handler: m}
m.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
})
m.HandleFunc(canaryConfigURL, func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
m.HandleFunc("/shutdown", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Goodbye, %q", html.EscapeString(r.URL.Path))
s.Shutdown(context.Background())
})
if !notFoundFlag {
m.HandleFunc(serviceConfigURL, func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
})
m.HandleFunc(canaryConfigURL, func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
})
}
if err := s.ListenAndServe(); err != nil && err != http.ErrServerClosed {
fmt.Println(err)
}
fmt.Println("Finished")
finished <- true
}()

var client = &http.Client{Timeout: time.Second}
for i := 0; ; i++ {
resp, err := client.Get("http:https://127.0.0.1:8181/hello")
if err == nil {
resp.Body.Close()
break
}
if i >= 9 {
return err
}
time.Sleep(10 * time.Millisecond)
}
if err := s.ListenAndServe(); err != nil && err != http.ErrServerClosed {
fmt.Println(err)
}
fmt.Println("Finished")
finished <- true

return nil
}

func getTestService() spec.Service {
Expand All @@ -71,17 +91,22 @@ func getTestService() spec.Service {

func TestAgentClientSuccess(t *testing.T) {
logger.InitNop()

finished := make(chan bool)
go httpServer(finished, false)
err := createHTTPServer(finished, false)
if err != nil {
t.Errorf("failed to create HTTP server: %v\n", err)
return
}

agent := NewAgentClient("127.0.0.1", "8181")
fmt.Printf("%+v\n", agent)

service := getTestService()
// UpdateService check
err := agent.UpdateService(&service, 1)
err = agent.UpdateService(&service, 1)
if err != nil {
t.Errorf("agent update service failed\n")
t.Errorf("agent update service failed: %v\n", err)
}

// UpdateCanary
Expand All @@ -90,13 +115,11 @@ func TestAgentClientSuccess(t *testing.T) {
}
err = agent.UpdateCanary(header, 1)
if err != nil {
t.Errorf("agent update canary failed\n")
t.Errorf("agent update canary failed: %v\n", err)
}

// shutdown
var client = &http.Client{
Timeout: time.Second,
}
var client = &http.Client{Timeout: time.Second}
client.Get("http:https://127.0.0.1:8181/shutdown")
<-finished
}
Expand All @@ -121,7 +144,11 @@ func TestAgentClientFail(t *testing.T) {

// test with 404
finished := make(chan bool)
go httpServer(finished, true)
err = createHTTPServer(finished, true)
if err != nil {
t.Errorf("failed to create HTTP server: %v\n", err)
return
}

err = agent.UpdateService(&service, 1)
if err == nil {
Expand All @@ -131,10 +158,9 @@ func TestAgentClientFail(t *testing.T) {
if err == nil {
t.Errorf("agent should fail\n")
}

// shutdown
var client = &http.Client{
Timeout: time.Second,
}
var client = &http.Client{Timeout: time.Second}
client.Get("http:https://127.0.0.1:8181/shutdown")
<-finished
}

0 comments on commit bf95a21

Please sign in to comment.