-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfuture_test.go
49 lines (40 loc) · 854 Bytes
/
future_test.go
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
49
/*
© 2023–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
ISC License
*/
package parl
import (
"sync"
"testing"
)
func TestAwaitableCalculation(t *testing.T) {
value := 1
var calculation *Future[int]
var result int
var isValid bool
calculation = NewFuture[int]()
var wgStart sync.WaitGroup
wgStart.Add(1)
var wgEnd sync.WaitGroup
wgEnd.Add(1)
go func() {
defer wgEnd.Done()
wgStart.Done()
result, isValid = calculation.Result()
}()
wgStart.Wait()
if calculation.IsCompleted() {
t.Error("calculation.IsCompleted true")
}
calculation.End(&value, nil, nil)
if !calculation.IsCompleted() {
t.Error("calculation.IsCompleted false")
}
wgEnd.Wait()
if !isValid {
t.Error("isValid false")
}
if result != value {
t.Errorf("result bad: %d exp %d", result, value)
}
}