trpc.group/trpc-go/trpc-go@v1.0.3/stream/flow_control_test.go (about) 1 // 2 // 3 // Tencent is pleased to support the open source community by making tRPC available. 4 // 5 // Copyright (C) 2023 THL A29 Limited, a Tencent company. 6 // All rights reserved. 7 // 8 // If you have downloaded a copy of the tRPC source code from Tencent, 9 // please note that tRPC source code is licensed under the Apache 2.0 License, 10 // A copy of the Apache 2.0 License is included in this file. 11 // 12 // 13 14 package stream 15 16 import ( 17 "errors" 18 "testing" 19 "time" 20 21 "github.com/stretchr/testify/assert" 22 ) 23 24 // TestSendControl test flow control sender related implementation. 25 func TestSendControl(t *testing.T) { 26 done := make(chan struct{}) 27 sc := newSendControl(defaultInitWindowSize, done) 28 err := sc.GetWindow(100) 29 assert.Nil(t, err) 30 31 // Available window drops less than 0. 32 err = sc.GetWindow(uint32(defaultInitWindowSize - 99)) 33 assert.Nil(t, err) 34 35 // block 36 t1 := time.Now() 37 go func() { 38 time.Sleep(500 * time.Millisecond) 39 sc.UpdateWindow(201) 40 }() 41 err = sc.GetWindow(200) 42 assert.Nil(t, err) 43 t2 := time.Since(t1) 44 assert.GreaterOrEqual(t, t2, 500*time.Millisecond) 45 } 46 47 // TestReceiveControl test. 48 func TestReceiveControl(t *testing.T) { 49 fb := func(uint32) error { 50 return nil 51 } 52 rc := newReceiveControl(defaultInitWindowSize, fb) 53 err := rc.OnRecv(100) 54 assert.Nil(t, err) 55 56 // need to send updates. 57 err = rc.OnRecv(defaultInitWindowSize / 4) 58 assert.Nil(t, err) 59 60 // test for feedback errors. 61 fb = func(uint32) error { 62 return errors.New("feedback error") 63 } 64 err = rc.OnRecv(100) 65 assert.Nil(t, err) 66 67 rc = newReceiveControl(defaultInitWindowSize, fb) 68 err = rc.OnRecv(defaultInitWindowSize / 4) 69 assert.NotNil(t, err) 70 }