github.com/sixexorg/magnetic-ring@v0.0.0-20191119090307-31705a21e419/p2pserver/sync/conn/conn_test.go (about)

     1  /*
     2   * Copyright (C) 2018 The ontology Authors
     3   * This file is part of The ontology library.
     4   *
     5   * The ontology is free software: you can redistribute it and/or modify
     6   * it under the terms of the GNU Lesser General Public License as published by
     7   * the Free Software Foundation, either version 3 of the License, or
     8   * (at your option) any later version.
     9   *
    10   * The ontology is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU Lesser General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU Lesser General Public License
    16   * along with The ontology.  If not, see <http://www.gnu.org/licenses/>.
    17   */
    18  
    19  package conn
    20  
    21  import (
    22  	"bytes"
    23  	"math/rand"
    24  	"testing"
    25  	"time"
    26  
    27  	"github.com/ontio/ontology-crypto/keypair"
    28  	"github.com/ontio/ontology/account"
    29  	comm "github.com/ontio/ontology/common"
    30  	"github.com/ontio/ontology/common/log"
    31  	// "github.com/ontio/ontology/core/payload"
    32  	ct "github.com/ontio/ontology/core/types"
    33  	"github.com/sixexorg/magnetic-ring/p2pserver/common"
    34  	mt "github.com/sixexorg/magnetic-ring/p2pserver/message/types"
    35  	"github.com/stretchr/testify/assert"
    36  )
    37  
    38  var (
    39  	cliLink    *Link
    40  	serverLink *Link
    41  	cliChan    chan *mt.MsgPayload
    42  	serverChan chan *mt.MsgPayload
    43  	cliAddr    string
    44  	serAddr    string
    45  )
    46  
    47  func init() {
    48  	log.Init(log.Stdout)
    49  
    50  	cliLink = NewLink()
    51  	serverLink = NewLink()
    52  
    53  	cliLink.id = 0x733936
    54  	serverLink.id = 0x8274950
    55  
    56  	cliLink.port = 50338
    57  	serverLink.port = 50339
    58  
    59  	cliChan = make(chan *mt.MsgPayload, 100)
    60  	serverChan = make(chan *mt.MsgPayload, 100)
    61  	//listen ip addr
    62  	cliAddr = "127.0.0.1:50338"
    63  	serAddr = "127.0.0.1:50339"
    64  
    65  }
    66  
    67  func TestNewLink(t *testing.T) {
    68  
    69  	id := 0x74936295
    70  	port := 40339
    71  
    72  	if cliLink.GetID() != 0x733936 {
    73  		t.Fatal("link GetID failed")
    74  	}
    75  
    76  	cliLink.SetID(uint64(id))
    77  	if cliLink.GetID() != uint64(id) {
    78  		t.Fatal("link SetID failed")
    79  	}
    80  
    81  	if cliLink.GetPort() != 50338 {
    82  		t.Fatal("link GetPort failed")
    83  	}
    84  
    85  	cliLink.SetPort(uint16(port))
    86  	if cliLink.GetPort() != uint16(port) {
    87  		t.Fatal("link SetPort failed")
    88  	}
    89  
    90  	cliLink.SetChan(cliChan)
    91  	serverLink.SetChan(serverChan)
    92  
    93  	cliLink.UpdateRXTime(time.Now())
    94  
    95  	msg := &mt.MsgPayload{
    96  		Id:      cliLink.id,
    97  		Addr:    cliLink.addr,
    98  		Payload: &mt.NotFound{comm.UINT256_EMPTY},
    99  	}
   100  	go func() {
   101  		time.Sleep(5000000)
   102  		cliChan <- msg
   103  	}()
   104  
   105  	timeout := time.NewTimer(time.Second)
   106  	select {
   107  	case <-cliLink.recvChan:
   108  		t.Log("read data from channel")
   109  	case <-timeout.C:
   110  		timeout.Stop()
   111  		t.Fatal("can`t read data from link channel")
   112  	}
   113  
   114  }
   115  
   116  func TestUnpackBufNode(t *testing.T) {
   117  	cliLink.SetChan(cliChan)
   118  
   119  	msgType := "block"
   120  
   121  	var msg mt.Message
   122  
   123  	switch msgType {
   124  	case "addr":
   125  		var newaddrs []common.PeerAddr
   126  		for i := 0; i < 10000000; i++ {
   127  			newaddrs = append(newaddrs, common.PeerAddr{
   128  				Time: uint64(time.Now().Unix()),
   129  				ID:   uint64(i),
   130  			})
   131  		}
   132  		var addr mt.Addr
   133  		addr.NodeAddrs = newaddrs
   134  		msg = &addr
   135  	case "consensuspayload":
   136  		acct := account.NewAccount("SHA256withECDSA")
   137  		key := acct.PubKey()
   138  		payload := mt.ConsensusPayload{
   139  			Owner: key,
   140  		}
   141  		for i := 0; uint32(i) < 200000000; i++ {
   142  			byteInt := rand.Intn(256)
   143  			payload.Data = append(payload.Data, byte(byteInt))
   144  		}
   145  
   146  		msg = &mt.Consensus{payload}
   147  	case "consensus":
   148  		acct := account.NewAccount("SHA256withECDSA")
   149  		key := acct.PubKey()
   150  		payload := &mt.ConsensusPayload{
   151  			Owner: key,
   152  		}
   153  		for i := 0; uint32(i) < 200000000; i++ {
   154  			byteInt := rand.Intn(256)
   155  			payload.Data = append(payload.Data, byte(byteInt))
   156  		}
   157  		consensus := mt.Consensus{
   158  			Cons: *payload,
   159  		}
   160  		msg = &consensus
   161  	case "blkheader":
   162  		var headers []*ct.Header
   163  		blkHeader := &mt.BlkHeader{}
   164  		for i := 0; uint32(i) < 100000000; i++ {
   165  			header := &ct.Header{}
   166  			header.Height = uint32(i)
   167  			header.Bookkeepers = make([]keypair.PublicKey, 0)
   168  			header.SigData = make([][]byte, 0)
   169  			headers = append(headers, header)
   170  		}
   171  		blkHeader.BlkHdr = headers
   172  		msg = blkHeader
   173  	case "tx":
   174  		// var tx ct.Transaction
   175  		// trn := &mt.Trn{}
   176  		// sig := ct.Sig{}
   177  		// sigCnt := 100000000
   178  		// for i := 0; i < sigCnt; i++ {
   179  		// 	data := [][]byte{
   180  		// 		{byte(i)},
   181  		// 	}
   182  		// 	sig.SigData = append(sig.SigData, data...)
   183  		// }
   184  		// sigs := [1]*ct.Sig{&sig}
   185  		// tx.Payload = new(payload.DeployCode)
   186  		// tx.Sigs = sigs[:]
   187  		// trn.Txn = &tx
   188  		// msg = trn
   189  	case "block":
   190  		// var blk ct.Block
   191  		// mBlk := &mt.Block{}
   192  		// var txs []*ct.Transaction
   193  		// header := ct.Header{}
   194  		// header.Height = uint32(1)
   195  		// header.Bookkeepers = make([]keypair.PublicKey, 0)
   196  		// header.SigData = make([][]byte, 0)
   197  		// blk.Header = &header
   198  
   199  		// for i := 0; i < 2400000; i++ {
   200  		// 	var tx ct.Transaction
   201  		// 	sig := ct.Sig{}
   202  		// 	sig.SigData = append(sig.SigData, [][]byte{
   203  		// 		{byte(1)},
   204  		// 	}...)
   205  		// 	sigs := [1]*ct.Sig{&sig}
   206  		// 	tx.Payload = new(payload.DeployCode)
   207  		// 	tx.Sigs = sigs[:]
   208  		// 	txs = append(txs, &tx)
   209  		// }
   210  
   211  		// blk.Transactions = txs
   212  		// mBlk.Blk = &blk
   213  
   214  		// msg = mBlk
   215  	}
   216  
   217  	buf := bytes.NewBuffer(nil)
   218  	sink := comm.NewZeroCopySink(nil)
   219  	err := mt.WriteMessage(sink, msg)
   220  	assert.Nil(t, err)
   221  
   222  	demsg, _, err := mt.ReadMessage(buf)
   223  	assert.Nil(t, demsg)
   224  	assert.NotNil(t, err)
   225  }