2021-10-18 10:00:03 +02:00
|
|
|
package tasks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
2023-07-09 03:32:27 +02:00
|
|
|
"github.com/jesseduffield/gocui"
|
2021-10-18 10:00:03 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAsyncHandler(t *testing.T) {
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
wg.Add(2)
|
|
|
|
|
2023-07-09 13:09:52 +02:00
|
|
|
onWorker := func(f func(gocui.Task)) {
|
|
|
|
go f(gocui.NewFakeTask())
|
2023-07-03 06:16:43 +02:00
|
|
|
}
|
|
|
|
handler := NewAsyncHandler(onWorker)
|
2021-10-18 10:00:03 +02:00
|
|
|
handler.onReject = func() {
|
|
|
|
wg.Done()
|
|
|
|
}
|
|
|
|
|
|
|
|
result := 0
|
|
|
|
|
|
|
|
wg2 := sync.WaitGroup{}
|
|
|
|
wg2.Add(1)
|
|
|
|
|
|
|
|
handler.Do(func() func() {
|
|
|
|
wg2.Wait()
|
|
|
|
return func() {
|
|
|
|
fmt.Println("setting to 1")
|
|
|
|
result = 1
|
|
|
|
}
|
|
|
|
})
|
|
|
|
handler.Do(func() func() {
|
|
|
|
return func() {
|
|
|
|
fmt.Println("setting to 2")
|
|
|
|
result = 2
|
|
|
|
wg.Done()
|
|
|
|
wg2.Done()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
assert.EqualValues(t, 2, result)
|
|
|
|
}
|