From 00f4281459a9bc8cc59fd0642ccb40317a0b4918 Mon Sep 17 00:00:00 2001 From: castaneai Date: Fri, 22 Nov 2024 10:04:19 +0900 Subject: [PATCH] fix WithTestServerListenAddr does not work --- testing.go | 9 +++++++++ tests/intergration_test.go | 7 +++++++ 2 files changed, 16 insertions(+) diff --git a/testing.go b/testing.go index 6721edb..1ea2ab4 100644 --- a/testing.go +++ b/testing.go @@ -101,6 +101,11 @@ func NewTestFrontendServer(t *testing.T, store statestore.FrontendStore, addr st mux := http.NewServeMux() mux.Handle(openmatchconnect.NewFrontendServiceHandler(NewFrontendService(store, opts...))) sv := httptest.NewUnstartedServer(h2c.NewHandler(mux, &http2.Server{})) + lis, err := newLocalListener(addr) + if err != nil { + t.Fatalf("failed to listen TCP addr: %+v", err) + } + sv.Listener = lis sv.EnableHTTP2 = true ts := &TestFrontendServer{ sv: sv, @@ -109,6 +114,10 @@ func NewTestFrontendServer(t *testing.T, store statestore.FrontendStore, addr st return ts } +func newLocalListener(addr string) (net.Listener, error) { + return net.Listen("tcp", addr) +} + // RunTestServer helps with integration tests using Open Match. // It provides an Open Match Frontend equivalent API in the Go process using a random port. func RunTestServer(t *testing.T, matchFunctions map[*pb.MatchProfile]MatchFunction, assigner Assigner, opts ...TestServerOption) *TestServer { diff --git a/tests/intergration_test.go b/tests/intergration_test.go index e6a7962..7b52c0d 100644 --- a/tests/intergration_test.go +++ b/tests/intergration_test.go @@ -295,6 +295,13 @@ func TestAssignerError(t *testing.T) { assert.Equal(t, as1.Connection, as2.Connection) } +func TestTestServer(t *testing.T) { + addr := "127.0.0.1:34543" + ts := minimatch.RunTestServer(t, map[*pb.MatchProfile]minimatch.MatchFunction{ + anyProfile: minimatch.MatchFunctionSimple1vs1}, minimatch.AssignerFunc(dummyAssign), minimatch.WithTestServerListenAddr(addr)) + assert.Equal(t, addr, ts.FrontendAddr()) +} + func mustCreateTicket(ctx context.Context, t *testing.T, c openmatchconnect.FrontendServiceClient, ticket *pb.Ticket) *pb.Ticket { t.Helper() resp, err := c.CreateTicket(ctx, connect.NewRequest(&pb.CreateTicketRequest{Ticket: ticket}))