fix(#1): 🚑 Fix application stopping early (#4)

Co-authored-by: Daniel Sy <Daniel.Sy@t-systems.com>
Reviewed-on: #4
This commit is contained in:
Daniel.Sy 2025-03-28 02:56:49 +00:00
parent e151b4a6e1
commit 483484e2e2
3 changed files with 16 additions and 4 deletions

View file

@ -7,8 +7,8 @@ import (
var webCmd = &cobra.Command{ var webCmd = &cobra.Command{
Use: "web", Use: "web",
Short: "Startet den Web-Server", Short: "starts the web server",
Long: "Startet den Web-Server für myapp", Long: "starts the web UI for LOIC",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
web.Start() web.Start()
}, },

View file

@ -16,6 +16,7 @@ var requestsSent metric.Int64Counter
var currentTest *Test var currentTest *Test
var isRunning bool var isRunning bool
var TestDone = make(chan struct{}) var TestDone = make(chan struct{})
var closeOnce sync.Once
func init() { func init() {
var err error var err error
@ -47,7 +48,7 @@ func StartTest(targetURL string, concurrency int, duration time.Duration, rampUp
} }
log.Printf("Starting test with target: %s, concurrency: %d, duration: %v, rampUp: %v", targetURL, concurrency, duration, rampUp) log.Printf("Starting test with target: %s, concurrency: %d, duration: %v, rampUp: %v", targetURL, concurrency, duration, rampUp)
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
currentTest := &Test{ currentTest = &Test{
TargetURL: targetURL, TargetURL: targetURL,
Concurrency: concurrency, Concurrency: concurrency,
Duration: duration, Duration: duration,
@ -56,14 +57,16 @@ func StartTest(targetURL string, concurrency int, duration time.Duration, rampUp
cancel: cancel, cancel: cancel,
} }
isRunning = true isRunning = true
closeOnce = sync.Once{} // Reset closeOnce for the new test
log.Println("isRunning set to true, launching goroutine") log.Println("isRunning set to true, launching goroutine")
go func() { go func() {
currentTest.run() currentTest.run()
close(TestDone) closeOnce.Do(func() { close(TestDone) })
}() }()
} }
func StopTest() { func StopTest() {
log.Printf("StopTest called: currentTest=%v, isRunning=%v", currentTest, isRunning)
if currentTest != nil && isRunning { if currentTest != nil && isRunning {
log.Println("Stopping test") log.Println("Stopping test")
currentTest.cancel() currentTest.cancel()
@ -71,6 +74,7 @@ func StopTest() {
currentTest = nil currentTest = nil
isRunning = false isRunning = false
log.Println("Test stopped, isRunning set to false") log.Println("Test stopped, isRunning set to false")
closeOnce.Do(func() { close(TestDone) })
} else { } else {
log.Println("No test running to stop") log.Println("No test running to stop")
} }

View file

@ -23,6 +23,10 @@ func Start() {
}) })
r.POST("/start", func(c *gin.Context) { r.POST("/start", func(c *gin.Context) {
if c.Request.Method != http.MethodPost {
http.Error(c.Writer, "Invalid request method", http.StatusMethodNotAllowed)
return
}
targetURL := c.PostForm("target_url") targetURL := c.PostForm("target_url")
concurrency, _ := strconv.Atoi(c.PostForm("concurrency")) concurrency, _ := strconv.Atoi(c.PostForm("concurrency"))
duration, _ := time.ParseDuration(c.PostForm("duration")) duration, _ := time.ParseDuration(c.PostForm("duration"))
@ -33,6 +37,10 @@ func Start() {
}) })
r.POST("/stop", func(c *gin.Context) { r.POST("/stop", func(c *gin.Context) {
if c.Request.Method != http.MethodPost {
http.Error(c.Writer, "Invalid request method", http.StatusMethodNotAllowed)
return
}
loic.StopTest() loic.StopTest()
c.Redirect(http.StatusSeeOther, "/") c.Redirect(http.StatusSeeOther, "/")
}) })