2020-12-17 01:06:47 +00:00
|
|
|
package process
|
2020-12-17 00:53:44 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-11-24 20:56:54 +00:00
|
|
|
"errors"
|
2020-12-17 00:53:44 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/mock"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestWaitOrKillNilProcess(t *testing.T) {
|
|
|
|
// Process not started
|
|
|
|
mockCmd := &mockExecCmd{}
|
|
|
|
defer mock.AssertExpectationsForObjects(t, mockCmd)
|
|
|
|
mockCmd.On("OsProcess").Return(nil)
|
|
|
|
|
|
|
|
p := newWithMock(mockCmd)
|
2021-01-14 17:40:33 +00:00
|
|
|
err := p.WaitOrKill(context.Background(), 1*time.Second)
|
2020-12-17 00:53:44 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
assert.Contains(t, err.Error(), "non-nil OsProcess")
|
|
|
|
}
|
|
|
|
|
2021-01-14 17:40:33 +00:00
|
|
|
func TestWaitOrKillProcessCompleted(t *testing.T) {
|
2020-12-17 00:53:44 +00:00
|
|
|
// Process already completed
|
|
|
|
mockCmd := &mockExecCmd{}
|
|
|
|
mockProcess := &mockOsProcess{}
|
|
|
|
defer mock.AssertExpectationsForObjects(t, mockCmd, mockProcess)
|
|
|
|
mockCmd.On("OsProcess").Return(mockProcess)
|
|
|
|
mockCmd.On("Wait").Return(nil)
|
|
|
|
|
|
|
|
p := newWithMock(mockCmd)
|
2021-01-14 17:40:33 +00:00
|
|
|
err := p.WaitOrKill(context.Background(), 10*time.Millisecond)
|
2020-12-17 00:53:44 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2021-01-14 17:40:33 +00:00
|
|
|
func TestWaitOrKillProcessCompletedError(t *testing.T) {
|
2020-12-17 00:53:44 +00:00
|
|
|
// Process already completed with error
|
|
|
|
mockCmd := &mockExecCmd{}
|
|
|
|
mockProcess := &mockOsProcess{}
|
|
|
|
defer mock.AssertExpectationsForObjects(t, mockCmd, mockProcess)
|
|
|
|
mockCmd.On("OsProcess").Return(mockProcess)
|
2021-11-24 20:56:54 +00:00
|
|
|
mockCmd.On("Wait").After(10 * time.Millisecond).Return(errors.New("super bad"))
|
2020-12-17 00:53:44 +00:00
|
|
|
|
|
|
|
p := newWithMock(mockCmd)
|
2021-01-14 17:40:33 +00:00
|
|
|
err := p.WaitOrKill(context.Background(), 10*time.Millisecond)
|
2020-12-17 00:53:44 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
assert.Contains(t, err.Error(), "super bad")
|
|
|
|
}
|
|
|
|
|
2021-01-14 17:40:33 +00:00
|
|
|
func TestWaitOrKillWait(t *testing.T) {
|
2020-12-17 00:53:44 +00:00
|
|
|
// Process completes after the wait call and after the signal is sent
|
|
|
|
mockCmd := &mockExecCmd{}
|
|
|
|
mockProcess := &mockOsProcess{}
|
|
|
|
defer mock.AssertExpectationsForObjects(t, mockCmd, mockProcess)
|
|
|
|
mockCmd.On("OsProcess").Return(mockProcess)
|
|
|
|
mockCmd.On("Wait").After(5 * time.Millisecond).Return(nil)
|
2020-12-17 00:59:58 +00:00
|
|
|
mockProcess.On("Signal", stopSignal()).Return(nil)
|
2020-12-17 00:53:44 +00:00
|
|
|
|
|
|
|
p := newWithMock(mockCmd)
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
cancel()
|
2021-01-14 17:40:33 +00:00
|
|
|
err := p.WaitOrKill(ctx, 10*time.Millisecond)
|
2020-12-17 00:53:44 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
assert.Contains(t, err.Error(), "context canceled")
|
|
|
|
}
|
|
|
|
|
2021-01-14 17:40:33 +00:00
|
|
|
func TestWaitOrKillWaitSignalCompleted(t *testing.T) {
|
2020-12-17 00:53:44 +00:00
|
|
|
// Process completes after the wait call and before the signal is sent
|
|
|
|
mockCmd := &mockExecCmd{}
|
|
|
|
mockProcess := &mockOsProcess{}
|
|
|
|
defer mock.AssertExpectationsForObjects(t, mockCmd, mockProcess)
|
|
|
|
mockCmd.On("OsProcess").Return(mockProcess)
|
|
|
|
mockCmd.On("Wait").After(10 * time.Millisecond).Return(nil)
|
2021-11-24 20:56:54 +00:00
|
|
|
mockProcess.On("Signal", stopSignal()).Return(errors.New("os: process already finished"))
|
2020-12-17 00:53:44 +00:00
|
|
|
|
|
|
|
p := newWithMock(mockCmd)
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
cancel()
|
2021-01-14 17:40:33 +00:00
|
|
|
err := p.WaitOrKill(ctx, 5*time.Millisecond)
|
2020-12-17 00:53:44 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2021-01-14 17:40:33 +00:00
|
|
|
func TestWaitOrKillWaitKilled(t *testing.T) {
|
2020-12-17 00:53:44 +00:00
|
|
|
// Process is killed after the wait call and signal
|
|
|
|
mockCmd := &mockExecCmd{}
|
|
|
|
mockProcess := &mockOsProcess{}
|
|
|
|
defer mock.AssertExpectationsForObjects(t, mockCmd, mockProcess)
|
|
|
|
mockCmd.On("OsProcess").Return(mockProcess)
|
2021-11-24 20:56:54 +00:00
|
|
|
mockCmd.On("Wait").After(10 * time.Millisecond).Return(errors.New("killed"))
|
2020-12-17 00:59:58 +00:00
|
|
|
mockProcess.On("Signal", stopSignal()).Return(nil)
|
2020-12-17 00:53:44 +00:00
|
|
|
mockProcess.On("Kill").Return(nil)
|
|
|
|
|
|
|
|
p := newWithMock(mockCmd)
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
cancel()
|
2021-01-14 17:40:33 +00:00
|
|
|
err := p.WaitOrKill(ctx, 5*time.Millisecond)
|
2020-12-17 00:53:44 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
assert.Contains(t, err.Error(), "context canceled")
|
|
|
|
}
|