Skip to content

Commit a4b18e0

Browse files
author
Kazuyoshi Kato
committed
Make "go test" work on macOS
Abstract sockets, procfs and unix.Ucred are only available on Linux. Signed-off-by: Kazuyoshi Kato <[email protected]>
1 parent 25f5476 commit a4b18e0

2 files changed

Lines changed: 150 additions & 118 deletions

File tree

server_linux_test.go

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package ttrpc
18+
19+
import (
20+
"context"
21+
"strings"
22+
"testing"
23+
"time"
24+
25+
"github.com/prometheus/procfs"
26+
)
27+
28+
func TestUnixSocketHandshake(t *testing.T) {
29+
var (
30+
ctx = context.Background()
31+
server = mustServer(t)(NewServer(WithServerHandshaker(UnixSocketRequireSameUser())))
32+
addr, listener = newTestListener(t)
33+
errs = make(chan error, 1)
34+
client, cleanup = newTestClient(t, addr)
35+
)
36+
defer cleanup()
37+
defer listener.Close()
38+
go func() {
39+
errs <- server.Serve(ctx, listener)
40+
}()
41+
42+
registerTestingService(server, &testingServer{})
43+
44+
var tp testPayload
45+
// server shutdown, but we still make a call.
46+
if err := client.Call(ctx, serviceName, "Test", &tp, &tp); err != nil {
47+
t.Fatalf("unexpected error making call: %v", err)
48+
}
49+
}
50+
51+
func BenchmarkRoundTripUnixSocketCreds(b *testing.B) {
52+
// TODO(stevvooe): Right now, there is a 5x performance decrease when using
53+
// unix socket credentials. See (UnixCredentialsFunc).Handshake for
54+
// details.
55+
56+
var (
57+
ctx = context.Background()
58+
server = mustServer(b)(NewServer(WithServerHandshaker(UnixSocketRequireSameUser())))
59+
testImpl = &testingServer{}
60+
addr, listener = newTestListener(b)
61+
client, cleanup = newTestClient(b, addr)
62+
tclient = newTestingClient(client)
63+
)
64+
65+
defer listener.Close()
66+
defer cleanup()
67+
68+
registerTestingService(server, testImpl)
69+
70+
go server.Serve(ctx, listener)
71+
defer server.Shutdown(ctx)
72+
73+
var tp testPayload
74+
b.ResetTimer()
75+
76+
for i := 0; i < b.N; i++ {
77+
if _, err := tclient.Test(ctx, &tp); err != nil {
78+
b.Fatal(err)
79+
}
80+
}
81+
}
82+
83+
func TestServerEOF(t *testing.T) {
84+
var (
85+
ctx = context.Background()
86+
server = mustServer(t)(NewServer())
87+
addr, listener = newTestListener(t)
88+
client, cleanup = newTestClient(t, addr)
89+
)
90+
defer cleanup()
91+
defer listener.Close()
92+
93+
socketCountBefore := socketCount(t)
94+
95+
go server.Serve(ctx, listener)
96+
97+
registerTestingService(server, &testingServer{})
98+
99+
tp := &testPayload{}
100+
// do a regular call
101+
if err := client.Call(ctx, serviceName, "Test", tp, tp); err != nil {
102+
t.Fatalf("unexpected error during test call: %v", err)
103+
}
104+
105+
// close the client, so that server gets EOF
106+
if err := client.Close(); err != nil {
107+
t.Fatalf("unexpected error while closing client: %v", err)
108+
}
109+
110+
// server should eventually close the client connection
111+
maxAttempts := 20
112+
for i := 1; i <= maxAttempts; i++ {
113+
socketCountAfter := socketCount(t)
114+
if socketCountAfter < socketCountBefore {
115+
break
116+
}
117+
if i == maxAttempts {
118+
t.Fatalf("expected number of open sockets to be less than %d after client close, got %d open sockets",
119+
socketCountBefore, socketCountAfter)
120+
}
121+
time.Sleep(100 * time.Millisecond)
122+
}
123+
}
124+
125+
func socketCount(t *testing.T) int {
126+
proc, err := procfs.Self()
127+
if err != nil {
128+
t.Fatalf("unexpected error while reading procfs: %v", err)
129+
}
130+
fds, err := proc.FileDescriptorTargets()
131+
if err != nil {
132+
t.Fatalf("unexpected error while listing open file descriptors: %v", err)
133+
}
134+
135+
sockets := 0
136+
for _, fd := range fds {
137+
if strings.Contains(fd, "socket") {
138+
sockets++
139+
}
140+
}
141+
return sockets
142+
}

server_test.go

Lines changed: 8 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ import (
2121
"fmt"
2222
"net"
2323
"reflect"
24+
"runtime"
2425
"strings"
2526
"sync"
2627
"testing"
2728
"time"
2829

2930
"github.com/gogo/protobuf/proto"
3031
"github.com/pkg/errors"
31-
"github.com/prometheus/procfs"
3232
"google.golang.org/grpc/codes"
3333
"google.golang.org/grpc/status"
3434
)
@@ -366,71 +366,6 @@ func TestClientEOF(t *testing.T) {
366366
}
367367
}
368368

369-
func TestServerEOF(t *testing.T) {
370-
var (
371-
ctx = context.Background()
372-
server = mustServer(t)(NewServer())
373-
addr, listener = newTestListener(t)
374-
client, cleanup = newTestClient(t, addr)
375-
)
376-
defer cleanup()
377-
defer listener.Close()
378-
379-
socketCountBefore := socketCount(t)
380-
381-
go server.Serve(ctx, listener)
382-
383-
registerTestingService(server, &testingServer{})
384-
385-
tp := &testPayload{}
386-
// do a regular call
387-
if err := client.Call(ctx, serviceName, "Test", tp, tp); err != nil {
388-
t.Fatalf("unexpected error during test call: %v", err)
389-
}
390-
391-
// close the client, so that server gets EOF
392-
if err := client.Close(); err != nil {
393-
t.Fatalf("unexpected error while closing client: %v", err)
394-
}
395-
396-
// server should eventually close the client connection
397-
maxAttempts := 20
398-
for i := 1; i <= maxAttempts; i++ {
399-
socketCountAfter := socketCount(t)
400-
if socketCountAfter < socketCountBefore {
401-
break
402-
}
403-
if i == maxAttempts {
404-
t.Fatalf("expected number of open sockets to be less than %d after client close, got %d open sockets",
405-
socketCountBefore, socketCountAfter)
406-
}
407-
time.Sleep(100 * time.Millisecond)
408-
}
409-
}
410-
411-
func TestUnixSocketHandshake(t *testing.T) {
412-
var (
413-
ctx = context.Background()
414-
server = mustServer(t)(NewServer(WithServerHandshaker(UnixSocketRequireSameUser())))
415-
addr, listener = newTestListener(t)
416-
errs = make(chan error, 1)
417-
client, cleanup = newTestClient(t, addr)
418-
)
419-
defer cleanup()
420-
defer listener.Close()
421-
go func() {
422-
errs <- server.Serve(ctx, listener)
423-
}()
424-
425-
registerTestingService(server, &testingServer{})
426-
427-
var tp testPayload
428-
// server shutdown, but we still make a call.
429-
if err := client.Call(ctx, serviceName, "Test", &tp, &tp); err != nil {
430-
t.Fatalf("unexpected error making call: %v", err)
431-
}
432-
}
433-
434369
func TestServerRequestTimeout(t *testing.T) {
435370
var (
436371
ctx, cancel = context.WithDeadline(context.Background(), time.Now().Add(10*time.Minute))
@@ -534,38 +469,6 @@ func BenchmarkRoundTrip(b *testing.B) {
534469
}
535470
}
536471

537-
func BenchmarkRoundTripUnixSocketCreds(b *testing.B) {
538-
// TODO(stevvooe): Right now, there is a 5x performance decrease when using
539-
// unix socket credentials. See (UnixCredentialsFunc).Handshake for
540-
// details.
541-
542-
var (
543-
ctx = context.Background()
544-
server = mustServer(b)(NewServer(WithServerHandshaker(UnixSocketRequireSameUser())))
545-
testImpl = &testingServer{}
546-
addr, listener = newTestListener(b)
547-
client, cleanup = newTestClient(b, addr)
548-
tclient = newTestingClient(client)
549-
)
550-
551-
defer listener.Close()
552-
defer cleanup()
553-
554-
registerTestingService(server, testImpl)
555-
556-
go server.Serve(ctx, listener)
557-
defer server.Shutdown(ctx)
558-
559-
var tp testPayload
560-
b.ResetTimer()
561-
562-
for i := 0; i < b.N; i++ {
563-
if _, err := tclient.Test(ctx, &tp); err != nil {
564-
b.Fatal(err)
565-
}
566-
}
567-
}
568-
569472
func checkServerShutdown(t *testing.T, server *Server) {
570473
t.Helper()
571474
server.mu.Lock()
@@ -620,7 +523,13 @@ func newTestClient(t testing.TB, addr string, opts ...ClientOpts) (*Client, func
620523
}
621524

622525
func newTestListener(t testing.TB) (string, net.Listener) {
623-
addr := "\x00" + t.Name()
526+
var prefix string
527+
528+
// Abstracts sockets are only available on Linux.
529+
if runtime.GOOS == "linux" {
530+
prefix = "\x00"
531+
}
532+
addr := prefix + t.Name()
624533
listener, err := net.Listen("unix", addr)
625534
if err != nil {
626535
t.Fatal(err)
@@ -639,22 +548,3 @@ func mustServer(t testing.TB) func(server *Server, err error) *Server {
639548
return server
640549
}
641550
}
642-
643-
func socketCount(t *testing.T) int {
644-
proc, err := procfs.Self()
645-
if err != nil {
646-
t.Fatalf("unexpected error while reading procfs: %v", err)
647-
}
648-
fds, err := proc.FileDescriptorTargets()
649-
if err != nil {
650-
t.Fatalf("unexpected error while listing open file descriptors: %v", err)
651-
}
652-
653-
sockets := 0
654-
for _, fd := range fds {
655-
if strings.Contains(fd, "socket") {
656-
sockets++
657-
}
658-
}
659-
return sockets
660-
}

0 commit comments

Comments
 (0)