Skip to content

Commit

Permalink
Add shutdown order test
Browse files Browse the repository at this point in the history
  • Loading branch information
F1bonacc1 authored and anmonteiro committed Mar 2, 2024
1 parent 1cc0938 commit 8941afb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
14 changes: 6 additions & 8 deletions fixtures-code/process-compose-shutdown-inorder.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,24 @@ log_length: 1000
processes:
procA:
command: |
trap 'echo "A: exit"' SIGTERM
echo "A: starting"
sleep 3
procB:
command: |
trap 'echo "B: exit"' SIGTERM
echo "B: starting"
is_daemon: true
shutdown:
command: |
echo "B: exiting"
sleep 3
depends_on:
procA:
condition: process_started

procC:
command: |
trap 'echo "C: exit"' SIGTERM
echo "C: starting"
isDaemon: true
shutdown:
command: |
echo "C: exiting"
sleep 3
depends_on:
procB:
condition: process_started
30 changes: 29 additions & 1 deletion src/app/system_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package app

import (
"bufio"
"github.com/f1bonacc1/process-compose/src/loader"
"github.com/f1bonacc1/process-compose/src/types"
"os"
"path/filepath"
"reflect"
"slices"
"strings"
"testing"
"time"
)
Expand Down Expand Up @@ -410,6 +414,7 @@ func TestSystem_TestProcListToRun(t *testing.T) {
func TestSystem_TestProcListShutsDownInOrder(t *testing.T) {
fixture1 := filepath.Join("..", "..", "fixtures-code", "process-compose-shutdown-inorder.yaml")
t.Run("Single Proc with deps", func(t *testing.T) {

project, err := loader.Load(&loader.LoaderOptions{
FileNames: []string{fixture1},
})
Expand All @@ -436,7 +441,16 @@ func TestSystem_TestProcListShutsDownInOrder(t *testing.T) {
t.Errorf("process %s is disabled", name)
}
}

file, err := os.CreateTemp("/tmp", "pc_log.*.log")
defer os.Remove(file.Name())
project.LogLocation = file.Name()
project.LoggerConfig = &types.LoggerConfig{
FieldsOrder: []string{"message"},
DisableJSON: true,
TimestampFormat: "",
NoMetadata: true,
FlushEachLine: true,
}
go runner.Run()
time.Sleep(10 * time.Millisecond)
states, err := runner.GetProcessesState()
Expand Down Expand Up @@ -470,5 +484,19 @@ func TestSystem_TestProcListShutsDownInOrder(t *testing.T) {
if runningProcesses != want {
t.Errorf("len(runner.runningProcesses) = %d, want %d", len(runner.runningProcesses), want)
}
//read file and validate the shutdown order
scanner := bufio.NewScanner(file)
order := make([]string, 0)
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, "exit") {
order = append(order, line)
}
}
wantOrder := []string{"C: exit", "B: exit", "A: exit"}
if !slices.Equal(wantOrder, order) {
t.Errorf("content = %v, want %v", order, wantOrder)
return
}
})
}

0 comments on commit 8941afb

Please sign in to comment.