github.com/cranelv/ethereum_mpc@v0.0.0-20191031014521-23aeb1415092/consensus_pbft/pbft/requeststore_test.go (about)

     1  /*
     2  Copyright IBM Corp. 2016 All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8                   http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package pbft
    18  
    19  import (
    20  	"testing"
    21  	"github.com/ethereum/go-ethereum/consensus_pbft/message"
    22  	"github.com/ethereum/go-ethereum/consensus_pbft/pbftTypes"
    23  )
    24  
    25  func TestOrderedRequests(t *testing.T) {
    26  	or := &orderedRequests{}
    27  	or.empty()
    28  
    29  	r1 := createPbftReq(2, 1)
    30  	r2 := createPbftReq(2, 2)
    31  	r3 := createPbftReq(19, 1)
    32  	if or.has(or.wrapRequest(r1).key) {
    33  		t.Errorf("should not have req")
    34  	}
    35  	or.add(r1)
    36  	if !or.has(or.wrapRequest(r1).key) {
    37  		t.Errorf("should have req")
    38  	}
    39  	if or.has(or.wrapRequest(r2).key) {
    40  		t.Errorf("should not have req")
    41  	}
    42  	if or.remove(r2) {
    43  		t.Errorf("should not have removed req")
    44  	}
    45  	if !or.remove(r1) {
    46  		t.Errorf("should have removed req")
    47  	}
    48  	if or.remove(r1) {
    49  		t.Errorf("should not have removed req")
    50  	}
    51  	if or.order.Len() != 0 || len(or.presence) != 0 {
    52  		t.Errorf("should have 0 len")
    53  	}
    54  	or.adds(&message.RequestBatch{r1, r2, r3})
    55  
    56  	if or.order.Back().Value.(requestContainer).req != r3 {
    57  		t.Errorf("incorrect order")
    58  	}
    59  }
    60  
    61  func BenchmarkOrderedRequests(b *testing.B) {
    62  	or := &orderedRequests{}
    63  	or.empty()
    64  
    65  	Nreq := 1000
    66  
    67  	reqs := make(map[pbftTypes.MessageDigest]*message.Request)
    68  	for i := 0; i < Nreq; i++ {
    69  		rc := or.wrapRequest(createPbftReq(int64(i), 0))
    70  		reqs[rc.key] = rc.req
    71  	}
    72  	b.ResetTimer()
    73  
    74  	for i := 0; i < b.N; i++ {
    75  		for _, r := range reqs {
    76  			or.add(r)
    77  		}
    78  
    79  		for k := range reqs {
    80  			_ = or.has(k)
    81  		}
    82  
    83  		for _, r := range reqs {
    84  			or.remove(r)
    85  		}
    86  	}
    87  }