Skip to content

Commit 2442804

Browse files
committed
{channel,server}_test: add tests for message limits.
Adjust unit test to accomodate for altered internal interfaces. Add unit tests to exercise the new message size limit options. Signed-off-by: Krisztian Litkey <[email protected]>
1 parent 0753662 commit 2442804

File tree

2 files changed

+135
-35
lines changed

2 files changed

+135
-35
lines changed

channel_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ import (
3131
func TestReadWriteMessage(t *testing.T) {
3232
var (
3333
w, r = net.Pipe()
34-
ch = newChannel(w)
35-
rch = newChannel(r)
34+
ch = newChannel(w, 0)
35+
rch = newChannel(r, 0)
3636
messages = [][]byte{
3737
[]byte("hello"),
3838
[]byte("this is a test"),
@@ -90,7 +90,7 @@ func TestReadWriteMessage(t *testing.T) {
9090
func TestMessageOversize(t *testing.T) {
9191
var (
9292
w, r = net.Pipe()
93-
wch, rch = newChannel(w), newChannel(r)
93+
wch, rch = newChannel(w, 0), newChannel(r, 0)
9494
msg = bytes.Repeat([]byte("a message of massive length"), 512<<10)
9595
errs = make(chan error, 1)
9696
)

server_test.go

Lines changed: 132 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package ttrpc
1919
import (
2020
"bytes"
2121
"context"
22+
"crypto/md5"
2223
"errors"
2324
"fmt"
2425
"net"
@@ -61,10 +62,17 @@ func (tc *testingClient) Test(ctx context.Context, req *internal.TestPayload) (*
6162
}
6263

6364
// testingServer is what would be implemented by the user of this package.
64-
type testingServer struct{}
65+
type testingServer struct {
66+
echoOnce bool
67+
}
6568

6669
func (s *testingServer) Test(ctx context.Context, req *internal.TestPayload) (*internal.TestPayload, error) {
67-
tp := &internal.TestPayload{Foo: strings.Repeat(req.Foo, 2)}
70+
tp := &internal.TestPayload{}
71+
if s.echoOnce {
72+
tp.Foo = req.Foo
73+
} else {
74+
tp.Foo = strings.Repeat(req.Foo, 2)
75+
}
6876
if dl, ok := ctx.Deadline(); ok {
6977
tp.Deadline = dl.UnixNano()
7078
}
@@ -299,37 +307,122 @@ func TestServerClose(t *testing.T) {
299307
}
300308

301309
func TestOversizeCall(t *testing.T) {
302-
var (
303-
ctx = context.Background()
304-
server = mustServer(t)(NewServer())
305-
addr, listener = newTestListener(t)
306-
errs = make(chan error, 1)
307-
client, cleanup = newTestClient(t, addr)
308-
)
309-
defer cleanup()
310-
defer listener.Close()
311-
go func() {
312-
errs <- server.Serve(ctx, listener)
313-
}()
314-
315-
registerTestingService(server, &testingServer{})
310+
type testCase struct {
311+
name string
312+
echoOnce bool
313+
clientLimit int
314+
serverLimit int
315+
requestSize int
316+
shouldFail bool
317+
}
318+
319+
runTest := func(t *testing.T, tc *testCase) {
320+
var (
321+
ctx = context.Background()
322+
server = mustServer(t)(NewServer(WithServerWireMessageLimit(tc.serverLimit)))
323+
addr, listener = newTestListener(t)
324+
errs = make(chan error, 1)
325+
client, cleanup = newTestClient(t, addr, WithClientWireMessageLimit(tc.clientLimit))
326+
)
327+
defer cleanup()
328+
defer listener.Close()
329+
go func() {
330+
errs <- server.Serve(ctx, listener)
331+
}()
332+
333+
registerTestingService(server, &testingServer{echoOnce: tc.echoOnce})
334+
335+
req := &internal.TestPayload{
336+
Foo: strings.Repeat("a", tc.requestSize),
337+
}
338+
rsp := &internal.TestPayload{}
339+
340+
err := client.Call(ctx, serviceName, "Test", req, rsp)
341+
if tc.shouldFail {
342+
if err == nil {
343+
t.Fatalf("expected error from oversized message")
344+
} else if status, ok := status.FromError(err); !ok {
345+
t.Fatalf("expected status present in error: %v", err)
346+
} else if status.Code() != codes.ResourceExhausted {
347+
t.Fatalf("expected code: %v != %v", status.Code(), codes.ResourceExhausted)
348+
}
349+
} else {
350+
if err != nil {
351+
t.Fatalf("expected success, got error %v", err)
352+
}
353+
}
316354

317-
tp := &internal.TestPayload{
318-
Foo: strings.Repeat("a", 1+messageLengthMax),
319-
}
320-
if err := client.Call(ctx, serviceName, "Test", tp, tp); err == nil {
321-
t.Fatalf("expected error from oversized message")
322-
} else if status, ok := status.FromError(err); !ok {
323-
t.Fatalf("expected status present in error: %v", err)
324-
} else if status.Code() != codes.ResourceExhausted {
325-
t.Fatalf("expected code: %v != %v", status.Code(), codes.ResourceExhausted)
355+
if err := server.Shutdown(ctx); err != nil {
356+
t.Fatal(err)
357+
}
358+
if err := <-errs; err != ErrServerClosed {
359+
t.Fatal(err)
360+
}
326361
}
327362

328-
if err := server.Shutdown(ctx); err != nil {
329-
t.Fatal(err)
330-
}
331-
if err := <-errs; err != ErrServerClosed {
332-
t.Fatal(err)
363+
// in principle min. marshalled Request{} + messageheaderLength == 29 would be enough
364+
overhead := 32
365+
366+
for _, tc := range []*testCase{
367+
{
368+
name: "default limits, fitting request and response",
369+
echoOnce: true,
370+
clientLimit: 0,
371+
serverLimit: 0,
372+
requestSize: messageLengthMax - overhead,
373+
shouldFail: false,
374+
},
375+
{
376+
name: "default limits, oversized request",
377+
echoOnce: true,
378+
clientLimit: 0,
379+
serverLimit: 0,
380+
requestSize: messageLengthMax,
381+
shouldFail: true,
382+
},
383+
{
384+
name: "default limits, oversized response",
385+
clientLimit: 0,
386+
serverLimit: 0,
387+
requestSize: messageLengthMax / 2,
388+
shouldFail: true,
389+
},
390+
{
391+
name: "8K limits, fitting 4K request and response",
392+
echoOnce: true,
393+
clientLimit: 8 * 1024,
394+
serverLimit: 8 * 1024,
395+
requestSize: 4 * 1024,
396+
shouldFail: false,
397+
},
398+
{
399+
name: "8K limits, fitting cc. 4K request and response",
400+
echoOnce: true,
401+
clientLimit: 4 * 1024,
402+
serverLimit: 4 * 1024,
403+
requestSize: 4*1024 - overhead,
404+
shouldFail: false,
405+
},
406+
{
407+
name: "4K limits, non-fitting 4K response",
408+
echoOnce: true,
409+
clientLimit: 4 * 1024,
410+
serverLimit: 4 * 1024,
411+
requestSize: 4 * 1024,
412+
shouldFail: true,
413+
},
414+
{
415+
name: "too small limits, adjusted to minimum accepted limit",
416+
echoOnce: true,
417+
clientLimit: 4,
418+
serverLimit: 4,
419+
requestSize: 4*1024 - overhead,
420+
shouldFail: false,
421+
},
422+
} {
423+
t.Run(tc.name, func(t *testing.T) {
424+
runTest(t, tc)
425+
})
333426
}
334427
}
335428

@@ -551,13 +644,20 @@ func newTestClient(t testing.TB, addr string, opts ...ClientOpts) (*Client, func
551644
}
552645

553646
func newTestListener(t testing.TB) (string, net.Listener) {
554-
var prefix string
647+
var (
648+
name = t.Name()
649+
prefix string
650+
)
555651

556652
// Abstracts sockets are only available on Linux.
557653
if runtime.GOOS == "linux" {
558654
prefix = "\x00"
655+
} else {
656+
if split := strings.SplitN(name, "/", 2); len(split) == 2 {
657+
name = split[0] + "-" + fmt.Sprintf("%x", md5.Sum([]byte(split[1])))
658+
}
559659
}
560-
addr := prefix + t.Name()
660+
addr := prefix + name
561661
listener, err := net.Listen("unix", addr)
562662
if err != nil {
563663
t.Fatal(err)

0 commit comments

Comments
 (0)