-
Notifications
You must be signed in to change notification settings - Fork 15
test: add pool concurrency coverage #92
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,149 @@ | ||
| package pyproc | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "sync" | ||
| "sync/atomic" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/YuminosukeSato/pyproc/internal/protocol" | ||
| ) | ||
|
|
||
| func TestPoolCall_SerializesPerWorker(t *testing.T) { | ||
| paths := []string{ | ||
| tempSocketPath(t, "serial-w0"), | ||
| tempSocketPath(t, "serial-w1"), | ||
| } | ||
| var collisions atomic.Int32 | ||
| for _, path := range paths { | ||
| path := path | ||
| var inflight atomic.Int32 | ||
| stop := startUnixServer(t, path, func(req protocol.Request) *protocol.Response { | ||
| if inflight.Add(1) > 1 { | ||
| collisions.Add(1) | ||
| } | ||
| time.Sleep(120 * time.Millisecond) | ||
| inflight.Add(-1) | ||
| resp, _ := protocol.NewResponse(req.ID, map[string]bool{"ok": true}) | ||
| return resp | ||
| }) | ||
| t.Cleanup(stop) | ||
| } | ||
|
|
||
| workers := []workerHandle{ | ||
| newStubWorker(paths[0], true), | ||
| newStubWorker(paths[1], true), | ||
| } | ||
| p := newPoolWithWorkers(PoolConfig{ | ||
| Workers: 2, | ||
| MaxInFlight: 4, | ||
| MaxInFlightPerWorker: 1, | ||
| HealthInterval: 10 * time.Millisecond, | ||
| }, workers) | ||
| for _, pw := range p.workers { | ||
| pw.healthy.Store(true) | ||
| } | ||
|
|
||
| var wg sync.WaitGroup | ||
| for i := 0; i < 4; i++ { | ||
| wg.Add(1) | ||
| go func() { | ||
| defer wg.Done() | ||
| ctx, cancel := context.WithTimeout(context.Background(), time.Second) | ||
| defer cancel() | ||
| if err := p.Call(ctx, "echo", map[string]string{"msg": "x"}, &map[string]any{}); err != nil { | ||
| t.Errorf("call failed: %v", err) | ||
| } | ||
| }() | ||
| } | ||
| wg.Wait() | ||
|
|
||
| if collisions.Load() != 0 { | ||
| t.Fatalf("expected no concurrent requests per worker, got %d", collisions.Load()) | ||
| } | ||
| } | ||
|
|
||
| func TestPoolCall_OversubscribeBlocksWithContext(t *testing.T) { | ||
| path := tempSocketPath(t, "oversub") | ||
| firstStarted := make(chan struct{}) | ||
| var reqCount atomic.Int32 | ||
| stop := startUnixServer(t, path, func(req protocol.Request) *protocol.Response { | ||
| if reqCount.Add(1) == 1 { | ||
| close(firstStarted) | ||
| time.Sleep(200 * time.Millisecond) | ||
| } | ||
| resp, _ := protocol.NewResponse(req.ID, map[string]bool{"ok": true}) | ||
| return resp | ||
| }) | ||
| t.Cleanup(stop) | ||
|
|
||
| workers := []workerHandle{newStubWorker(path, true)} | ||
| p := newPoolWithWorkers(PoolConfig{ | ||
| Workers: 1, | ||
| MaxInFlight: 2, | ||
| MaxInFlightPerWorker: 1, | ||
| }, workers) | ||
| p.workers[0].healthy.Store(true) | ||
|
|
||
| firstErrCh := make(chan error, 1) | ||
| go func() { | ||
| ctx, cancel := context.WithTimeout(context.Background(), time.Second) | ||
| defer cancel() | ||
| firstErrCh <- p.Call(ctx, "echo", map[string]string{"msg": "first"}, &map[string]any{}) | ||
| }() | ||
|
|
||
| <-firstStarted | ||
|
|
||
| ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond) | ||
| defer cancel() | ||
| err := p.Call(ctx, "echo", map[string]string{"msg": "second"}, &map[string]any{}) | ||
| if err == nil { | ||
| t.Fatal("expected deadline exceeded for oversubscribed call") | ||
| } | ||
| if !errors.Is(err, context.DeadlineExceeded) { | ||
| t.Fatalf("expected deadline exceeded, got %v", err) | ||
| } | ||
|
|
||
| if firstErr := <-firstErrCh; firstErr != nil { | ||
| t.Fatalf("first call failed: %v", firstErr) | ||
| } | ||
| } | ||
|
|
||
| func TestPoolCall_ShutdownConcurrent(t *testing.T) { | ||
| path := tempSocketPath(t, "shutdown") | ||
| started := make(chan struct{}) | ||
| stop := startUnixServer(t, path, func(req protocol.Request) *protocol.Response { | ||
| close(started) | ||
| time.Sleep(150 * time.Millisecond) | ||
| resp, _ := protocol.NewResponse(req.ID, map[string]bool{"ok": true}) | ||
| return resp | ||
| }) | ||
| t.Cleanup(stop) | ||
|
|
||
| workers := []workerHandle{newStubWorker(path, true)} | ||
| p := newPoolWithWorkers(PoolConfig{ | ||
| Workers: 1, | ||
| MaxInFlight: 1, | ||
| MaxInFlightPerWorker: 1, | ||
| }, workers) | ||
| p.workers[0].healthy.Store(true) | ||
|
|
||
| callErrCh := make(chan error, 1) | ||
| go func() { | ||
| ctx, cancel := context.WithTimeout(context.Background(), time.Second) | ||
| defer cancel() | ||
| callErrCh <- p.Call(ctx, "echo", map[string]string{"msg": "x"}, &map[string]any{}) | ||
| }() | ||
|
|
||
| <-started | ||
|
|
||
| if err := p.Shutdown(context.Background()); err != nil { | ||
| t.Fatalf("shutdown failed: %v", err) | ||
| } | ||
|
|
||
| if callErr := <-callErrCh; callErr != nil { | ||
| t.Fatalf("call failed during shutdown: %v", callErr) | ||
| } | ||
| } | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These new tests call
startUnixServerdirectly, which hard-fails onnet.Listen("unix", ...)when Unix domain sockets are unavailable (the exact environment thatrequireUnixSocketis meant to skip inmain_test.go). Because none of the new test functions invoke that guard, they can make CI fail in restricted or non-UDS environments instead of being skipped.Useful? React with 👍 / 👎.