dubbo.apache.org/dubbo-go/v3@v3.1.1/protocol/grpc/client_test.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  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  package grpc
    19  
    20  import (
    21  	"context"
    22  	"fmt"
    23  	"testing"
    24  )
    25  
    26  import (
    27  	"github.com/dustin/go-humanize"
    28  
    29  	"github.com/stretchr/testify/assert"
    30  )
    31  
    32  import (
    33  	"dubbo.apache.org/dubbo-go/v3/common"
    34  	"dubbo.apache.org/dubbo-go/v3/protocol/grpc/internal/helloworld"
    35  	"dubbo.apache.org/dubbo-go/v3/protocol/grpc/internal/routeguide"
    36  )
    37  
    38  func TestUnaryClient(t *testing.T) {
    39  	server, err := helloworld.NewServer("127.0.0.1:30000")
    40  	assert.NoError(t, err)
    41  	go server.Start()
    42  	defer server.Stop()
    43  
    44  	url, err := common.NewURL(helloworldURL)
    45  	assert.NoError(t, err)
    46  
    47  	cli, err := NewClient(url)
    48  	assert.NoError(t, err)
    49  
    50  	impl := &helloworld.GreeterClientImpl{}
    51  	client := impl.GetDubboStub(cli.ClientConn)
    52  	result, err := client.SayHello(context.Background(), &helloworld.HelloRequest{Name: "request name"})
    53  	assert.NoError(t, err)
    54  	assert.Equal(t, &helloworld.HelloReply{Message: "Hello request name"}, result)
    55  }
    56  
    57  func TestStreamClient(t *testing.T) {
    58  	server, err := routeguide.NewServer("127.0.0.1:30000")
    59  	assert.NoError(t, err)
    60  	go server.Start()
    61  	defer server.Stop()
    62  
    63  	url, err := common.NewURL(routeguideURL)
    64  	assert.NoError(t, err)
    65  
    66  	cli, err := NewClient(url)
    67  	assert.NoError(t, err)
    68  
    69  	impl := &routeguide.RouteGuideClientImpl{}
    70  	client := impl.GetDubboStub(cli.ClientConn)
    71  
    72  	result, err := client.GetFeature(context.Background(), &routeguide.Point{Latitude: 409146138, Longitude: -746188906})
    73  	assert.NoError(t, err)
    74  	assert.Equal(t, &routeguide.Feature{
    75  		Name:     "Berkshire Valley Management Area Trail, Jefferson, NJ, USA",
    76  		Location: &routeguide.Point{Latitude: 409146138, Longitude: -746188906},
    77  	}, result)
    78  
    79  	listFeaturesStream, err := client.ListFeatures(context.Background(), &routeguide.Rectangle{
    80  		Lo: &routeguide.Point{Latitude: 400000000, Longitude: -750000000},
    81  		Hi: &routeguide.Point{Latitude: 420000000, Longitude: -730000000},
    82  	})
    83  	assert.NoError(t, err)
    84  	routeguide.PrintFeatures(listFeaturesStream)
    85  
    86  	recordRouteStream, err := client.RecordRoute(context.Background())
    87  	assert.NoError(t, err)
    88  	routeguide.RunRecordRoute(recordRouteStream)
    89  
    90  	routeChatStream, err := client.RouteChat(context.Background())
    91  	assert.NoError(t, err)
    92  	routeguide.RunRouteChat(routeChatStream)
    93  }
    94  
    95  func TestT(t *testing.T) {
    96  	bytes, err := humanize.ParseBytes("0")
    97  	fmt.Println(bytes, err)
    98  }