github.com/cloudwego/kitex@v0.9.0/internal/stream/stream_option_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 stream
    18  
    19  import (
    20  	"context"
    21  	"errors"
    22  	"testing"
    23  
    24  	"github.com/cloudwego/kitex/internal/test"
    25  	"github.com/cloudwego/kitex/pkg/endpoint"
    26  	"github.com/cloudwego/kitex/pkg/streaming"
    27  )
    28  
    29  func TestStreamingConfig_InitMiddlewares(t *testing.T) {
    30  	t.Run("no-event-handler", func(t *testing.T) {
    31  		sc := &StreamingConfig{}
    32  		sc.InitMiddlewares(context.Background())
    33  		test.Assert(t, len(sc.RecvMiddlewareBuilders) == 0)
    34  		test.Assert(t, len(sc.SendMiddlewareBuilders) == 0)
    35  		test.Assert(t, len(sc.RecvMiddlewares) == 0)
    36  		test.Assert(t, len(sc.SendMiddlewares) == 0)
    37  	})
    38  	t.Run("recv-mw-builder", func(t *testing.T) {
    39  		expectedErr := errors.New("expected error")
    40  		sc := &StreamingConfig{
    41  			RecvMiddlewares: []endpoint.RecvMiddleware{
    42  				nil,
    43  			},
    44  			RecvMiddlewareBuilders: []endpoint.RecvMiddlewareBuilder{
    45  				func(ctx context.Context) endpoint.RecvMiddleware {
    46  					return func(next endpoint.RecvEndpoint) endpoint.RecvEndpoint {
    47  						return func(stream streaming.Stream, message interface{}) (err error) {
    48  							return expectedErr
    49  						}
    50  					}
    51  				},
    52  			},
    53  		}
    54  
    55  		sc.InitMiddlewares(context.Background())
    56  
    57  		test.Assert(t, len(sc.RecvMiddlewares) == 2)
    58  		test.Assert(t, sc.RecvMiddlewares[0] == nil)
    59  		mw := sc.RecvMiddlewares[1]
    60  		got := mw(nil)(nil, nil)
    61  		test.Assert(t, got == expectedErr)
    62  	})
    63  
    64  	t.Run("send-mw-builder", func(t *testing.T) {
    65  		expectedErr := errors.New("expected error")
    66  		sc := &StreamingConfig{
    67  			SendMiddlewares: []endpoint.SendMiddleware{
    68  				nil,
    69  			},
    70  			SendMiddlewareBuilders: []endpoint.SendMiddlewareBuilder{
    71  				func(ctx context.Context) endpoint.SendMiddleware {
    72  					return func(next endpoint.SendEndpoint) endpoint.SendEndpoint {
    73  						return func(stream streaming.Stream, message interface{}) (err error) {
    74  							return expectedErr
    75  						}
    76  					}
    77  				},
    78  			},
    79  		}
    80  
    81  		sc.InitMiddlewares(context.Background())
    82  
    83  		test.Assert(t, len(sc.SendMiddlewares) == 2)
    84  		test.Assert(t, sc.SendMiddlewares[0] == nil)
    85  		mw := sc.SendMiddlewares[1]
    86  		got := mw(nil)(nil, nil)
    87  		test.Assert(t, got == expectedErr)
    88  	})
    89  }