Skip to content
This repository has been archived by the owner on Dec 28, 2020. It is now read-only.

Commit

Permalink
Removing Killable.
Browse files Browse the repository at this point in the history
  • Loading branch information
Burcu Dogan committed Feb 4, 2014
1 parent c769157 commit 095866b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 58 deletions.
14 changes: 0 additions & 14 deletions README.md
Expand Up @@ -60,20 +60,6 @@ done := coop.Timeout(time.Second, func() {
<-done // will return false, because timeout occurred
~~~

### coop.Killable(fn)
Runs fn and returns a signal to enable job cancellation. Once a message is sent to the cancellation channel, Killable returns immediately before waiting for fn to be completed.

~~~ go
kill, done := coop.Killable(func() {
time.Sleep(time.Hour)
fmt.Println("Hello world")
})
coop.After(time.Minute, func() {
kill <- true // sends a kill message after a minute
})
<-done // It will receive false after the cancellation
~~~

### coop.All(fns...)
Runs the list of fns concurrently, returns a boolean channel that will receive a message after all of the fns are completed. The following example will start 4 printing jobs concurrently and wait until all of them are completed.

Expand Down
34 changes: 11 additions & 23 deletions coop.go
Expand Up @@ -43,15 +43,15 @@ func untilRecv(ch chan bool, t time.Time, dur time.Duration, fn func()) {
})
return
}
ch <- true
doneSig(ch, true)
}

// Runs fn after duration. Similar to time.AfterFunc
func After(duration time.Duration, fn func()) (done <-chan bool) {
ch := make(chan bool, 1)
time.AfterFunc(duration, func() {
fn()
ch <- true
doneSig(ch, true)
})
return ch
}
Expand All @@ -72,32 +72,15 @@ func Timeout(duration time.Duration, fn func()) (done <-chan bool) {
ch := make(chan bool, 2)
go func() {
<-time.After(duration)
ch <- false
doneSig(ch, false)
}()
go func() {
fn()
ch <- true
doneSig(ch, true)
}()
return ch
}

// Starts a job and returns a channel for cancellation signal.
// Once a message is sent to the channel, stops the fn.
func Killable(fn func()) (kill chan<- bool, done <-chan bool) {
ch := make(chan bool, 2)
dch := make(chan bool, 1)
go func() {
<-dch
ch <- false
}()
go func() {
fn()
ch <- true
dch <- true
}()
return dch, ch
}

// Starts to run the given list of fns concurrently.
func All(fns ...func()) (done <-chan bool) {
var wg sync.WaitGroup
Expand All @@ -112,7 +95,7 @@ func All(fns ...func()) (done <-chan bool) {
}
go func() {
wg.Wait()
ch <- true
doneSig(ch, true)
}()
return ch
}
Expand All @@ -131,7 +114,7 @@ func AllWithThrottle(throttle int, fns ...func()) (done <-chan bool) {
fns = fns[num:]
<-All(next...)
if len(fns) == 0 {
ch <- true
doneSig(ch, true)
break
}
}
Expand All @@ -147,3 +130,8 @@ func Replicate(n int, fn func()) (done <-chan bool) {
}
return All(funcs...)
}

func doneSig(ch chan bool, val bool) {
ch <- val
close(ch)
}
21 changes: 0 additions & 21 deletions coop_test.go
Expand Up @@ -104,27 +104,6 @@ func TestTimeout_Completed(test *testing.T) {
}
}

func TestKillable_Killed(test *testing.T) {
kill, done := Killable(func() {
time.Sleep(time.Minute)
})
time.AfterFunc(50*time.Millisecond, func() {
kill <- true
})
if <-done {
test.Errorf("Expected to be dead, but it didn't")
}
}

func TestKillable_Completed(test *testing.T) {
_, done := Killable(func() {
time.Sleep(100 * time.Millisecond)
})
if !<-done {
test.Errorf("Expected to be completed, but it didn't")
}
}

func TestAll(test *testing.T) {
start := time.Now()
var val1, val2, val3 bool
Expand Down

0 comments on commit 095866b

Please sign in to comment.