Atlas › Test

TestGetIfReady_Sequential

Exact test identity: go.temporal.io/server/common/future/TestFutureSuite/TestGetIfReady_Sequential

Package
go.temporal.io/server/common/future
Suite / test hierarchy
TestFutureSuite/TestGetIfReady_Sequential
Test
TestGetIfReady_Sequential
Introduced at
TestGetIfReady_Sequential Frontier kind: Test frontier
Covered ranges
7
Covered lines
31
Covered files
1

Covered source

Expand a file to inspect source; the > gutter marks covered lines.

go.temporal.io/server/common/future/future_impl.go 31 covered LOC · 7 ranges

Open complete file

29 )
30
31 > func NewFuture[T any]() *FutureImpl[T] { future_impl.go
32 > var value T
33 > return &FutureImpl[T]{
34 > status: pending,
35 > readyCh: make(chan struct{}),
36 >
37 > value: value,
38 > err: nil,
39 > }
40 > }
41
42 func (f *FutureImpl[T]) Get(
56 }
57
58 > func (f *FutureImpl[T]) GetIfReady() (T, error) { future_impl.go
59 > if f.Ready() {
60 > return f.value, f.err future_impl.go
61 > }
62 > var value T future_impl.go
63 > return value, errorFutureNotReady
64 }
65
67 value T,
68 err error,
69 > ) { future_impl.go
70 > // cannot directly set status to `ready`, to prevent data race in case multiple `Get` occurs
71 > // instead set status to `setting` to prevent concurrent completion of this future
72 > if !atomic.CompareAndSwapInt32(
73 > &f.status,
74 > pending,
75 > setting,
76 > ) {
77 panic("future has already been completed")
78 }
79
80 > f.value = value future_impl.go
81 > f.err = err
82 > atomic.CompareAndSwapInt32(&f.status, setting, ready)
83 > close(f.readyCh)
84 }
85
104 }
105
106 > func (f *FutureImpl[T]) Ready() bool { future_impl.go
107 > return atomic.LoadInt32(&f.status) == ready
108 > }