Atlas › Test

TestSetGetReady_Parallel

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

Package
go.temporal.io/server/common/future
Suite / test hierarchy
TestFutureSuite/TestSetGetReady_Parallel
Test
TestSetGetReady_Parallel
Introduced at
TestSetGetReady_Parallel Frontier kind: Test frontier
Covered ranges
8
Covered lines
32
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 32 covered LOC · 8 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(
43 ctx context.Context,
44 > ) (T, error) { future_impl.go
45 > if f.Ready() {
46 > return f.value, f.err future_impl.go
47 > }
48
49 > select { future_impl.go
50 > case <-f.readyCh: future_impl.go
51 > return f.value, f.err
52 case <-ctx.Done():
53 var value T
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 > }