Source file
src/google.golang.org/grpc/default_dial_option_server_option_test.go
1
18
19 package grpc
20
21 import (
22 "fmt"
23 "strings"
24 "testing"
25
26 "google.golang.org/grpc/credentials/insecure"
27 "google.golang.org/grpc/internal"
28 )
29
30 func (s) TestAddGlobalDialOptions(t *testing.T) {
31
32 if _, err := Dial("fake"); err == nil {
33 t.Fatalf("Dialing without a credential did not fail")
34 } else {
35 if !strings.Contains(err.Error(), "no transport security set") {
36 t.Fatalf("Dialing failed with unexpected error: %v", err)
37 }
38 }
39
40
41 opts := []DialOption{WithTransportCredentials(insecure.NewCredentials()), WithTransportCredentials(insecure.NewCredentials()), WithTransportCredentials(insecure.NewCredentials())}
42 internal.AddGlobalDialOptions.(func(opt ...DialOption))(opts...)
43 for i, opt := range opts {
44 if globalDialOptions[i] != opt {
45 t.Fatalf("Unexpected global dial option at index %d: %v != %v", i, globalDialOptions[i], opt)
46 }
47 }
48
49
50 if cc, err := Dial("fake"); err != nil {
51 t.Fatalf("Dialing with insecure credential failed: %v", err)
52 } else {
53 cc.Close()
54 }
55
56 internal.ClearGlobalDialOptions()
57 if len(globalDialOptions) != 0 {
58 t.Fatalf("Unexpected len of globalDialOptions: %d != 0", len(globalDialOptions))
59 }
60 }
61
62
63
64 func (s) TestDisableGlobalOptions(t *testing.T) {
65
66 internal.AddGlobalDialOptions.(func(opt ...DialOption))(WithTransportCredentials(insecure.NewCredentials()))
67
68
69
70 noTSecStr := "no transport security set"
71 if _, err := Dial("fake", internal.DisableGlobalDialOptions.(func() DialOption)()); !strings.Contains(fmt.Sprint(err), noTSecStr) {
72 t.Fatalf("Dialing received unexpected error: %v, want error containing \"%v\"", err, noTSecStr)
73 }
74 internal.ClearGlobalDialOptions()
75 }
76
77 func (s) TestAddGlobalServerOptions(t *testing.T) {
78 const maxRecvSize = 998765
79
80 opts := []ServerOption{Creds(insecure.NewCredentials()), MaxRecvMsgSize(maxRecvSize)}
81 internal.AddGlobalServerOptions.(func(opt ...ServerOption))(opts...)
82 for i, opt := range opts {
83 if globalServerOptions[i] != opt {
84 t.Fatalf("Unexpected global server option at index %d: %v != %v", i, globalServerOptions[i], opt)
85 }
86 }
87
88
89 s := NewServer()
90 if s.opts.maxReceiveMessageSize != maxRecvSize {
91 t.Fatalf("Unexpected s.opts.maxReceiveMessageSize: %d != %d", s.opts.maxReceiveMessageSize, maxRecvSize)
92 }
93
94 internal.ClearGlobalServerOptions()
95 if len(globalServerOptions) != 0 {
96 t.Fatalf("Unexpected len of globalServerOptions: %d != 0", len(globalServerOptions))
97 }
98 }
99
100
101
102
103 func (s) TestJoinDialOption(t *testing.T) {
104 const maxRecvSize = 998765
105 const initialWindowSize = 100
106 jdo := newJoinDialOption(WithTransportCredentials(insecure.NewCredentials()), WithReadBufferSize(maxRecvSize), WithInitialWindowSize(initialWindowSize))
107 cc, err := Dial("fake", jdo)
108 if err != nil {
109 t.Fatalf("Dialing with insecure credentials failed: %v", err)
110 }
111 defer cc.Close()
112 if cc.dopts.copts.ReadBufferSize != maxRecvSize {
113 t.Fatalf("Unexpected cc.dopts.copts.ReadBufferSize: %d != %d", cc.dopts.copts.ReadBufferSize, maxRecvSize)
114 }
115 if cc.dopts.copts.InitialWindowSize != initialWindowSize {
116 t.Fatalf("Unexpected cc.dopts.copts.InitialWindowSize: %d != %d", cc.dopts.copts.InitialWindowSize, initialWindowSize)
117 }
118 }
119
120
121
122
123 func (s) TestJoinServerOption(t *testing.T) {
124 const maxRecvSize = 998765
125 const initialWindowSize = 100
126 jso := newJoinServerOption(Creds(insecure.NewCredentials()), MaxRecvMsgSize(maxRecvSize), InitialWindowSize(initialWindowSize))
127 s := NewServer(jso)
128 if s.opts.maxReceiveMessageSize != maxRecvSize {
129 t.Fatalf("Unexpected s.opts.maxReceiveMessageSize: %d != %d", s.opts.maxReceiveMessageSize, maxRecvSize)
130 }
131 if s.opts.initialWindowSize != initialWindowSize {
132 t.Fatalf("Unexpected s.opts.initialWindowSize: %d != %d", s.opts.initialWindowSize, initialWindowSize)
133 }
134 }
135
136
137 func (s) TestHeaderListSizeDialOptionServerOption(t *testing.T) {
138 const maxHeaderListSize uint32 = 998765
139 clientHeaderListSize := WithMaxHeaderListSize(maxHeaderListSize)
140 if clientHeaderListSize.(MaxHeaderListSizeDialOption).MaxHeaderListSize != maxHeaderListSize {
141 t.Fatalf("Unexpected s.opts.MaxHeaderListSizeDialOption.MaxHeaderListSize: %d != %d", clientHeaderListSize, maxHeaderListSize)
142 }
143 serverHeaderListSize := MaxHeaderListSize(maxHeaderListSize)
144 if serverHeaderListSize.(MaxHeaderListSizeServerOption).MaxHeaderListSize != maxHeaderListSize {
145 t.Fatalf("Unexpected s.opts.MaxHeaderListSizeDialOption.MaxHeaderListSize: %d != %d", serverHeaderListSize, maxHeaderListSize)
146 }
147 }
148
View as plain text