Skip to content
This repository has been archived by the owner on Jun 27, 2024. It is now read-only.

Commit

Permalink
[#85]: feature: allow using []string command instead of string
Browse files Browse the repository at this point in the history
  • Loading branch information
rustatian authored Aug 7, 2023
2 parents 16ad626 + 8062c2a commit c1d5303
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 54 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"errgroup",
"Failboot",
"goridge",
"mapstructure",
"memleak",
"nolint",
"pexec",
Expand Down
4 changes: 3 additions & 1 deletion fsm/fsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,11 @@ func (s *Fsm) recognizer(to int64) error {
// to
case StateInactive:
// from
// No-one can transition to Inactive
if atomic.LoadInt64(s.currentState) == StateDestroyed {
return errors.E(op, errors.Errorf("can't transition from state: %s", s.String()))
}
// to
// to from StateWorking/StateInactive only
case StateReady:
// from
switch atomic.LoadInt64(s.currentState) {
Expand All @@ -123,6 +124,7 @@ func (s *Fsm) recognizer(to int64) error {
// to
case StateWorking:
// from
// StateWorking can be transitioned only from StateReady
if atomic.LoadInt64(s.currentState) == StateReady {
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion pool/allocator.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Factory interface {
}

// NewPoolAllocator initializes allocator of the workers
func NewPoolAllocator(ctx context.Context, timeout time.Duration, factory Factory, cmd Command, command string, log *zap.Logger) func() (*worker.Process, error) {
func NewPoolAllocator(ctx context.Context, timeout time.Duration, factory Factory, cmd Command, command []string, log *zap.Logger) func() (*worker.Process, error) {
return func() (*worker.Process, error) {
ctxT, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
Expand Down
2 changes: 1 addition & 1 deletion pool/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ import (
)

// Command is a function that returns a new exec.Cmd instance for the given command string.
type Command func(cmd string) *exec.Cmd
type Command func(cmd []string) *exec.Cmd
4 changes: 2 additions & 2 deletions pool/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ type Config struct {
Debug bool

// Command used to override the server command with the custom one
Command string `mapstructure:"command"`
Command []string `mapstructure:"command"`

// AfterInitCommand command used to override the server's AfterInitCommand
AfterInitCommand string `mapstructure:"after_init"`
AfterInitCommand []string `mapstructure:"after_init"`

// NumWorkers defines how many sub-processes can be run at once. This value
// might be doubled by Swapper while hot-swap. Defaults to number of CPU cores.
Expand Down
2 changes: 1 addition & 1 deletion pool/static_pool/fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func FuzzStaticPoolEcho(f *testing.F) {
ctx := context.Background()
p, err := NewPool(
ctx,
func(cmd string) *exec.Cmd { return exec.Command("php", "../../tests/client.php", "echo", "pipes") },
func(cmd []string) *exec.Cmd { return exec.Command("php", "../../tests/client.php", "echo", "pipes") },
pipe.NewPipeFactory(log()),
testCfg,
log(),
Expand Down
34 changes: 17 additions & 17 deletions pool/static_pool/supervisor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func Test_SupervisedPool_Exec(t *testing.T) {
ctx := context.Background()
p, err := NewPool(
ctx,
func(cmd string) *exec.Cmd { return exec.Command("php", "../../tests/memleak.php", "pipes") },
func(cmd []string) *exec.Cmd { return exec.Command("php", "../../tests/memleak.php", "pipes") },
pipe.NewPipeFactory(log()),
cfgSupervised,
log(),
Expand Down Expand Up @@ -66,7 +66,7 @@ func Test_SupervisedPool_ImmediateDestroy(t *testing.T) {

p, err := NewPool(
ctx,
func(cmd string) *exec.Cmd { return exec.Command("php", "../../tests/client.php", "echo", "pipes") },
func(cmd []string) *exec.Cmd { return exec.Command("php", "../../tests/client.php", "echo", "pipes") },
pipe.NewPipeFactory(log()),
&pool.Config{
AllocateTimeout: time.Second * 10,
Expand Down Expand Up @@ -96,7 +96,7 @@ func Test_SupervisedPool_NilFactory(t *testing.T) {
ctx := context.Background()
p, err := NewPool(
ctx,
func(cmd string) *exec.Cmd { return exec.Command("php", "../../tests/client.php", "echo", "pipes") },
func(cmd []string) *exec.Cmd { return exec.Command("php", "../../tests/client.php", "echo", "pipes") },
pipe.NewPipeFactory(log()),
nil,
log(),
Expand All @@ -109,7 +109,7 @@ func Test_SupervisedPool_NilConfig(t *testing.T) {
ctx := context.Background()
p, err := NewPool(
ctx,
func(cmd string) *exec.Cmd { return exec.Command("php", "../../tests/client.php", "echo", "pipes") },
func(cmd []string) *exec.Cmd { return exec.Command("php", "../../tests/client.php", "echo", "pipes") },
nil,
cfgSupervised,
log(),
Expand All @@ -123,7 +123,7 @@ func Test_SupervisedPool_RemoveWorker(t *testing.T) {

p, err := NewPool(
ctx,
func(cmd string) *exec.Cmd { return exec.Command("php", "../../tests/client.php", "echo", "pipes") },
func(cmd []string) *exec.Cmd { return exec.Command("php", "../../tests/client.php", "echo", "pipes") },
pipe.NewPipeFactory(log()),
cfgSupervised,
log(),
Expand Down Expand Up @@ -151,7 +151,7 @@ func Test_SupervisedPoolReset(t *testing.T) {
ctx := context.Background()
p, err := NewPool(
ctx,
func(cmd string) *exec.Cmd { return exec.Command("php", "../../tests/client.php", "echo", "pipes") },
func(cmd []string) *exec.Cmd { return exec.Command("php", "../../tests/client.php", "echo", "pipes") },
pipe.NewPipeFactory(log()),
cfgSupervised,
log(),
Expand Down Expand Up @@ -181,7 +181,7 @@ func TestSupervisedPool_ExecWithDebugMode(t *testing.T) {
ctx := context.Background()
p, err := NewPool(
ctx,
func(cmd string) *exec.Cmd { return exec.Command("php", "../../tests/supervised.php") },
func(cmd []string) *exec.Cmd { return exec.Command("php", "../../tests/supervised.php") },
pipe.NewPipeFactory(log()),
&pool.Config{
Debug: true,
Expand Down Expand Up @@ -232,7 +232,7 @@ func TestSupervisedPool_ExecTTL_TimedOut(t *testing.T) {
ctx := context.Background()
p, err := NewPool(
ctx,
func(cmd string) *exec.Cmd { return exec.Command("php", "../../tests/sleep.php", "pipes") },
func(cmd []string) *exec.Cmd { return exec.Command("php", "../../tests/sleep.php", "pipes") },
pipe.NewPipeFactory(log()),
cfgExecTTL,
log(),
Expand Down Expand Up @@ -266,7 +266,7 @@ func TestSupervisedPool_TTL_WorkerRestarted(t *testing.T) {
ctx := context.Background()
p, err := NewPool(
ctx,
func(cmd string) *exec.Cmd { return exec.Command("php", "../../tests/sleep-ttl.php") },
func(cmd []string) *exec.Cmd { return exec.Command("php", "../../tests/sleep-ttl.php") },
pipe.NewPipeFactory(log()),
cfgExecTTL,
log(),
Expand Down Expand Up @@ -328,7 +328,7 @@ func TestSupervisedPool_Idle(t *testing.T) {
ctx := context.Background()
p, err := NewPool(
ctx,
func(cmd string) *exec.Cmd { return exec.Command("php", "../../tests/idle.php", "pipes") },
func(cmd []string) *exec.Cmd { return exec.Command("php", "../../tests/idle.php", "pipes") },
pipe.NewPipeFactory(log()),
cfgExecTTL,
log(),
Expand Down Expand Up @@ -380,7 +380,7 @@ func TestSupervisedPool_IdleTTL_StateAfterTimeout(t *testing.T) {
ctx := context.Background()
p, err := NewPool(
ctx,
func(cmd string) *exec.Cmd { return exec.Command("php", "../../tests/exec_ttl.php", "pipes") },
func(cmd []string) *exec.Cmd { return exec.Command("php", "../../tests/exec_ttl.php", "pipes") },
pipe.NewPipeFactory(log()),
cfgExecTTL,
log(),
Expand Down Expand Up @@ -432,7 +432,7 @@ func TestSupervisedPool_ExecTTL_OK(t *testing.T) {
ctx := context.Background()
p, err := NewPool(
ctx,
func(cmd string) *exec.Cmd { return exec.Command("php", "../../tests/exec_ttl.php", "pipes") },
func(cmd []string) *exec.Cmd { return exec.Command("php", "../../tests/exec_ttl.php", "pipes") },
pipe.NewPipeFactory(log()),
cfgExecTTL,
log(),
Expand Down Expand Up @@ -478,7 +478,7 @@ func TestSupervisedPool_ShouldRespond(t *testing.T) {
ctx := context.Background()
p, err := NewPool(
ctx,
func(cmd string) *exec.Cmd {
func(cmd []string) *exec.Cmd {
return exec.Command("php", "../../tests/should-not-be-killed.php", "pipes")
},
pipe.NewPipeFactory(log()),
Expand Down Expand Up @@ -524,7 +524,7 @@ func TestSupervisedPool_MaxMemoryReached(t *testing.T) {
ctx := context.Background()
p, err := NewPool(
ctx,
func(cmd string) *exec.Cmd { return exec.Command("php", "../../tests/memleak.php", "pipes") },
func(cmd []string) *exec.Cmd { return exec.Command("php", "../../tests/memleak.php", "pipes") },
pipe.NewPipeFactory(log()),
cfgExecTTL,
log(),
Expand Down Expand Up @@ -552,7 +552,7 @@ func Test_SupervisedPool_FastCancel(t *testing.T) {
ctx := context.Background()
p, err := NewPool(
ctx,
func(cmd string) *exec.Cmd { return exec.Command("php", "../../tests/sleep.php") },
func(cmd []string) *exec.Cmd { return exec.Command("php", "../../tests/sleep.php") },
pipe.NewPipeFactory(log()),
cfgSupervised,
log(),
Expand Down Expand Up @@ -583,7 +583,7 @@ func Test_SupervisedPool_AllocateFailedOK(t *testing.T) {
ctx := context.Background()
p, err := NewPool(
ctx,
func(cmd string) *exec.Cmd { return exec.Command("php", "../../tests/allocate-failed.php") },
func(cmd []string) *exec.Cmd { return exec.Command("php", "../../tests/allocate-failed.php") },
pipe.NewPipeFactory(log()),
cfgExecTTL,
log(),
Expand Down Expand Up @@ -624,7 +624,7 @@ func Test_SupervisedPool_NoFreeWorkers(t *testing.T) {
p, err := NewPool(
ctx,
// sleep for the 3 seconds
func(cmd string) *exec.Cmd { return exec.Command("php", "../../tests/sleep.php", "pipes") },
func(cmd []string) *exec.Cmd { return exec.Command("php", "../../tests/sleep.php", "pipes") },
pipe.NewPipeFactory(log()),
&pool.Config{
Debug: false,
Expand Down
Loading

0 comments on commit c1d5303

Please sign in to comment.