-
Notifications
You must be signed in to change notification settings - Fork 10
chore: simplify, modernize (Go 1.26), update deps #147
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e61c4c2
chore: simplify, modernize (Go 1.26), update deps
rustatian 358cb6c
fix(grpc): avoid new package-level globals
rustatian 87679e4
fix(grpc): prevent Stop panic on failed pool init; restore payload reset
rustatian 5aefc4e
test(grpc): cover status/metrics/health server; include root pkg in C…
rustatian e949a0b
test(grpc): simplify descriptor-count assertion in metrics test
rustatian f78cf64
chore: review fixes
rustatian 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
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
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,123 @@ | ||
| package grpc | ||
|
|
||
| import ( | ||
| "context" | ||
| "log/slog" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| "google.golang.org/grpc" | ||
| "google.golang.org/grpc/codes" | ||
| "google.golang.org/grpc/health/grpc_health_v1" | ||
| "google.golang.org/grpc/status" | ||
| ) | ||
|
|
||
| func discardLog() *slog.Logger { return slog.New(slog.DiscardHandler) } | ||
|
|
||
| // fakeWatchStream is a minimal grpc_health_v1.Health_WatchServer: it records the | ||
| // responses Watch sends and exposes a controllable context. Only Send and | ||
| // Context are used by HealthCheckServer.Watch; the embedded grpc.ServerStream | ||
| // satisfies the rest of the interface and is intentionally left nil. | ||
| type fakeWatchStream struct { | ||
| grpc.ServerStream | ||
| ctx context.Context | ||
| sent chan *grpc_health_v1.HealthCheckResponse | ||
| } | ||
|
|
||
| func (f *fakeWatchStream) Context() context.Context { return f.ctx } | ||
|
|
||
| func (f *fakeWatchStream) Send(resp *grpc_health_v1.HealthCheckResponse) error { | ||
| f.sent <- resp | ||
| return nil | ||
| } | ||
|
|
||
| func TestHealthServer_CheckListAndServingStatus(t *testing.T) { | ||
| h := NewHeathServer(nil, discardLog()) | ||
|
|
||
| // A fresh server starts NOT_SERVING. | ||
| resp, err := h.Check(t.Context(), &grpc_health_v1.HealthCheckRequest{}) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, grpc_health_v1.HealthCheckResponse_NOT_SERVING, resp.GetStatus()) | ||
|
|
||
| // List reports the same status under the "grpc" key. | ||
| lst, err := h.List(t.Context(), &grpc_health_v1.HealthListRequest{}) | ||
| require.NoError(t, err) | ||
| require.Contains(t, lst.GetStatuses(), "grpc") | ||
| assert.Equal(t, grpc_health_v1.HealthCheckResponse_NOT_SERVING, lst.GetStatuses()["grpc"].GetStatus()) | ||
|
|
||
| // Flipping to SERVING is reflected by Check. | ||
| h.SetServingStatus(grpc_health_v1.HealthCheckResponse_SERVING) | ||
| resp, err = h.Check(t.Context(), &grpc_health_v1.HealthCheckRequest{}) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, grpc_health_v1.HealthCheckResponse_SERVING, resp.GetStatus()) | ||
| } | ||
|
|
||
| func TestHealthServer_ShutdownIgnoresStatusChange(t *testing.T) { | ||
| h := NewHeathServer(nil, discardLog()) | ||
| h.SetServingStatus(grpc_health_v1.HealthCheckResponse_SERVING) | ||
|
|
||
| h.Shutdown() | ||
|
|
||
| // After Shutdown, further status changes must be ignored. | ||
| h.SetServingStatus(grpc_health_v1.HealthCheckResponse_NOT_SERVING) | ||
|
|
||
| resp, err := h.Check(t.Context(), &grpc_health_v1.HealthCheckRequest{}) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, grpc_health_v1.HealthCheckResponse_SERVING, resp.GetStatus(), | ||
| "status must not change after Shutdown") | ||
| } | ||
|
|
||
| func TestHealthServer_Watch(t *testing.T) { | ||
| h := NewHeathServer(nil, discardLog()) | ||
|
|
||
| ctx, cancel := context.WithCancel(t.Context()) | ||
| defer cancel() | ||
|
|
||
| stream := &fakeWatchStream{ | ||
| ctx: ctx, | ||
| sent: make(chan *grpc_health_v1.HealthCheckResponse, 8), | ||
| } | ||
|
|
||
| watchErr := make(chan error, 1) | ||
| go func() { watchErr <- h.Watch(&grpc_health_v1.HealthCheckRequest{}, stream) }() | ||
|
|
||
| // Watch emits the current status immediately on subscribe. | ||
| assert.Equal(t, grpc_health_v1.HealthCheckResponse_NOT_SERVING, recvStatus(t, stream.sent)) | ||
|
|
||
| // A status change is streamed to the watcher. | ||
| h.SetServingStatus(grpc_health_v1.HealthCheckResponse_SERVING) | ||
| assert.Equal(t, grpc_health_v1.HealthCheckResponse_SERVING, recvStatus(t, stream.sent)) | ||
|
|
||
| // Canceling the stream context ends Watch with a Canceled status. | ||
| cancel() | ||
|
|
||
| select { | ||
| case err := <-watchErr: | ||
| require.Error(t, err) | ||
| assert.Equal(t, codes.Canceled, status.Code(err)) | ||
| case <-time.After(5 * time.Second): | ||
| t.Fatal("Watch did not return after context cancellation") | ||
| } | ||
| } | ||
|
|
||
| func recvStatus(t *testing.T, ch <-chan *grpc_health_v1.HealthCheckResponse) grpc_health_v1.HealthCheckResponse_ServingStatus { | ||
| t.Helper() | ||
| select { | ||
| case resp := <-ch: | ||
| return resp.GetStatus() | ||
| case <-time.After(5 * time.Second): | ||
| t.Fatal("timed out waiting for a health status update") | ||
| return 0 | ||
| } | ||
| } | ||
|
|
||
| func TestHealthServer_RegisterServer(t *testing.T) { | ||
| h := NewHeathServer(nil, discardLog()) | ||
| srv := grpc.NewServer() | ||
| t.Cleanup(srv.Stop) | ||
|
|
||
| // RegisterServer must wire the health service onto the gRPC server. | ||
| require.NotPanics(t, func() { h.RegisterServer(srv) }) | ||
| } |
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,89 @@ | ||
| package grpc | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/prometheus/client_golang/prometheus" | ||
| dto "github.com/prometheus/client_model/go" | ||
| "github.com/roadrunner-server/pool/v2/fsm" | ||
| "github.com/roadrunner-server/pool/v2/state/process" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // fakeInformer returns a fixed set of worker states so the StatsExporter can be | ||
| // exercised without a running worker pool. | ||
| type fakeInformer struct { | ||
| states []*process.State | ||
| } | ||
|
|
||
| func (f *fakeInformer) Workers() []*process.State { return f.states } | ||
|
|
||
| func TestStatsExporter_DescribeAndCollect(t *testing.T) { | ||
| inf := &fakeInformer{states: []*process.State{ | ||
| {Pid: 1, Status: fsm.StateReady, StatusStr: "ready", MemoryUsage: 100}, | ||
| {Pid: 2, Status: fsm.StateWorking, StatusStr: "working", MemoryUsage: 200}, | ||
| {Pid: 3, Status: fsm.StateErrored, StatusStr: "errored", MemoryUsage: 300}, | ||
| }} | ||
|
|
||
| exp := newStatsExporter(inf) | ||
|
|
||
| // Describe must announce every descriptor the collector can emit. | ||
| descCh := make(chan *prometheus.Desc, 16) | ||
| exp.Describe(descCh) | ||
| close(descCh) | ||
|
|
||
| var descs int | ||
| for range descCh { | ||
| descs++ | ||
| } | ||
| assert.Equal(t, 7, descs, "StatsExporter must describe all 7 descriptors") | ||
|
|
||
| // Collect through a registry so the metric families can be asserted by name. | ||
| reg := prometheus.NewRegistry() | ||
| require.NoError(t, reg.Register(exp)) | ||
|
|
||
| mfs, err := reg.Gather() | ||
| require.NoError(t, err) | ||
|
|
||
| byName := make(map[string]*dto.MetricFamily, len(mfs)) | ||
| for _, mf := range mfs { | ||
| byName[mf.GetName()] = mf | ||
| } | ||
|
|
||
| // One worker lands in each branch of Collect's switch: ready / working / | ||
| // everything-else (StateErrored falls through to the invalid bucket). | ||
| assert.Equal(t, float64(1), gaugeValue(t, byName, "rr_grpc_workers_ready")) | ||
| assert.Equal(t, float64(1), gaugeValue(t, byName, "rr_grpc_workers_working")) | ||
| assert.Equal(t, float64(1), gaugeValue(t, byName, "rr_grpc_workers_invalid")) | ||
|
|
||
| // Totals: 3 workers, cumulative RSS = 100 + 200 + 300. | ||
| assert.Equal(t, float64(3), gaugeValue(t, byName, "rr_grpc_total_workers")) | ||
| assert.Equal(t, float64(600), gaugeValue(t, byName, "rr_grpc_workers_memory_bytes")) | ||
|
|
||
| // Per-worker series: one sample per worker for state and memory. | ||
| require.Contains(t, byName, "rr_grpc_worker_state") | ||
| require.Contains(t, byName, "rr_grpc_worker_memory_bytes") | ||
| assert.Len(t, byName["rr_grpc_worker_state"].GetMetric(), 3) | ||
| assert.Len(t, byName["rr_grpc_worker_memory_bytes"].GetMetric(), 3) | ||
| } | ||
|
|
||
| // gaugeValue returns the single gauge sample for a label-less metric family. | ||
| func gaugeValue(t *testing.T, byName map[string]*dto.MetricFamily, name string) float64 { | ||
| t.Helper() | ||
| mf, ok := byName[name] | ||
| require.Truef(t, ok, "metric %q was not collected", name) | ||
| require.Lenf(t, mf.GetMetric(), 1, "metric %q must have a single sample", name) | ||
| return mf.GetMetric()[0].GetGauge().GetValue() | ||
| } | ||
|
|
||
| func TestMetricsCollector(t *testing.T) { | ||
| p := &Plugin{ | ||
| statsExporter: newStatsExporter(&fakeInformer{}), | ||
| queueSize: prometheus.NewGauge(prometheus.GaugeOpts{Name: "q"}), | ||
| requestCounter: prometheus.NewCounterVec(prometheus.CounterOpts{Name: "c"}, []string{"l"}), | ||
| requestDuration: prometheus.NewHistogramVec(prometheus.HistogramOpts{Name: "d"}, []string{"l"}), | ||
| } | ||
|
|
||
| assert.Len(t, p.MetricsCollector(), 4) | ||
| } |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.