github.com/annchain/OG@v0.0.9/og/syncer/syncer_test.go (about)

     1  // Copyright © 2019 Annchain Authors <EMAIL ADDRESS>
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  package syncer
    15  
    16  import (
    17  	"fmt"
    18  	types2 "github.com/annchain/OG/arefactor/og/types"
    19  	"github.com/annchain/OG/common/crypto"
    20  	"github.com/annchain/OG/og/types/archive"
    21  	"github.com/annchain/OG/types"
    22  	"github.com/sirupsen/logrus"
    23  	"sync"
    24  	"testing"
    25  	"time"
    26  )
    27  
    28  func newTestIncrementalSyncer() *IncrementalSyncer {
    29  
    30  	isKnownHash := func(h types2.Hash) bool {
    31  		return false
    32  	}
    33  	newTxEnable := func() bool {
    34  		return true
    35  	}
    36  	hashOrder := func() types2.Hashes {
    37  		return nil
    38  	}
    39  	heighter := func() uint64 {
    40  		return 0
    41  	}
    42  	log = logrus.StandardLogger()
    43  	log.SetLevel(logrus.InfoLevel)
    44  	syncer := NewIncrementalSyncer(
    45  		&SyncerConfig{
    46  			BatchTimeoutMilliSecond:                  100,
    47  			AcquireTxQueueSize:                       1000,
    48  			MaxBatchSize:                             100,
    49  			AcquireTxDedupCacheMaxSize:               10000,
    50  			AcquireTxDedupCacheExpirationSeconds:     60,
    51  			BufferedIncomingTxCacheExpirationSeconds: 600,
    52  			BufferedIncomingTxCacheMaxSize:           80000,
    53  			FiredTxCacheExpirationSeconds:            600,
    54  			FiredTxCacheMaxSize:                      10000,
    55  		}, nil, hashOrder, isKnownHash,
    56  		heighter, newTxEnable)
    57  	syncer.Enabled = true
    58  	for i := 0; i < 5000; i++ {
    59  		tx := archive.RandomTx()
    60  		err := syncer.bufferedIncomingTxCache.EnQueue(tx)
    61  		if err != nil {
    62  			panic(err)
    63  		}
    64  	}
    65  	go syncer.txNotifyLoop()
    66  	return syncer
    67  
    68  }
    69  
    70  func stopIncSyncer(syncer *IncrementalSyncer) {
    71  	syncer.quitNotifyEvent <- true
    72  }
    73  
    74  func TestIncrementalSyncer_AddTxs(t *testing.T) {
    75  	syncer := newTestIncrementalSyncer()
    76  	defer stopIncSyncer(syncer)
    77  	var wg sync.WaitGroup
    78  	start := time.Now()
    79  	signer := crypto.NewSigner(crypto.CryptoTypeEd25519)
    80  	pubKey, _ := signer.RandomKeyPair()
    81  	types.Signer = signer
    82  	for i := 0; i < 60000; i++ {
    83  		tx := archive.RandomTx()
    84  		tx.PublicKey = pubKey.KeyBytes
    85  		msg := &archive.MessageNewTx{tx.RawTx()}
    86  		wg.Add(1)
    87  		go func() {
    88  			syncer.HandleNewTx(msg, "123")
    89  			wg.Done()
    90  		}()
    91  	}
    92  	wg.Wait()
    93  	fmt.Println("used", time.Now().Sub(start).String(), "len", syncer.bufferedIncomingTxCache.Len())
    94  }
    95  
    96  func TestSyncBuffer_AddTxs(t *testing.T) {
    97  	signer := crypto.NewSigner(crypto.CryptoTypeEd25519)
    98  	syncer := newTestIncrementalSyncer()
    99  	pubKey, _ := signer.RandomKeyPair()
   100  	types.Signer = signer
   101  	var wg sync.WaitGroup
   102  	for i := 0; i < 60000; i++ {
   103  		tx := archive.RandomTx()
   104  		tx.PublicKey = pubKey.KeyBytes
   105  		msg := &archive.MessageNewTx{tx.RawTx()}
   106  		wg.Add(1)
   107  		go func() {
   108  			syncer.HandleNewTx(msg, "123")
   109  			wg.Done()
   110  		}()
   111  	}
   112  }