-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add async.Once implementation (#11)
- Loading branch information
Showing
3 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package async | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
) | ||
|
||
// Once is an object that will execute the given function exactly once. | ||
// Any subsequent call will return the previous result. | ||
type Once[T any] struct { | ||
runOnce sync.Once | ||
result T | ||
err error | ||
} | ||
|
||
// Do calls the function f if and only if Do is being called for the | ||
// first time for this instance of Once. In other words, given | ||
// | ||
// var once Once | ||
// | ||
// if once.Do(f) is called multiple times, only the first call will invoke f, | ||
// even if f has a different value in each invocation. A new instance of | ||
// Once is required for each function to execute. | ||
// | ||
// The return values for each subsequent call will be the result of the | ||
// first execution. | ||
// | ||
// If f panics, Do considers it to have returned; future calls of Do return | ||
// without calling f | ||
func (o *Once[T]) Do(f func() (T, error)) (T, error) { | ||
o.runOnce.Do(func() { | ||
defer func() { | ||
if err := recover(); err != nil { | ||
o.err = fmt.Errorf("recovered %v", err) | ||
} | ||
}() | ||
o.result, o.err = f() | ||
}) | ||
return o.result, o.err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package async | ||
|
||
import ( | ||
"sync" | ||
"sync/atomic" | ||
"testing" | ||
|
||
"github.com/reugn/async/internal" | ||
) | ||
|
||
func TestOnce(t *testing.T) { | ||
var once Once[int32] | ||
var count int32 | ||
|
||
for i := 0; i < 10; i++ { | ||
count, _ = once.Do(func() (int32, error) { | ||
count++ | ||
return count, nil | ||
}) | ||
} | ||
internal.AssertEqual(t, count, 1) | ||
} | ||
|
||
func TestOnceConcurrent(t *testing.T) { | ||
var once Once[int32] | ||
var count int32 | ||
var wg sync.WaitGroup | ||
|
||
for i := 0; i < 10; i++ { | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
result, _ := once.Do(func() (int32, error) { | ||
newCount := atomic.AddInt32(&count, 1) | ||
return newCount, nil | ||
}) | ||
atomic.StoreInt32(&count, result) | ||
}() | ||
} | ||
wg.Wait() | ||
internal.AssertEqual(t, count, 1) | ||
} | ||
|
||
func TestOncePanic(t *testing.T) { | ||
var once Once[int32] | ||
var count int32 | ||
var err error | ||
|
||
for i := 0; i < 10; i++ { | ||
count, err = once.Do(func() (int32, error) { | ||
count /= count | ||
return count, nil | ||
}) | ||
} | ||
internal.AssertEqual(t, err.Error(), "recovered runtime error: integer divide by zero") | ||
internal.AssertEqual(t, count, 0) | ||
} |