github.com/cloudwego/kitex@v0.9.0/pkg/streaming/timeout_test.go (about) 1 /* 2 * Copyright 2023 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 streaming 18 19 import ( 20 "context" 21 "strings" 22 "testing" 23 "time" 24 25 "github.com/cloudwego/kitex/internal/test" 26 ) 27 28 func TestCallWithTimeout(t *testing.T) { 29 cancelCreator := func(u *int) context.CancelFunc { 30 return func() { 31 *u = 1 32 } 33 } 34 t.Run("without-timeout", func(t *testing.T) { 35 canceled := 0 36 cancel := cancelCreator(&canceled) 37 begin := time.Now() 38 _ = CallWithTimeout(0, cancel, func() (err error) { 39 return nil 40 }) 41 elapsed := time.Since(begin) 42 test.Assertf(t, canceled == 0, "canceled = %v", canceled) 43 test.Assertf(t, elapsed < 50*time.Millisecond, "elapsed = %v", elapsed) 44 }) 45 46 t.Run("without-cancel", func(t *testing.T) { 47 defer func() { 48 recover() 49 }() 50 _ = CallWithTimeout(100*time.Millisecond, nil, func() (err error) { 51 return nil 52 }) 53 t.Errorf("should not reach here") 54 }) 55 56 t.Run("without-timeout-and-panic", func(t *testing.T) { 57 canceled := 0 58 cancel := cancelCreator(&canceled) 59 defer func() { 60 recover() 61 test.Assertf(t, canceled == 0, "canceled = %v", canceled) 62 }() 63 _ = CallWithTimeout(0, cancel, func() (err error) { 64 panic("deliberate panic") 65 }) 66 t.Errorf("should not reach here") 67 }) 68 69 t.Run("with-timeout-and-returned-in-time", func(t *testing.T) { 70 canceled := 0 71 cancel := cancelCreator(&canceled) 72 begin := time.Now() 73 err := CallWithTimeout(100*time.Millisecond, cancel, func() (err error) { 74 return nil 75 }) 76 elapsed := time.Since(begin) 77 test.Assertf(t, err == nil, "err = %v", err) 78 test.Assertf(t, elapsed < 50*time.Millisecond, "elapsed = %v", elapsed) 79 test.Assertf(t, canceled == 0, "canceled = %v", canceled) 80 }) 81 82 t.Run("with-timeout-and-returned-late", func(t *testing.T) { 83 canceled := 0 84 cancel := cancelCreator(&canceled) 85 begin := time.Now() 86 err := CallWithTimeout(50*time.Millisecond, cancel, func() (err error) { 87 time.Sleep(100 * time.Millisecond) 88 return nil 89 }) 90 elapsed := time.Since(begin) 91 test.Assertf(t, err != nil, "err = %v", err) 92 test.Assertf(t, strings.Contains(err.Error(), "timeout in business code"), "err = %v", err) 93 test.Assertf(t, elapsed < 80*time.Millisecond, "elapsed = %v", elapsed) 94 test.Assertf(t, canceled == 1, "canceled = %v", canceled) 95 }) 96 97 t.Run("with-timeout-and-panic", func(t *testing.T) { 98 canceled := 0 99 cancel := cancelCreator(&canceled) 100 begin := time.Now() 101 err := CallWithTimeout(100*time.Millisecond, cancel, func() (err error) { 102 panic("deliberate panic") 103 }) 104 elapsed := time.Since(begin) 105 test.Assertf(t, elapsed < 50*time.Millisecond, "elapsed = %v", elapsed) 106 test.Assertf(t, err != nil, "err = %v", err) 107 test.Assertf(t, strings.Contains(err.Error(), "panic in business code"), "err = %v", err) 108 test.Assertf(t, canceled == 1, "canceled = %v", canceled) 109 }) 110 }