Skip to content

Commit e287628

Browse files
author
fupeng1
committed
fix: testing error
1 parent 8eed43e commit e287628

File tree

3 files changed

+45
-16
lines changed

3 files changed

+45
-16
lines changed

promise.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,19 @@ func (p *Promise[T]) Finally(onFinally func()) *Promise[T] {
272272
}
273273

274274
func (p *Promise[T]) Await() (T, error) {
275+
// Check if already completed without locking
276+
state := p.getState()
277+
if state == Fulfilled {
278+
value, _ := p.getValue()
279+
return value, nil
280+
}
281+
if state == Rejected {
282+
var zero T
283+
err, _ := p.getError()
284+
return zero, err
285+
}
286+
287+
// Still pending, wait for completion
275288
p.mu.Lock()
276289
if p.done == nil {
277290
p.done = make(chan struct{})
@@ -281,6 +294,7 @@ func (p *Promise[T]) Await() (T, error) {
281294

282295
<-done
283296

297+
// Check final state
284298
if p.getState() == Fulfilled {
285299
value, _ := p.getValue()
286300
return value, nil
@@ -292,6 +306,19 @@ func (p *Promise[T]) Await() (T, error) {
292306

293307
// AwaitWithContext waits for Promise completion with context
294308
func (p *Promise[T]) AwaitWithContext(ctx context.Context) (T, error) {
309+
// Check if already completed without locking
310+
state := p.getState()
311+
if state == Fulfilled {
312+
value, _ := p.getValue()
313+
return value, nil
314+
}
315+
if state == Rejected {
316+
var zero T
317+
err, _ := p.getError()
318+
return zero, err
319+
}
320+
321+
// Still pending, wait for completion or context cancellation
295322
p.mu.Lock()
296323
if p.done == nil {
297324
p.done = make(chan struct{})
@@ -306,6 +333,7 @@ func (p *Promise[T]) AwaitWithContext(ctx context.Context) (T, error) {
306333
case <-done:
307334
}
308335

336+
// Check final state
309337
if p.getState() == Fulfilled {
310338
value, _ := p.getValue()
311339
return value, nil

promise_test.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ import (
1212

1313
// TestMain provides setup and teardown for the entire test suite
1414
func TestMain(m *testing.M) {
15-
mgr := GetDefaultMgr()
16-
1715
// Run tests
1816
code := m.Run()
1917

20-
mgr.Close()
18+
// Cleanup: close the current default manager and wait for shutdown
19+
currentMgr := GetDefaultMgr()
20+
currentMgr.Close()
21+
currentMgr.WaitForShutdown()
2122

2223
if code != 0 {
2324
panic(fmt.Sprintf("Tests failed with code %d", code))
@@ -1086,14 +1087,17 @@ func TestWithResolvers(t *testing.T) {
10861087
t.Run("Multiple resolves should only take first", func(t *testing.T) {
10871088
promise, resolve, _ := WithResolvers[string]()
10881089

1090+
// Use a channel to ensure the first resolve happens before the second
1091+
firstDone := make(chan struct{})
1092+
10891093
// Multiple resolves
10901094
go func() {
1091-
time.Sleep(5 * time.Millisecond)
10921095
resolve("first")
1096+
close(firstDone)
10931097
}()
10941098
go func() {
1095-
time.Sleep(10 * time.Millisecond)
1096-
resolve("second")
1099+
<-firstDone // Wait for first resolve to complete
1100+
resolve("second") // This should be ignored
10971101
}()
10981102

10991103
result, err := promise.Await()
@@ -1108,14 +1112,17 @@ func TestWithResolvers(t *testing.T) {
11081112
t.Run("Multiple rejects should only take first", func(t *testing.T) {
11091113
promise, _, reject := WithResolvers[string]()
11101114

1115+
// Use a channel to ensure the first reject happens before the second
1116+
firstDone := make(chan struct{})
1117+
11111118
// Multiple rejects
11121119
go func() {
1113-
time.Sleep(5 * time.Millisecond)
11141120
reject(errors.New("first reject"))
1121+
close(firstDone)
11151122
}()
11161123
go func() {
1117-
time.Sleep(10 * time.Millisecond)
1118-
reject(errors.New("second reject"))
1124+
<-firstDone // Wait for first reject to complete
1125+
reject(errors.New("second reject")) // This should be ignored
11191126
}()
11201127

11211128
_, err := promise.Await()

types.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,7 @@ func (p *Promise[T]) setState(state State) bool {
118118
}
119119

120120
// Now try to transition from Pending to target state
121-
if p.state.CompareAndSwap(Pending, state) {
122-
return true
123-
}
124-
125-
// If transition failed, check current state
126-
currentState := p.state.Load().(State)
127-
return currentState == state // Return true if already in target state
121+
return p.state.CompareAndSwap(Pending, state)
128122
}
129123

130124
// Helper function: safely get value

0 commit comments

Comments
 (0)