...

Source file src/google.golang.org/grpc/stream_test.go

Documentation: google.golang.org/grpc

     1  /*
     2   *
     3   * Copyright 2023 gRPC authors.
     4   *
     5   * Licensed under the Apache License, Version 2.0 (the "License");
     6   * you may not use this file except in compliance with the License.
     7   * You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   */
    18  
    19  package grpc_test
    20  
    21  import (
    22  	"context"
    23  	"testing"
    24  	"time"
    25  
    26  	"google.golang.org/grpc/codes"
    27  	"google.golang.org/grpc/internal/grpctest"
    28  	"google.golang.org/grpc/internal/stubserver"
    29  	"google.golang.org/grpc/status"
    30  
    31  	testgrpc "google.golang.org/grpc/interop/grpc_testing"
    32  )
    33  
    34  const defaultTestTimeout = 10 * time.Second
    35  
    36  type s struct {
    37  	grpctest.Tester
    38  }
    39  
    40  func Test(t *testing.T) {
    41  	grpctest.RunSubTests(t, s{})
    42  }
    43  
    44  func (s) TestStream_Header_TrailersOnly(t *testing.T) {
    45  	ss := stubserver.StubServer{
    46  		FullDuplexCallF: func(stream testgrpc.TestService_FullDuplexCallServer) error {
    47  			return status.Errorf(codes.NotFound, "a test error")
    48  		},
    49  	}
    50  	if err := ss.Start(nil); err != nil {
    51  		t.Fatal("Error starting server:", err)
    52  	}
    53  	defer ss.Stop()
    54  
    55  	ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
    56  	defer cancel()
    57  
    58  	s, err := ss.Client.FullDuplexCall(ctx)
    59  	if err != nil {
    60  		t.Fatal("Error staring call", err)
    61  	}
    62  	if md, err := s.Header(); md != nil || err != nil {
    63  		t.Fatalf("s.Header() = %v, %v; want nil, nil", md, err)
    64  	}
    65  	if _, err := s.Recv(); status.Code(err) != codes.NotFound {
    66  		t.Fatalf("s.Recv() = _, %v; want _, err.Code()=codes.NotFound", err)
    67  	}
    68  }
    69  

View as plain text