github.com/cloudwego/kitex@v0.9.0/pkg/remote/trans/nphttp2/grpc/controlbuf_test.go (about) 1 /* 2 * Copyright 2022 CloudWeGo 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 grpc 18 19 import ( 20 "context" 21 "testing" 22 "time" 23 24 "github.com/cloudwego/kitex/internal/test" 25 ) 26 27 func TestControlBuf(t *testing.T) { 28 ctx := context.Background() 29 cb := newControlBuffer(ctx.Done()) 30 31 // test put() 32 testItem := &ping{} 33 err := cb.put(testItem) 34 test.Assert(t, err == nil, err) 35 36 // test get() with block 37 item, err := cb.get(true) 38 test.Assert(t, err == nil, err) 39 test.Assert(t, item == testItem, err) 40 41 // test get() with no block 42 item, err = cb.get(false) 43 test.Assert(t, err == nil, err) 44 test.Assert(t, item == nil, err) 45 46 // test executeAndPut() 47 success, err := cb.execute(func(it interface{}) bool { 48 return false 49 }, &ping{}) 50 51 test.Assert(t, err == nil, err) 52 test.Assert(t, !success, err) 53 54 // test throttle() mock a lot of response frame so throttle() will block current goroutine 55 for i := 0; i < maxQueuedTransportResponseFrames+5; i++ { 56 err := cb.put(&ping{}) 57 test.Assert(t, err == nil, err) 58 } 59 60 // start a new goroutine to consume response frame 61 go func() { 62 time.Sleep(time.Millisecond * 100) 63 for { 64 it, err := cb.get(false) 65 if err != nil || it == nil { 66 break 67 } 68 } 69 }() 70 71 cb.throttle() 72 73 // test finish() 74 cb.finish() 75 }