Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
wtetsu committed Apr 29, 2023
1 parent 857f259 commit 07d8af9
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 32 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ jobs:
run: go test -v -coverprofile=coverage.txt -covermode=atomic ./pkg/...
- name: Codecov
uses: codecov/codecov-action@v3
- name: End to end test
run: make e2e
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ build-linux:
GOOS=linux GOARCH=amd64 go build -ldflags "-s -w" -o ${OUT}/linux/$(BINARY_NAME) -v ${CMD}
ut:
go test github.com/wtetsu/gaze/pkg/...
e2e:
go build -ldflags "-s -w" -o test/e2e -v ${CMD}
cd test/e2e && ./test_all.sh

cov:
go test -coverprofile=coverage.txt -covermode=atomic github.com/wtetsu/gaze/pkg/...
clean:
Expand Down
63 changes: 36 additions & 27 deletions pkg/gazer/gazer.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,51 +69,59 @@ func (g *Gazer) Run(configs *config.Config, timeout int64, restart bool) error {
return err
}

// repeatRunAndWait continuously monitors file system events.
// - Executes corresponding commands based on provided configuration
// - Handles process restarts and timeouts if needed
// - Gracefully shuts down upon receiving a SIGINT signal
func (g *Gazer) repeatRunAndWait(commandConfigs *config.Config, timeout int64, restart bool) error {
sigInt := sigIntChannel()

isDisposed := false
isTerminated := false
for {
select {
case event := <-g.notify.Events:
if isDisposed {
if isTerminated {
break
}
logger.Debug("Receive: %s", event.Name)
g.handleEvent(commandConfigs, timeout, restart, event)

commandStringList := g.tryToFindCommand(event.Name, commandConfigs)
if commandStringList == nil {
continue
}
case <-sigInt:
isTerminated = true
return nil
}
}
}

queueManageKey := strings.Join(commandStringList, "\n")
// handleEvent processes the received file system event.
func (g *Gazer) handleEvent(commandConfigs *config.Config, timeout int64, restart bool, event notify.Event) {
commandStringList := g.tryToFindCommand(event.Name, commandConfigs)
if commandStringList == nil {
return
}

ongoingCommand := g.commands.get(queueManageKey)
queueManageKey := strings.Join(commandStringList, "\n")

if ongoingCommand != nil && restart {
kill(ongoingCommand.cmd, "Restart")
g.commands.update(queueManageKey, nil)
}
ongoingCommand := g.commands.get(queueManageKey)

if ongoingCommand != nil && !restart {
g.commands.enqueue(queueManageKey, event)
continue
}
if ongoingCommand != nil && restart {
kill(ongoingCommand.cmd, "Restart")
g.commands.update(queueManageKey, nil)
}

mutex := g.lock(queueManageKey)
if ongoingCommand != nil && !restart {
g.commands.enqueue(queueManageKey, event)
return
}

g.invokeCount++
mutex := g.lock(queueManageKey)

go func() {
g.invoke(commandStringList, queueManageKey, timeout)
mutex.Unlock()
}()
g.invokeCount++

case <-sigInt:
isDisposed = true
return nil
}
}
go func() {
g.invoke(commandStringList, queueManageKey, timeout)
mutex.Unlock()
}()
}

func (g *Gazer) tryToFindCommand(filePath string, commandConfigs *config.Config) []string {
Expand Down Expand Up @@ -146,6 +154,7 @@ func (g *Gazer) lock(queueManageKey string) *sync.Mutex {
return mutex
}

// invoke executes commands, handles timeouts, and processes queued events.
func (g *Gazer) invoke(commandStringList []string, queueManageKey string, timeout int64) {
lastLaunched := time.Now()

Expand Down
4 changes: 2 additions & 2 deletions test/e2e/test01.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ filedir=$dir/files
cd $dir
rm -f test.*.log

timeout -sKILL 3 ${gaze} -v files/*.* | tee test.log &
timeout -sKILL 5 ${gaze} -v files/*.* | tee test.log &

sleep 0.2
sleep 1.0
echo >> $filedir/hello.rb
sleep 0.2
echo >> $filedir/hello.go
Expand Down
3 changes: 2 additions & 1 deletion test/e2e/test02.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ filedir=$dir/files
cd $dir
rm -f test.*.log

timeout -sKILL 3 ${gaze} -v -c "ruby {{file}} 1" -r files/*.* | tee test.log &
timeout -sKILL 5 ${gaze} -v -c "ruby {{file}} 1" -r files/*.* | tee test.log &

sleep 1.0
echo >> $filedir/hello.rb
sleep 0.2
echo >> $filedir/hello.rb
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/test03.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ cp $filedir/hello.py "$filedir/he&llo.py"
cp $filedir/hello.py "$filedir/he llo.py"
cp $filedir/hello.py "$filedir/he(llo.py"

timeout -sKILL 3 ${gaze} -v files/*.* | tee test.log &
timeout -sKILL 5 ${gaze} -v files/*.* | tee test.log &

sleep 0.2
sleep 1.0
echo >> "$filedir/he'llo.py"
sleep 0.2
echo >> "$filedir/he&llo.py"
Expand Down

0 comments on commit 07d8af9

Please sign in to comment.