-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
853796f
commit f2b1849
Showing
4 changed files
with
139 additions
and
10 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,34 @@ | ||
package async | ||
|
||
import ( | ||
"context" | ||
"github.com/brickingsoft/rxp" | ||
) | ||
|
||
type unlimitedSubmitter struct { | ||
exec rxp.Executors | ||
} | ||
|
||
func (submitter *unlimitedSubmitter) Submit(task rxp.Task) (ok bool) { | ||
err := submitter.exec.UnlimitedExecute(task) | ||
ok = err == nil | ||
return | ||
} | ||
|
||
// UnlimitedPromise | ||
// 不受 rxp.Executors 最大协程限制的许诺 | ||
func UnlimitedPromise[R any](ctx context.Context) (promise Promise[R]) { | ||
exec := rxp.From(ctx) | ||
submitter := &unlimitedSubmitter{exec: exec} | ||
promise = newPromise[R](ctx, submitter) | ||
return | ||
} | ||
|
||
// UnlimitedStreamPromise | ||
// 不受 rxp.Executors 最大协程限制的流式许诺 | ||
func UnlimitedStreamPromise[R any](ctx context.Context, buf int) (promise Promise[R]) { | ||
exec := rxp.From(ctx) | ||
submitter := &unlimitedSubmitter{exec: exec} | ||
promise = newStreamPromise[R](ctx, submitter, buf) | ||
return | ||
} |
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,51 @@ | ||
package async_test | ||
|
||
import ( | ||
"context" | ||
"github.com/brickingsoft/rxp/async" | ||
"testing" | ||
) | ||
|
||
func TestUnlimitedPromise(t *testing.T) { | ||
ctx, closer := prepare() | ||
defer func() { | ||
err := closer() | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
}() | ||
promise := async.UnlimitedPromise[int](ctx) | ||
promise.Succeed(1) | ||
future := promise.Future() | ||
future.OnComplete(func(ctx context.Context, result int, err error) { | ||
t.Log("future entry:", result, err) | ||
}) | ||
} | ||
|
||
func TestUnlimitedStreamPromise(t *testing.T) { | ||
ctx, closer := prepare() | ||
defer func() { | ||
err := closer() | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
}() | ||
promise := async.UnlimitedStreamPromise[*Closer](ctx, 8) | ||
|
||
future := promise.Future() | ||
future.OnComplete(func(ctx context.Context, result *Closer, err error) { | ||
t.Log("future entry:", result, err) | ||
if err != nil { | ||
t.Log("is closed:", async.IsCanceled(err)) | ||
return | ||
} | ||
return | ||
}) | ||
for i := 0; i < 10; i++ { | ||
promise.Succeed(&Closer{N: i, t: t}) | ||
} | ||
promise.Cancel() | ||
for i := 0; i < 10; i++ { | ||
promise.Succeed(&Closer{N: i, t: t}) | ||
} | ||
} |
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