refactor: enhance IPC handling in task runner tests

- Updated IPC reader initialization in runner_test.go to use a channel for signaling readiness, improving synchronization.
- Added error logging when writing to the pipe to enhance traceability during tests.
- These changes improve the reliability and clarity of the test setup for the task runner.
This commit is contained in:
Marvin Zhang
2025-01-03 16:56:36 +08:00
parent ff5cd32de4
commit 37d77f7342

View File

@@ -289,8 +289,17 @@ func TestRunner(t *testing.T) {
defer pw.Close()
runner.stdoutPipe = pr
// Start IPC reader
go runner.startIPCReader()
// Create a channel to signal that the reader is ready
readerReady := make(chan struct{})
// Start IPC reader with ready signal
go func() {
close(readerReady) // Signal that reader is ready
runner.startIPCReader()
}()
// Wait for reader to be ready
<-readerReady
// Test cases
testCases := []struct {
@@ -390,8 +399,17 @@ func TestRunner(t *testing.T) {
defer pw.Close()
runner.stdoutPipe = pr
// Start IPC reader
go runner.startIPCReader()
// Create a channel to signal that the reader is ready
readerReady := make(chan struct{})
// Start IPC reader with ready signal
go func() {
close(readerReady) // Signal that reader is ready
runner.startIPCReader()
}()
// Wait for reader to be ready
<-readerReady
// Test cases for invalid data
testCases := []struct {
@@ -431,7 +449,9 @@ func TestRunner(t *testing.T) {
// Write test message to pipe
go func() {
_, err := fmt.Fprintln(pw, tc.message)
assert.NoError(t, err)
if err != nil {
log.Errorf("failed to write to pipe: %v", err)
}
}()
// Wait briefly to ensure no processing occurs