summaryrefslogtreecommitdiffstats
path: root/pkg/tasks/async_handler_test.go
blob: 6da363cd964f9c52cbf4153d666a0a6e3ee6c964 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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) error) {
		go func() { _ = 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)
}