lens @ master

 1//go:build unit
 2
 3package coroutine
 4
 5import (
 6	"context"
 7	"errors"
 8	"sync"
 9	"testing"
10	"time"
11
12	"git.sr.ht/~gabrielgio/img/pkg/testkit"
13)
14
15var rError = errors.New("This is a error")
16
17func imediatReturn() (string, error) {
18	return "A string", nil
19}
20
21func imediatErrorReturn() (string, error) {
22	return "", rError
23}
24
25func haltedReturn() (string, error) {
26	time.Sleep(time.Hour)
27	return "", nil
28}
29
30func TestImediatReturn(t *testing.T) {
31	ctx := context.Background()
32	v, err := WrapProcess(ctx, imediatReturn)
33	testkit.TestError(t, "WrapProcess", nil, err)
34	testkit.TestValue(t, "WrapProcess", "A string", v)
35}
36
37func TestImediatErrorReturn(t *testing.T) {
38	ctx := context.Background()
39	v, err := WrapProcess(ctx, imediatErrorReturn)
40	testkit.TestError(t, "WrapProcess", rError, err)
41	testkit.TestValue(t, "WrapProcess", "", v)
42}
43
44func TestHaltedReturn(t *testing.T) {
45	ctx := context.Background()
46	ctx, cancel := context.WithCancel(ctx)
47
48	var (
49		err error
50		wg  sync.WaitGroup
51	)
52
53	wg.Add(1)
54	go func(err *error) {
55		defer wg.Done()
56		_, *err = WrapProcess(ctx, haltedReturn)
57	}(&err)
58
59	cancel()
60	wg.Wait()
61
62	testkit.TestError(t, "WrapProcess", context.Canceled, err)
63}