Skip to content

Commit

Permalink
g
Browse files Browse the repository at this point in the history
  • Loading branch information
komuw committed Oct 18, 2024
1 parent 3d1522b commit 4520180
Showing 1 changed file with 76 additions and 47 deletions.
123 changes: 76 additions & 47 deletions internal/mx/mx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,52 +408,7 @@ func TestMux(t *testing.T) {
})
}

func getManyRoutes(b *testing.B) []Route {
b.Helper()

routes := []Route{}

for i := 0; i <= 200; i++ {
uri := fmt.Sprintf("uri-%d", i)
rt, err := NewRoute(
uri,
MethodAll,
someMuxHandler(uri),
)
attest.Ok(b, err)
routes = append(
routes,
rt,
)
}

return routes
}

var result Muxer //nolint:gochecknoglobals

func BenchmarkMuxNew(b *testing.B) {
var r Muxer

l := log.New(context.Background(), &bytes.Buffer{}, 500)

b.ReportAllocs()
b.ResetTimer()
for range b.N {
mux, err := New(
config.WithOpts("localhost", 443, tst.SecretKey(), config.DirectIpStrategy, l),
nil,
getManyRoutes(b)...,
)
attest.Ok(b, err)
r = mux
}
// always store the result to a package level variable
// so the compiler cannot eliminate the Benchmark itself.
result = r
}

func TestMuxKomu(t *testing.T) {
func TestMuxFlexiblePattern(t *testing.T) {
t.Parallel()

tr := &http.Transport{
Expand All @@ -467,7 +422,7 @@ func TestMuxKomu(t *testing.T) {

l := log.New(context.Background(), &bytes.Buffer{}, 500)

t.Run("todo", func(t *testing.T) {
t.Run("flexible pattern accepts all uris", func(t *testing.T) {
t.Parallel()

msg := "hello world"
Expand Down Expand Up @@ -510,4 +465,78 @@ func TestMuxKomu(t *testing.T) {
attest.Equal(t, string(rb), msg)
}
})

t.Run("conflict", func(t *testing.T) {
t.Parallel()

msg := "hello world"

rt1, err := NewRoute(
"/*",
MethodGet,
someMuxHandler(msg),
)
attest.Ok(t, err)

rt2, err := NewRoute(
"/hi",
MethodGet,
thisIsAnotherMuxHandler(),
)
attest.Ok(t, err)

_, err = New(
config.WithOpts(domain, httpsPort, tst.SecretKey(), config.DirectIpStrategy, l),
nil,
rt1,
rt2,
)
attest.Error(t, err)
attest.Subsequence(t, err.Error(), "would conflict")
})
}

func getManyRoutes(b *testing.B) []Route {
b.Helper()

routes := []Route{}

for i := 0; i <= 200; i++ {
uri := fmt.Sprintf("uri-%d", i)
rt, err := NewRoute(
uri,
MethodAll,
someMuxHandler(uri),
)
attest.Ok(b, err)
routes = append(
routes,
rt,
)
}

return routes
}

var result Muxer //nolint:gochecknoglobals

func BenchmarkMuxNew(b *testing.B) {
var r Muxer

l := log.New(context.Background(), &bytes.Buffer{}, 500)

b.ReportAllocs()
b.ResetTimer()
for range b.N {
mux, err := New(
config.WithOpts("localhost", 443, tst.SecretKey(), config.DirectIpStrategy, l),
nil,
getManyRoutes(b)...,
)
attest.Ok(b, err)
r = mux
}
// always store the result to a package level variable
// so the compiler cannot eliminate the Benchmark itself.
result = r
}

0 comments on commit 4520180

Please sign in to comment.