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

Merged
Daniel.Sy merged 2 commits from fix-status into main 2025-03-28 02:56:50 +00:00
3 changed files with 16 additions and 4 deletions

View file

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

View file

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

View file

@ -23,6 +23,10 @@ func Start() {
})
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")
concurrency, _ := strconv.Atoi(c.PostForm("concurrency"))
duration, _ := time.ParseDuration(c.PostForm("duration"))
@ -33,6 +37,10 @@ func Start() {
})
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()
c.Redirect(http.StatusSeeOther, "/")
})