Skip to content

Commit

Permalink
Fix test of lock file on Darwin (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
sevlyar committed May 18, 2017
1 parent 8577c7d commit 00b7cba
Showing 1 changed file with 6 additions and 104 deletions.
110 changes: 6 additions & 104 deletions lock_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package daemon

import (
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"testing"
)

Expand Down Expand Up @@ -89,116 +87,20 @@ func TestReadPid(test *testing.T) {
}

func TestLockFileLock(test *testing.T) {
lock, err := OpenLockFile(filename, fileperm)
lock1, err := OpenLockFile(filename, fileperm)
if err != nil {
test.Fatal(err)
}
defer lock.Remove()

if err := lock.Lock(); err != nil {
test.Fatal(err)
}
scr, msg, err := createLockScriptAndStart()
if err != nil {
test.Fatal(err)
}
if msg != "error" {
test.Fatal("script was able lock file")
}
if err = terminateLockScript(scr); err != nil {
if err := lock1.Lock(); err != nil {
test.Fatal(err)
}
defer lock1.Remove()

if err = lock.Unlock(); err != nil {
test.Fatal(err)
}
lock.Close()

scr, msg, err = createLockScriptAndStart()
lock2, err := OpenLockFile(filename, fileperm)
if err != nil {
test.Fatal(err)
}
if msg != "locked" {
test.Fatal("script can not lock file")
}
lock, err = CreatePidFile(filename, fileperm)
if err != ErrWouldBlock {
test.Fatal("Lock() not work properly")
}
if err = terminateLockScript(scr); err != nil {
test.Error(err)
}
}

func createLockScriptAndStart() (scr *script, msg string, err error) {
var text = fmt.Sprintf(`
set -e
exec 222>"%s"
flock -n 222||echo "error"
echo "locked"
read inp`, filename)

scr, err = createScript(text, true)
if err != nil {
return
if err := lock2.Lock(); err != ErrWouldBlock {
test.Fatal("To lock file more than once must be unavailable.")
}

if err = scr.cmd.Start(); err != nil {
return
}
// wait until the script does not try to lock the file
msg, err = scr.get()
return
}

func terminateLockScript(scr *script) (err error) {
if err = scr.send(""); err != nil {
return
}
err = scr.cmd.Wait()
return
}

type script struct {
cmd *exec.Cmd
stdout io.ReadCloser
stdin io.WriteCloser
}

func createScript(text string, createPipes bool) (scr *script, err error) {
var scrName string
if scrName, err = createScriptFile(text); err != nil {
return
}
scr = &script{cmd: exec.Command("bash", scrName)}
if createPipes {
if scr.stdout, err = scr.cmd.StdoutPipe(); err != nil {
return
}
if scr.stdin, err = scr.cmd.StdinPipe(); err != nil {
return
}
}
return
}

func (scr *script) send(line string) (err error) {
_, err = fmt.Fprintln(scr.stdin, line)
return
}

func (scr *script) get() (line string, err error) {
_, err = fmt.Fscanln(scr.stdout, &line)
return
}

func createScriptFile(text string) (name string, err error) {
var scr *os.File
if scr, err = ioutil.TempFile(os.TempDir(), "scr"); err != nil {
return
}
defer scr.Close()
name = scr.Name()
_, err = scr.WriteString(text)
return
}

0 comments on commit 00b7cba

Please sign in to comment.