github.com/cloudwego/kitex@v0.9.0/pkg/streaming/util_test.go (about) 1 /* 2 * Copyright 2024 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 "errors" 21 "testing" 22 23 "github.com/cloudwego/kitex/internal/test" 24 "github.com/cloudwego/kitex/pkg/serviceinfo" 25 ) 26 27 func TestUnaryCompatibleMiddleware(t *testing.T) { 28 t.Run("unary+allow", func(t *testing.T) { 29 test.Assert(t, UnaryCompatibleMiddleware(serviceinfo.StreamingUnary, true)) 30 }) 31 t.Run("unary+not-allow", func(t *testing.T) { 32 test.Assert(t, !UnaryCompatibleMiddleware(serviceinfo.StreamingUnary, false)) 33 }) 34 t.Run("not-unary+allow", func(t *testing.T) { 35 test.Assert(t, !UnaryCompatibleMiddleware(serviceinfo.StreamingNone, true)) 36 }) 37 t.Run("not-unary+not-allow", func(t *testing.T) { 38 test.Assert(t, !UnaryCompatibleMiddleware(serviceinfo.StreamingNone, false)) 39 }) 40 } 41 42 type mockStream struct { 43 Stream 44 } 45 46 type mockStreamWithDoFinish struct { 47 Stream 48 doFinish func(error) 49 } 50 51 func (m *mockStreamWithDoFinish) DoFinish(err error) { 52 m.doFinish(err) 53 } 54 55 func TestFinishStream(t *testing.T) { 56 t.Run("implemented-do-finish", func(t *testing.T) { 57 mockErr := errors.New("mock") 58 called := false 59 s := &mockStreamWithDoFinish{ 60 doFinish: func(err error) { 61 called = true 62 test.Assert(t, err == mockErr) 63 }, 64 } 65 FinishStream(s, mockErr) 66 test.Assert(t, called) 67 }) 68 t.Run("not-implemented-do-finish", func(t *testing.T) { 69 s := &mockStream{} 70 FinishStream(s, nil) 71 }) 72 }