github.com/reapchain/go-reapchain@v0.2.15-0.20210609012950-9735c110c705/consensus/podc/core/core_test.go (about)

     1  // Copyright 2017 AMIS Technologies
     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  	"os"
    22  	"reflect"
    23  	"testing"
    24  	"time"
    25  
    26  	"github.com/ethereum/go-ethereum/consensus/podc"
    27  	"github.com/ethereum/go-ethereum/core/types"
    28  	elog "github.com/ethereum/go-ethereum/log"
    29  )
    30  
    31  func makeBlock(number int64) *types.Block {
    32  	header := &types.Header{
    33  		Difficulty: big.NewInt(0),
    34  		Number:     big.NewInt(number),
    35  		GasLimit:   big.NewInt(0),
    36  		GasUsed:    big.NewInt(0),
    37  		Time:       big.NewInt(0),
    38  	}
    39  	block := &types.Block{}
    40  	return block.WithSeal(header)
    41  }
    42  
    43  func newTestProposal() podc.Proposal {
    44  	return makeBlock(1)
    45  }
    46  
    47  func TestNewRequest(t *testing.T) {
    48  	testLogger.SetHandler(elog.StdoutHandler)
    49  
    50  	N := uint64(28)
    51  	F := uint64(1)
    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  	request3 := makeBlock(3)
    72  	sys.backends[0].NewRequest(request3)
    73  
    74  	select {
    75  	case <-time.After(1 * time.Second):
    76  	}
    77  
    78  	for _, backend := range sys.backends {
    79  		if len(backend.commitMsgs) != 0 {
    80  			t.Errorf("the number of executed requests mismatch: have %v, want 2", len(backend.commitMsgs))
    81  		}
    82  		if backend.commitMsgs[0] == nil {
    83  			t.Errorf("backend.commitMsgs[0]h:")
    84  			os.Exit(0)
    85  		}
    86  		if !reflect.DeepEqual(request1.Number(), backend.commitMsgs[0].Number()) {
    87  			t.Errorf("the number of requests mismatch: have %v, want %v", request1.Number(), backend.commitMsgs[0].Number())
    88  		}
    89  		if !reflect.DeepEqual(request2.Number(), backend.commitMsgs[1].Number()) {
    90  			t.Errorf("the number of requests mismatch: have %v, want %v", request2.Number(), backend.commitMsgs[1].Number())
    91  		}
    92  	}
    93  }