github.com/turingchain2020/turingchain@v1.1.21/system/mempool/simplequeue_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 mempool
     6  
     7  import (
     8  	"testing"
     9  
    10  	"github.com/turingchain2020/turingchain/types"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestCache(t *testing.T) {
    15  	subConfig := SubConfig{1, 100000}
    16  	cache := NewSimpleQueue(subConfig)
    17  	tx := &types.Transaction{Payload: []byte("123")}
    18  	hash := string(tx.Hash())
    19  	assert.Equal(t, false, cache.Exist(hash))
    20  	item1 := &Item{Value: tx, Priority: tx.Fee, EnterTime: types.Now().Unix()}
    21  	err := cache.Push(item1)
    22  	assert.Nil(t, err)
    23  	assert.Equal(t, true, cache.Exist(hash))
    24  	it, err := cache.GetItem(hash)
    25  	assert.Nil(t, err)
    26  	assert.Equal(t, item1, it)
    27  
    28  	_, err = cache.GetItem(hash + ":")
    29  	assert.Equal(t, types.ErrNotFound, err)
    30  
    31  	err = cache.Push(item1)
    32  	assert.Equal(t, types.ErrTxExist, err)
    33  
    34  	tx2 := &types.Transaction{Payload: []byte("1234")}
    35  	item2 := &Item{Value: tx2, Priority: tx.Fee, EnterTime: types.Now().Unix()}
    36  	err = cache.Push(item2)
    37  	assert.Equal(t, types.ErrMemFull, err)
    38  
    39  	cache.Remove(hash)
    40  	assert.Equal(t, 0, cache.Size())
    41  	//push to item
    42  	subConfig = SubConfig{2, 100000}
    43  	cache = NewSimpleQueue(subConfig)
    44  	cache.Push(item1)
    45  	cache.Push(item2)
    46  	assert.Equal(t, 2, cache.Size())
    47  	var data [2]*Item
    48  	i := 0
    49  	cache.Walk(1, func(value *Item) bool {
    50  		data[i] = value
    51  		i++
    52  		return true
    53  	})
    54  	assert.Equal(t, 1, i)
    55  	assert.Equal(t, data[0], item1)
    56  
    57  	i = 0
    58  	cache.Walk(2, func(value *Item) bool {
    59  		data[i] = value
    60  		i++
    61  		return true
    62  	})
    63  	assert.Equal(t, 2, i)
    64  	assert.Equal(t, data[0], item1)
    65  	assert.Equal(t, data[1], item2)
    66  
    67  	i = 0
    68  	cache.Walk(2, func(value *Item) bool {
    69  		data[i] = value
    70  		i++
    71  		return false
    72  	})
    73  	assert.Equal(t, 1, i)
    74  
    75  	//test timeline GetProperFee
    76  	assert.Equal(t, int64(100000), cache.GetProperFee())
    77  }