github.com/clysto/awgo@v0.15.0/background_test.go (about) 1 // 2 // Copyright (c) 2016 Dean Jackson <deanishe@deanishe.net> 3 // 4 // MIT Licence. See http://opensource.org/licenses/MIT 5 // 6 // Created on 2016-11-08 7 // 8 9 package aw 10 11 import ( 12 "os/exec" 13 "testing" 14 15 "github.com/deanishe/awgo/util" 16 ) 17 18 // TestRunInBackground ensures background jobs work. 19 func TestRunInBackground(t *testing.T) { 20 21 wf := New() 22 23 cmd := exec.Command("sleep", "5") 24 if wf.IsRunning("sleep") { 25 t.Fatalf("Job 'sleep' is already running") 26 } 27 if err := wf.RunInBackground("sleep", cmd); err != nil { 28 t.Fatalf("Error starting job 'sleep': %s", err) 29 } 30 if !wf.IsRunning("sleep") { 31 t.Fatalf("Job 'sleep' is not running") 32 } 33 p := wf.pidFile("sleep") 34 if !util.PathExists(p) { 35 t.Fatalf("No PID file for 'sleep'") 36 } 37 // Duplicate jobs fail 38 cmd = exec.Command("sleep", "5") 39 err := wf.RunInBackground("sleep", cmd) 40 if err == nil { 41 t.Fatal("Starting duplicate 'sleep' job didn't error") 42 } 43 if _, ok := err.(ErrJobExists); !ok { 44 t.Fatal("RunInBackground didn't return ErrAlreadyRunning") 45 } 46 if !IsJobExists(err) { 47 t.Errorf("IsAlreadyRunning didn't identify ErrAlreadyRunning") 48 } 49 // Job killed OK 50 if err := wf.Kill("sleep"); err != nil { 51 t.Fatalf("Error killing 'sleep' job: %s", err) 52 } 53 if wf.IsRunning("sleep") { 54 t.Fatal("'sleep' job still running") 55 } 56 if util.PathExists(p) { 57 t.Fatal("'sleep' PID file not deleted") 58 } 59 }