Skip to content

Commit

Permalink
Added '-race' tests
Browse files Browse the repository at this point in the history
Fixed race 'issues' using a mutext. Looking at the benchmark the
mutex is somewhat faster, using channels feel more golang though.
This can be changed in the future.
  • Loading branch information
Tobias Laving committed Jul 20, 2020
1 parent 5419143 commit 0daafc4
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 92 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: help test
.PHONY: help test build arm format init_hooks
.DEFAULT_GOAL := help

help:
Expand All @@ -11,10 +11,10 @@ arm: ## Cross compile for arm
@GOARCH=arm GOOS=linux go build -o script-runner.arm cmd/script-runner/main.go

test: ## Run tests
@go test ./...
@go test -race ./...

format: ##Run go fmt
format: ## Run go fmt
@go fmt ./...

init_hooks: ##Will setup githooks for this git repository
init_hooks: ## Will setup githooks for this git repository
@ln -sf $$(pwd)/githooks/* $$(pwd)/.git/hooks
2 changes: 1 addition & 1 deletion githooks/pre-push
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ function log {
echo "Pre-push: $@"
}
log "Testing"
go test ./...
make test
6 changes: 6 additions & 0 deletions internal/runner/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package runner

import (
"os/exec"
"sync"
"time"
)

Expand All @@ -12,6 +13,7 @@ type RCmd struct {
err error
StartTime time.Time
EndTime time.Time
sync.RWMutex
}

func (c *RCmd) Wait() (string, error) {
Expand All @@ -22,6 +24,8 @@ func (c *RCmd) Wait() (string, error) {
}

func (c *RCmd) CheckDone() bool {
c.RLock()
defer c.RUnlock()
return c.done
}

Expand All @@ -30,8 +34,10 @@ func (c *RCmd) Collect() (string, error) {
}

func (c *RCmd) start() {
c.Lock()
c.StartTime = time.Now()
c.output, c.err = c.cmd.CombinedOutput()
c.EndTime = time.Now()
c.done = true
c.Unlock()
}
2 changes: 2 additions & 0 deletions internal/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"path"
"path/filepath"
"strings"
"sync"
"time"
)

Expand Down Expand Up @@ -67,6 +68,7 @@ func (r *Runner) RunAsync(file string, args []string) (*RCmd, error) {
nil,
time.Time{},
time.Time{},
sync.RWMutex{},
}

go c.start()
Expand Down
40 changes: 20 additions & 20 deletions internal/runner/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,32 @@ func TestRunner(t *testing.T) {
if assert.Nil(t, err) {
assert.Equal(t, "lol\n", res)
}
/*
rCmd, err := r.RunAsync("sleep_echo_lol.sh", []string{})
if assert.Nil(t, err) {
assert.False(t, rCmd.CheckDone())
}

rCmd2, err := r.RunAsync("sleep_echo_lol.sh", []string{})
assert.Nil(t, err)
rCmd, err := r.RunAsync("sleep_echo_lol.sh", []string{})
if assert.Nil(t, err) {
assert.False(t, rCmd.CheckDone())
}

res, err = r.Run("sleep_echo_lol.sh", []string{})
if assert.Nil(t, err) {
assert.Equal(t, "lol\n", res)
}
rCmd2, err := r.RunAsync("sleep_echo_lol.sh", []string{})
assert.Nil(t, err)

res, err = rCmd2.Wait()
res, err = r.Run("sleep_echo_lol.sh", []string{})
if assert.Nil(t, err) {
assert.Equal(t, "lol\n", res)
}

res, err = rCmd2.Wait()
if assert.Nil(t, err) {
assert.Equal(t, "lol\n", res)
}

//Should always be true, since we wait on the same command above
if assert.True(t, rCmd.CheckDone()) {
res, err = rCmd.Collect()
if assert.Nil(t, err) {
assert.Equal(t, "lol\n", res)
}
//Should always be true, since we wait on the same command above
if assert.True(t, rCmd.CheckDone()) {
res, err = rCmd.Collect()
if assert.Nil(t, err) {
assert.Equal(t, "lol\n", res)
}
} */
}

res, err = r.Run("does_not_exist.sh", []string{})
assert.NotNil(t, err)
Expand Down
67 changes: 0 additions & 67 deletions test/benchmarks/find_files.go

This file was deleted.

63 changes: 63 additions & 0 deletions test/benchmarks/find_files_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,72 @@
package benchmarks

import (
"os"
"path/filepath"
"strings"
"testing"
)

func walk(dirS string) ([]string, error) {
var files []string
err := filepath.Walk(dirS, func(path string, info os.FileInfo, err error) error {
if info != nil && !info.IsDir() {
files = append(files, strings.ReplaceAll(path, dirS, "")[1:])
}
return nil
})
if err != nil {
return nil, err
}
return files, nil
}

func homeBaked(dirS string) ([]string, error) {
dir, err := os.Open(dirS)
if err != nil {
return nil, err
}
files, err := recurseGetFilenames(dir)
if err != nil {
return nil, err
}
err = dir.Close()
if err != nil {
return nil, err
}
return files, nil
}

func recurseGetFilenames(dir *os.File) ([]string, error) {
var files []string
list, err := dir.Readdir(-1)
if err != nil {
return nil, err
}
for _, file := range list {
if file.IsDir() {
nestedDir, err := os.Open(dir.Name() + "/" + file.Name())
if err != nil {
return nil, err
}
nestedFiles, err := recurseGetFilenames(nestedDir)
if err != nil {
return nil, err
}
err = nestedDir.Close()
if err != nil {
return nil, err
}
for _, nestedFile := range nestedFiles {
files = append(files, file.Name()+"/"+nestedFile)
}
} else {
files = append(files, file.Name())
}
}
return files, nil
}

func BenchmarkHomeBaked(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = homeBaked("test/")
Expand Down
70 changes: 70 additions & 0 deletions test/benchmarks/runner_rcmd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package benchmarks

import (
"os/exec"
"sync"
"testing"
)

type wCmdChan struct {
c *exec.Cmd
err error
done chan bool
}

func (w *wCmdChan) start() {
go func() {
w.err = w.c.Run()
w.done <- true
}()
}

func (w *wCmdChan) check() bool {
return <-w.done
}

type wCmdMutext struct {
c *exec.Cmd
err error
done bool
sync.RWMutex
}

func (w *wCmdMutext) start() {
w.Lock()
w.err = w.c.Run()
w.done = true
w.Unlock()
}

func (w *wCmdMutext) check() bool {
w.RLock()
defer w.RUnlock()
return w.done
}

func BenchmarkChan(b *testing.B) {
for i := 0; i < b.N; i++ {
cmd := exec.Command("ls", "-la")
w := wCmdChan{
c: cmd,
done: make(chan bool),
}
w.start()
for w.check() != true {
}
}
}

func BenchmarkMutex(b *testing.B) {
for i := 0; i < b.N; i++ {
cmd := exec.Command("ls", "-la")
w := wCmdMutext{
c: cmd,
done: false,
}
w.start()
for w.check() != true {
}
}
}

0 comments on commit 0daafc4

Please sign in to comment.