github.com/turingchain2020/turingchain@v1.1.21/util/healthcheck_test.go (about)

     1  // Copyright Turing Corp. 2018 All Rights Reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package util
     6  
     7  import (
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/mock"
    11  
    12  	"time"
    13  
    14  	"github.com/turingchain2020/turingchain/client/mocks"
    15  	"github.com/turingchain2020/turingchain/queue"
    16  	"github.com/turingchain2020/turingchain/types"
    17  	"github.com/stretchr/testify/assert"
    18  )
    19  
    20  func TestStart(t *testing.T) {
    21  	q := queue.New("channel")
    22  	health := NewHealthCheckServer(q.Client())
    23  
    24  	api := new(mocks.QueueProtocolAPI)
    25  	api.On("IsSync").Return(&types.Reply{IsOk: true}, nil).Times(2)
    26  	api.On("IsSync").Return(&types.Reply{IsOk: false}, nil)
    27  	peer1 := &types.Peer{Addr: "addr1"}
    28  	peer2 := &types.Peer{Addr: "addr2"}
    29  	peers := &types.PeerList{Peers: []*types.Peer{peer1, peer2}}
    30  	api.On("PeerInfo", mock.Anything).Return(peers, nil)
    31  	api.On("Close").Return()
    32  	health.api = api
    33  
    34  	cfg, _ := types.InitCfg("../cmd/turingchain/turingchain.test.toml")
    35  	health.Start(cfg.Health)
    36  	time.Sleep(time.Second * 6)
    37  
    38  	health.Close()
    39  	time.Sleep(time.Second * 1)
    40  
    41  }
    42  
    43  func TestGetHealth(t *testing.T) {
    44  	api := new(mocks.QueueProtocolAPI)
    45  	reply := &types.Reply{IsOk: true}
    46  	api.On("IsSync").Return(reply, nil).Once()
    47  	peer2 := &types.Peer{Addr: "addr2"}
    48  	peerlist := &types.PeerList{Peers: []*types.Peer{peer2}}
    49  	api.On("PeerInfo", mock.Anything).Return(peerlist, nil).Once()
    50  
    51  	healthNil := NewHealthCheckServer(nil)
    52  	assert.Nil(t, healthNil)
    53  	q := queue.New("channel")
    54  	health := NewHealthCheckServer(q.Client())
    55  	health.api = api
    56  	ret, err := health.getHealth(true)
    57  	assert.Nil(t, err)
    58  	assert.Equal(t, false, ret)
    59  
    60  }