github.com/turingchain2020/turingchain@v1.1.21/client/api/api_test.go (about)

     1  package api
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  	"time"
     7  
     8  	"google.golang.org/grpc/codes"
     9  
    10  	"github.com/turingchain2020/turingchain/client/mocks"
    11  	"github.com/turingchain2020/turingchain/queue"
    12  	qmocks "github.com/turingchain2020/turingchain/queue/mocks"
    13  	"github.com/turingchain2020/turingchain/rpc"
    14  	"github.com/turingchain2020/turingchain/rpc/grpcclient"
    15  	"github.com/turingchain2020/turingchain/types"
    16  	"github.com/stretchr/testify/assert"
    17  	"github.com/stretchr/testify/mock"
    18  	"google.golang.org/grpc/status"
    19  )
    20  
    21  func TestAPI(t *testing.T) {
    22  	api := new(mocks.QueueProtocolAPI)
    23  	cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring())
    24  	gapi, err := grpcclient.NewMainChainClient(cfg, "")
    25  	assert.Nil(t, err)
    26  	api.On("GetConfig", mock.Anything).Return(cfg)
    27  	eapi := New(api, gapi)
    28  	param := &types.ReqHashes{
    29  		Hashes: [][]byte{[]byte("hello")},
    30  	}
    31  	api.On("GetBlockByHashes", mock.Anything).Return(&types.BlockDetails{}, nil)
    32  	detail, err := eapi.GetBlockByHashes(param)
    33  	assert.Nil(t, err)
    34  	assert.Equal(t, detail, &types.BlockDetails{})
    35  	param2 := &types.ReqRandHash{
    36  		ExecName: "ticket",
    37  		BlockNum: 5,
    38  		Hash:     []byte("hello"),
    39  	}
    40  	api.On("Query", "ticket", "RandNumHash", mock.Anything).Return(&types.ReplyHash{Hash: []byte("hello")}, nil)
    41  	randhash, err := eapi.GetRandNum(param2)
    42  	assert.Nil(t, err)
    43  	assert.Equal(t, randhash, []byte("hello"))
    44  	assert.Equal(t, false, eapi.IsErr())
    45  	api.On("QueryTx", mock.Anything).Return(&types.TransactionDetail{Height: 1}, nil)
    46  	param3 := &types.ReqHash{Hash: []byte("hash")}
    47  	txdetail, err := eapi.QueryTx(param3)
    48  	assert.Nil(t, err)
    49  	assert.Equal(t, int64(1), txdetail.Height)
    50  	cfg.SetTitleOnlyForTest("user.p.wzw.")
    51  	//testnode setup
    52  	rpcCfg := new(types.RPC)
    53  	rpcCfg.GrpcBindAddr = "127.0.0.1:8003"
    54  	rpcCfg.JrpcBindAddr = "127.0.0.1:8004"
    55  	rpcCfg.Whitelist = []string{"127.0.0.1", "0.0.0.0"}
    56  	rpcCfg.JrpcFuncWhitelist = []string{"*"}
    57  	rpcCfg.GrpcFuncWhitelist = []string{"*"}
    58  	rpc.InitCfg(rpcCfg)
    59  	qc := &qmocks.Client{}
    60  	qc.On("GetConfig", mock.Anything).Return(cfg)
    61  	server := rpc.NewGRpcServer(qc, api)
    62  	assert.NotNil(t, server)
    63  	go server.Listen()
    64  	time.Sleep(time.Second)
    65  
    66  	eapi = New(api, gapi)
    67  	_, err = eapi.GetBlockByHashes(param)
    68  	assert.Equal(t, true, IsGrpcError(err))
    69  	assert.Equal(t, true, IsGrpcError(status.New(codes.Aborted, "operation is abort").Err()))
    70  	assert.Equal(t, false, IsGrpcError(nil))
    71  	assert.Equal(t, false, IsGrpcError(errors.New("xxxx")))
    72  	assert.Equal(t, true, eapi.IsErr())
    73  	assert.Equal(t, true, IsFatalError(types.ErrConsensusHashErr))
    74  	assert.Equal(t, false, IsFatalError(errors.New("xxxx")))
    75  
    76  	gapi2, err := grpcclient.NewMainChainClient(cfg, "127.0.0.1:8003")
    77  	assert.Nil(t, err)
    78  	eapi = New(api, gapi2)
    79  	detail, err = eapi.GetBlockByHashes(param)
    80  	assert.Equal(t, err, nil)
    81  	assert.Equal(t, detail, &types.BlockDetails{})
    82  	randhash, err = eapi.GetRandNum(param2)
    83  	assert.Nil(t, err)
    84  	assert.Equal(t, randhash, []byte("hello"))
    85  	assert.Equal(t, false, eapi.IsErr())
    86  	param3 = &types.ReqHash{Hash: []byte("hash")}
    87  	txdetail, err = eapi.QueryTx(param3)
    88  	assert.Nil(t, err)
    89  	assert.Equal(t, int64(1), txdetail.Height)
    90  	//queue err
    91  	assert.Equal(t, false, IsQueueError(nil))
    92  	assert.Equal(t, false, IsQueueError(errors.New("xxxx")))
    93  	assert.Equal(t, true, IsQueueError(queue.ErrQueueTimeout))
    94  	assert.Equal(t, true, IsQueueError(queue.ErrIsQueueClosed))
    95  	assert.Equal(t, false, IsQueueError(errors.New("ErrIsQueueClosed")))
    96  }