github.com/pfcoder/quorum@v2.0.3-0.20180501191142-d4a1b0958135+incompatible/consensus/istanbul/core/core_test.go (about)

     1  // Copyright 2017 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum 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 go-ethereum 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 go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package core
    18  
    19  import (
    20  	"math/big"
    21  	"reflect"
    22  	"testing"
    23  	"time"
    24  
    25  	"github.com/ethereum/go-ethereum/consensus/istanbul"
    26  	"github.com/ethereum/go-ethereum/core/types"
    27  	elog "github.com/ethereum/go-ethereum/log"
    28  )
    29  
    30  func makeBlock(number int64) *types.Block {
    31  	header := &types.Header{
    32  		Difficulty: big.NewInt(0),
    33  		Number:     big.NewInt(number),
    34  		GasLimit:   big.NewInt(0),
    35  		GasUsed:    big.NewInt(0),
    36  		Time:       big.NewInt(0),
    37  	}
    38  	block := &types.Block{}
    39  	return block.WithSeal(header)
    40  }
    41  
    42  func newTestProposal() istanbul.Proposal {
    43  	return makeBlock(1)
    44  }
    45  
    46  func TestNewRequest(t *testing.T) {
    47  	testLogger.SetHandler(elog.StdoutHandler)
    48  
    49  	N := uint64(4)
    50  	F := uint64(1)
    51  
    52  	sys := NewTestSystemWithBackend(N, F)
    53  
    54  	close := sys.Run(true)
    55  	defer close()
    56  
    57  	request1 := makeBlock(1)
    58  	sys.backends[0].NewRequest(request1)
    59  
    60  	select {
    61  	case <-time.After(1 * time.Second):
    62  	}
    63  
    64  	request2 := makeBlock(2)
    65  	sys.backends[0].NewRequest(request2)
    66  
    67  	select {
    68  	case <-time.After(1 * time.Second):
    69  	}
    70  
    71  	for _, backend := range sys.backends {
    72  		if len(backend.committedMsgs) != 2 {
    73  			t.Errorf("the number of executed requests mismatch: have %v, want 2", len(backend.committedMsgs))
    74  		}
    75  		if !reflect.DeepEqual(request1.Number(), backend.committedMsgs[0].commitProposal.Number()) {
    76  			t.Errorf("the number of requests mismatch: have %v, want %v", request1.Number(), backend.committedMsgs[0].commitProposal.Number())
    77  		}
    78  		if !reflect.DeepEqual(request2.Number(), backend.committedMsgs[1].commitProposal.Number()) {
    79  			t.Errorf("the number of requests mismatch: have %v, want %v", request2.Number(), backend.committedMsgs[1].commitProposal.Number())
    80  		}
    81  	}
    82  }