MCPcopy
hub / github.com/grpc/grpc-go / TestServer_MaxHandlers

Method TestServer_MaxHandlers

server_ext_test.go:42–105  ·  view source on GitHub ↗

TestServer_MaxHandlers ensures that no more than MaxConcurrentStreams server handlers are active at one time.

(t *testing.T)

Source from the content-addressed store, hash-verified

40// TestServer_MaxHandlers ensures that no more than MaxConcurrentStreams server
41// handlers are active at one time.
42func (s) TestServer_MaxHandlers(t *testing.T) {
43 started := make(chan struct{})
44 blockCalls := grpcsync.NewEvent()
45
46 // This stub server does not properly respect the stream context, so it will
47 // not exit when the context is canceled.
48 ss := stubserver.StubServer{
49 FullDuplexCallF: func(testgrpc.TestService_FullDuplexCallServer) error {
50 started <- struct{}{}
51 <-blockCalls.Done()
52 return nil
53 },
54 }
55 if err := ss.Start([]grpc.ServerOption{grpc.MaxConcurrentStreams(1)}); err != nil {
56 t.Fatal("Error starting server:", err)
57 }
58 defer ss.Stop()
59
60 ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
61 defer cancel()
62
63 // Start one RPC to the server.
64 ctx1, cancel1 := context.WithCancel(ctx)
65 _, err := ss.Client.FullDuplexCall(ctx1)
66 if err != nil {
67 t.Fatal("Error staring call:", err)
68 }
69
70 // Wait for the handler to be invoked.
71 select {
72 case <-started:
73 case <-ctx.Done():
74 t.Fatalf("Timed out waiting for RPC to start on server.")
75 }
76
77 // Cancel it on the client. The server handler will still be running.
78 cancel1()
79
80 ctx2, cancel2 := context.WithCancel(ctx)
81 defer cancel2()
82 s, err := ss.Client.FullDuplexCall(ctx2)
83 if err != nil {
84 t.Fatal("Error staring call:", err)
85 }
86
87 // After 100ms, allow the first call to unblock. That should allow the
88 // second RPC to run and finish.
89 select {
90 case <-started:
91 blockCalls.Fire()
92 t.Fatalf("RPC started unexpectedly.")
93 case <-time.After(100 * time.Millisecond):
94 blockCalls.Fire()
95 }
96
97 select {
98 case <-started:
99 case <-ctx.Done():

Callers

nothing calls this directly

Calls 10

DoneMethod · 0.95
StartMethod · 0.95
StopMethod · 0.95
FireMethod · 0.95
NewEventFunction · 0.92
MaxConcurrentStreamsFunction · 0.92
FatalMethod · 0.65
FullDuplexCallMethod · 0.65
FatalfMethod · 0.65
RecvMethod · 0.65

Tested by

no test coverage detected