test: fix test cases issues

This commit is contained in:
Marvin Zhang
2024-11-22 17:43:59 +08:00
parent 331ddb4118
commit b4862f9a26
13 changed files with 161 additions and 497 deletions

View File

@@ -27,7 +27,6 @@ import (
"github.com/crawlab-team/crawlab/core/interfaces"
"github.com/crawlab-team/crawlab/core/models/client"
"github.com/crawlab-team/crawlab/core/models/service"
"github.com/crawlab-team/crawlab/core/sys_exec"
"github.com/crawlab-team/crawlab/core/utils"
"github.com/crawlab-team/crawlab/grpc"
"github.com/crawlab-team/crawlab/trace"
@@ -180,13 +179,12 @@ func (r *Runner) Cancel(force bool) (err error) {
r.cancel()
// Kill process
err = sys_exec.KillProcess(r.cmd, &sys_exec.KillProcessOptions{
Force: force,
})
err = utils.KillProcess(r.cmd, force)
if err != nil {
log.Errorf("kill process error: %v", err)
return err
}
log.Debugf("attempt to kill process[%d]", r.pid)
// Create a context with timeout
ctx, cancel := context.WithTimeout(context.Background(), r.svc.GetCancelTimeout())
@@ -236,7 +234,7 @@ func (r *Runner) configureCmd() (err error) {
}
// get cmd instance
r.cmd, err = sys_exec.BuildCmd(cmdStr)
r.cmd, err = utils.BuildCmd(cmdStr)
if err != nil {
log.Errorf("error building command: %v", err)
return err
@@ -511,28 +509,33 @@ func (r *Runner) getHttpRequestHeaders() (headers map[string]string) {
func (r *Runner) wait() (err error) {
// start a goroutine to wait for process to finish
go func() {
log.Debugf("waiting for process[%d] to finish", r.pid)
err = r.cmd.Wait()
if err != nil {
var exitError *exec.ExitError
if !errors.As(err, &exitError) {
r.ch <- constants.TaskSignalError
log.Debugf("process[%d] exited with error: %v", r.pid, err)
return
}
exitCode := exitError.ExitCode()
if exitCode == -1 {
// cancel error
r.ch <- constants.TaskSignalCancel
log.Debugf("process[%d] cancelled", r.pid)
return
}
// standard error
r.err = err
r.ch <- constants.TaskSignalError
log.Debugf("process[%d] exited with error: %v", r.pid, err)
return
}
// success
r.ch <- constants.TaskSignalFinish
log.Debugf("process[%d] exited successfully", r.pid)
}()
// declare task status

View File

@@ -2,6 +2,7 @@ package handler
import (
"bufio"
"context"
"encoding/json"
"fmt"
"io"
@@ -46,7 +47,7 @@ func setupTest(t *testing.T) *Runner {
case "windows":
task.Cmd = "ping -n 10 127.0.0.1"
default: // linux and darwin (macOS)
task.Cmd = "sleep 10"
task.Cmd = "ping -c 10 127.0.0.1"
}
taskId, err := service.NewModelService[models.Task]().InsertOne(*task)
require.NoError(t, err)
@@ -146,12 +147,18 @@ func TestRunner_Cancel(t *testing.T) {
}
}()
// Wait a bit longer on Windows for the process to start properly
waitTime := 100 * time.Millisecond
if runtime.GOOS == "windows" {
waitTime = 1 * time.Second
}
time.Sleep(waitTime)
// Wait for process to finish
go func() {
err = runner.cmd.Wait()
if err != nil {
log.Errorf("process[%d] exited with error: %v", runner.pid, err)
return
}
log.Infof("process[%d] exited successfully", runner.pid)
}()
// Wait for a certain period for the process to start properly
time.Sleep(1 * time.Second)
// Verify process exists before attempting to cancel
if !utils.ProcessIdExists(runner.pid) {
@@ -162,17 +169,25 @@ func TestRunner_Cancel(t *testing.T) {
go func() {
err = runner.Cancel(true)
assert.NoError(t, err)
log.Infof("process[%d] cancelled", runner.pid)
}()
// Wait for process to be killed, with shorter timeout
deadline := time.Now().Add(5 * time.Second)
for time.Now().Before(deadline) {
if !utils.ProcessIdExists(runner.pid) {
return // Process was killed
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
ticker := time.NewTicker(100 * time.Millisecond)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
t.Fatalf("Process with PID %d was not killed within timeout", runner.pid)
case <-ticker.C:
exists := utils.ProcessIdExists(runner.pid)
if !exists {
return // Exit the test when process is killed
}
}
time.Sleep(100 * time.Millisecond)
}
t.Errorf("Process with PID %d was not killed within timeout", runner.pid)
}
func TestRunner_HandleIPCData(t *testing.T) {