Thanks to visit codestin.com
Credit goes to github.com

Skip to content

srfake: add NewRegistry and Handler to serve on a custom listener#1345

Merged
twmb merged 1 commit into
masterfrom
srfake-expose-handler
Jun 15, 2026
Merged

srfake: add NewRegistry and Handler to serve on a custom listener#1345
twmb merged 1 commit into
masterfrom
srfake-expose-handler

Conversation

@twmb

@twmb twmb commented Jun 15, 2026

Copy link
Copy Markdown
Owner

srfake.New wraps an httptest.Server on a random port. That's ideal for unit
tests, but it means you can't serve the mock registry on a port you choose
(e.g. a fixed :8081 alongside a fake Kafka cluster).

This adds a small, backward-compatible way to bring your own listener:

  • NewRegistry(opts ...Option) *Registry — builds a Registry without starting
    any server.
  • (*Registry) Handler() http.Handler — returns the registry's HTTP handler so
    callers can serve it themselves.

New is unchanged and now builds on NewRegistry internally. For a
NewRegistry-created registry, URL() returns "" and Close() is a no-op;
all in-memory methods (SeedSchema, RegisterSchema, etc.) behave as before.

reg := srfake.NewRegistry()
ln, _ := net.Listen("tcp", "127.0.0.1:8081")
go http.Serve(ln, reg.Handler())
// ... reg.SeedSchema(...) etc. still work

Motivation: this lets a tool serve an in-process schema registry on a
configurable, fixed port in the same process as a kfake cluster (so e.g.
kcl fake can stand up brokers + registry together).

New() wraps an httptest.Server on a random port, which is ideal for unit
tests but prevents serving the registry on a chosen port. Add NewRegistry,
which builds the Registry without starting a server, and Handler, which
returns its http.Handler. Callers can serve the mock on their own
net.Listener (e.g. a fixed port). URL returns "" and Close is a no-op for
NewRegistry-created registries; all in-memory methods work as before.
@twmb twmb force-pushed the srfake-expose-handler branch from 0ab18b1 to 49d016f Compare June 15, 2026 00:21
@twmb twmb changed the title srfake: add NewHandler and Handler to serve on a custom listener srfake: add NewRegistry and Handler to serve on a custom listener Jun 15, 2026
@twmb twmb merged commit 36f3548 into master Jun 15, 2026
12 checks passed
@twmb twmb deleted the srfake-expose-handler branch June 15, 2026 00:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant