github.com/blend/go-sdk@v1.20220411.3/grpcutil/unary_server_chain_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package grpcutil
     9  
    10  import (
    11  	"context"
    12  	"testing"
    13  
    14  	"google.golang.org/grpc"
    15  
    16  	"github.com/blend/go-sdk/assert"
    17  )
    18  
    19  func TestUnaryServerChain(t *testing.T) {
    20  	assert := assert.New(t)
    21  
    22  	var calls []string
    23  	combined := UnaryServerChain(
    24  		func(ctx context.Context, args interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
    25  			calls = append(calls, "first")
    26  			return handler(ctx, args)
    27  		},
    28  		func(ctx context.Context, args interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
    29  			calls = append(calls, "second")
    30  			return handler(ctx, args)
    31  		},
    32  		func(ctx context.Context, args interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
    33  			calls = append(calls, "third")
    34  			return handler(ctx, args)
    35  		},
    36  	)
    37  
    38  	res, err := combined(context.Background(), nil, nil, func(ctx context.Context, args interface{}) (interface{}, error) {
    39  		calls = append(calls, "fourth")
    40  		return "ok!", nil
    41  	})
    42  	assert.Nil(err)
    43  	assert.Equal("ok!", res)
    44  	assert.Equal([]string{"third", "second", "first", "fourth"}, calls)
    45  }