Skip to content

Commit

Permalink
feat: add Done to test state
Browse files Browse the repository at this point in the history
  • Loading branch information
jizhuozhi committed Aug 3, 2024
1 parent 4153934 commit c5df41b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
8 changes: 8 additions & 0 deletions future.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ func (p *Promise[T]) Future() *Future[T] {
return &Future[T]{state: &p.state}
}

func (p *Promise[T]) Done() bool {
return isDone(atomic.LoadUint64(&p.state.state))
}

func (f *Future[T]) Get() (T, error) {
return f.state.get()
}
Expand All @@ -158,6 +162,10 @@ func (f *Future[T]) Subscribe(cb func(val T, err error)) {
f.state.subscribe(cb)
}

func (f *Future[T]) Done() bool {
return isDone(atomic.LoadUint64(&f.state.state))
}

func isFree(st uint64) bool {
return ((st & maskState) >> 32) == stateFree
}
Expand Down
11 changes: 11 additions & 0 deletions future_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,17 @@ func TestFutureSubscribe(t *testing.T) {
assert.Equal(t, val2, 3)
}

func TestPromiseFutureDone(t *testing.T) {
p := NewPromise[int]()
f := p.Future()
assert.False(t, p.Done())
assert.False(t, f.Done())

p.Set(1, nil)
assert.True(t, p.Done())
assert.True(t, f.Done())
}

func Benchmark(b *testing.B) {
b.Run("Promise", func(b *testing.B) {
for i := 0; i < b.N; i++ {
Expand Down

0 comments on commit c5df41b

Please sign in to comment.