github.com/klaytn/klaytn@v1.12.1/networks/grpc/gRPC_test.go (about)

     1  // Copyright 2019 The klaytn Authors
     2  // This file is part of the klaytn library.
     3  //
     4  // The klaytn library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The klaytn library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the klaytn library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package grpc
    18  
    19  import (
    20  	"encoding/json"
    21  	"sync"
    22  	"testing"
    23  	"time"
    24  
    25  	"github.com/klaytn/klaytn/networks/rpc"
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  const (
    30  	TEST_BLOCK_NUMBER = float64(123456789)
    31  )
    32  
    33  type APIgRPC struct{}
    34  
    35  func (a APIgRPC) BlockNumber() float64 {
    36  	return TEST_BLOCK_NUMBER
    37  }
    38  
    39  func TestGRPC(t *testing.T) {
    40  	wg := &sync.WaitGroup{}
    41  	wg.Add(2)
    42  
    43  	addr := "127.0.0.1:4000"
    44  	handler := rpc.NewServer()
    45  
    46  	handler.RegisterName("klay", &APIgRPC{})
    47  
    48  	listener := &Listener{Addr: addr}
    49  	listener.SetRPCServer(handler)
    50  	go listener.Start()
    51  
    52  	time.Sleep(2 * time.Second)
    53  
    54  	go testCall(t, addr, wg)
    55  	go testBiCall(t, addr, wg)
    56  	wg.Wait()
    57  }
    58  
    59  func testCall(t *testing.T, addr string, wg *sync.WaitGroup) {
    60  	defer wg.Done()
    61  
    62  	kclient, _ := NewgKlaytnClient(addr)
    63  	defer kclient.Close()
    64  
    65  	knclient, err := kclient.makeKlaytnClient(timeout)
    66  	assert.NoError(t, err)
    67  
    68  	request, err := kclient.makeRPCRequest("klay", "klay_blockNumber", nil)
    69  	assert.NoError(t, err)
    70  
    71  	response, err := knclient.Call(kclient.ctx, request)
    72  	assert.NoError(t, err)
    73  
    74  	err = kclient.handleRPCResponse(response)
    75  	assert.NoError(t, err)
    76  
    77  	var out jsonSuccessResponse
    78  	json.Unmarshal(response.Payload, &out)
    79  	assert.NoError(t, err)
    80  	assert.Equal(t, out.Result, TEST_BLOCK_NUMBER)
    81  }
    82  
    83  func testBiCall(t *testing.T, addr string, wg *sync.WaitGroup) {
    84  	defer wg.Done()
    85  
    86  	kclient, _ := NewgKlaytnClient(addr)
    87  	defer kclient.Close()
    88  
    89  	knclient, err := kclient.makeKlaytnClient(timeout)
    90  	assert.NoError(t, err)
    91  
    92  	stream, _ := knclient.BiCall(kclient.ctx)
    93  	go kclient.handleBiCall(stream, func() (request *RPCRequest, e error) {
    94  		request, err := kclient.makeRPCRequest("klay", "klay_blockNumber", nil)
    95  		if assert.NoError(t, err) {
    96  			return request, err
    97  		}
    98  
    99  		return request, nil
   100  	}, func(response *RPCResponse) error {
   101  		var out jsonSuccessResponse
   102  		err = json.Unmarshal(response.Payload, &out)
   103  		assert.NoError(t, err)
   104  		assert.Equal(t, out.Result, TEST_BLOCK_NUMBER)
   105  
   106  		return nil
   107  	})
   108  
   109  	time.Sleep(3 * time.Second)
   110  }