github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/consensus/dbft/core/request_test.go (about)

     1  package core
     2  
     3  import (
     4  	"math/big"
     5  	"reflect"
     6  	"sync"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/quickchainproject/quickchain/common"
    11  	"github.com/quickchainproject/quickchain/consensus/dbft"
    12  	"github.com/quickchainproject/quickchain/event"
    13  	"github.com/quickchainproject/quickchain/log"
    14  	"gopkg.in/karalabe/cookiejar.v2/collections/prque"
    15  )
    16  
    17  func TestCheckRequestMsg(t *testing.T) {
    18  	c := &core{
    19  		state: StateAcceptRequest,
    20  		current: newRoundState(&bft.View{
    21  			Sequence: big.NewInt(1),
    22  			Round:    big.NewInt(0),
    23  		}, newTestValidatorSet(4), common.Hash{}, nil, nil, nil),
    24  	}
    25  
    26  	// invalid request
    27  	err := c.checkRequestMsg(nil)
    28  	if err != errInvalidMessage {
    29  		t.Errorf("error mismatch: have %v, want %v", err, errInvalidMessage)
    30  	}
    31  	r := &bft.Request{
    32  		Proposal: nil,
    33  	}
    34  	err = c.checkRequestMsg(r)
    35  	if err != errInvalidMessage {
    36  		t.Errorf("error mismatch: have %v, want %v", err, errInvalidMessage)
    37  	}
    38  
    39  	// old request
    40  	r = &bft.Request{
    41  		Proposal: makeBlock(0),
    42  	}
    43  	err = c.checkRequestMsg(r)
    44  	if err != errOldMessage {
    45  		t.Errorf("error mismatch: have %v, want %v", err, errOldMessage)
    46  	}
    47  
    48  	// future request
    49  	r = &bft.Request{
    50  		Proposal: makeBlock(2),
    51  	}
    52  	err = c.checkRequestMsg(r)
    53  	if err != errFutureMessage {
    54  		t.Errorf("error mismatch: have %v, want %v", err, errFutureMessage)
    55  	}
    56  
    57  	// current request
    58  	r = &bft.Request{
    59  		Proposal: makeBlock(1),
    60  	}
    61  	err = c.checkRequestMsg(r)
    62  	if err != nil {
    63  		t.Errorf("error mismatch: have %v, want nil", err)
    64  	}
    65  }
    66  
    67  func TestStoreRequestMsg(t *testing.T) {
    68  	backend := &testSystemBackend{
    69  		events: new(event.TypeMux),
    70  	}
    71  	c := &core{
    72  		logger:  log.New("backend", "test", "id", 0),
    73  		backend: backend,
    74  		state:   StateAcceptRequest,
    75  		current: newRoundState(&bft.View{
    76  			Sequence: big.NewInt(0),
    77  			Round:    big.NewInt(0),
    78  		}, newTestValidatorSet(4), common.Hash{}, nil, nil, nil),
    79  		pendingRequests:   prque.New(),
    80  		pendingRequestsMu: new(sync.Mutex),
    81  	}
    82  	requests := []bft.Request{
    83  		{
    84  			Proposal: makeBlock(1),
    85  		},
    86  		{
    87  			Proposal: makeBlock(2),
    88  		},
    89  		{
    90  			Proposal: makeBlock(3),
    91  		},
    92  	}
    93  
    94  	c.storeRequestMsg(&requests[1])
    95  	c.storeRequestMsg(&requests[0])
    96  	c.storeRequestMsg(&requests[2])
    97  	if c.pendingRequests.Size() != len(requests) {
    98  		t.Errorf("the size of pending requests mismatch: have %v, want %v", c.pendingRequests.Size(), len(requests))
    99  	}
   100  
   101  	c.current.sequence = big.NewInt(3)
   102  
   103  	c.subscribeEvents()
   104  	defer c.unsubscribeEvents()
   105  
   106  	c.processPendingRequests()
   107  
   108  	const timeoutDura = 2 * time.Second
   109  	timeout := time.NewTimer(timeoutDura)
   110  	select {
   111  	case ev := <-c.events.Chan():
   112  		e, ok := ev.Data.(bft.RequestEvent)
   113  		if !ok {
   114  			t.Errorf("unexpected event comes: %v", reflect.TypeOf(ev.Data))
   115  		}
   116  		if e.Proposal.Number().Cmp(requests[2].Proposal.Number()) != 0 {
   117  			t.Errorf("the number of proposal mismatch: have %v, want %v", e.Proposal.Number(), requests[2].Proposal.Number())
   118  		}
   119  	case <-timeout.C:
   120  		t.Error("unexpected timeout occurs")
   121  	}
   122  }