1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-12 11:15:00 +02:00
lazygit/pkg/tasks/async_handler_test.go
Jesse Duffield 6b9390409e Use an interface for tasks instead of a concrete struct
By using an interface for tasks we can use a fake implementation in tests with extra methods
2023-07-10 17:12:21 +10:00

49 lines
693 B
Go

package tasks
import (
"fmt"
"sync"
"testing"
"github.com/jesseduffield/gocui"
"github.com/stretchr/testify/assert"
)
func TestAsyncHandler(t *testing.T) {
wg := sync.WaitGroup{}
wg.Add(2)
onWorker := func(f func(gocui.Task)) {
go f(gocui.NewFakeTask())
}
handler := NewAsyncHandler(onWorker)
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)
}