github.com/sixexorg/magnetic-ring@v0.0.0-20191119090307-31705a21e419/p2pserver/actor/server/actor_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 server
    20  
    21  import (
    22  	"fmt"
    23  	"testing"
    24  	"time"
    25  
    26  	"github.com/ontio/ontology/account"
    27  	"github.com/ontio/ontology/common/log"
    28  	"github.com/sixexorg/magnetic-ring/p2pserver"
    29  	"github.com/sixexorg/magnetic-ring/p2pserver/common"
    30  )
    31  
    32  func TestP2PActorServer(t *testing.T) {
    33  	log.Init(log.Stdout)
    34  	fmt.Println("Start test the p2pserver by actor...")
    35  
    36  	acct := account.NewAccount("SHA256withECDSA")
    37  	p2p, err := p2pserver.NewServer(acct)
    38  	if err != nil {
    39  		t.Fatalf("TestP2PActorServer: p2pserver NewServer error %s", err)
    40  	}
    41  
    42  	p2pActor := NewP2PActor(p2p)
    43  	p2pPID, err := p2pActor.Start()
    44  	if err != nil {
    45  		t.Fatalf("p2pActor init error %s", err)
    46  	}
    47  
    48  	//test server api
    49  
    50  	//false: disable sync,running without ledger
    51  	future := p2pPID.RequestFuture(&StartServerReq{StartSync: false}, common.ACTOR_TIMEOUT*time.Second)
    52  	result, err := future.Result()
    53  	if err != nil {
    54  		t.Fatalf("TestP2PActorServer: p2p start error %s", err)
    55  	}
    56  
    57  	future = p2pPID.RequestFuture(&GetConnectionCntReq{}, common.ACTOR_TIMEOUT*time.Second)
    58  	result, err = future.Result()
    59  	if err != nil {
    60  		t.Errorf("GetConnectionCntReq error %s", err)
    61  	}
    62  	_, ok := result.(*GetConnectionCntRsp)
    63  	if !ok {
    64  		t.Error("GetConnectionCntRsp error")
    65  	}
    66  
    67  	future = p2pPID.RequestFuture(&GetNeighborAddrsReq{}, common.ACTOR_TIMEOUT*time.Second)
    68  	result, err = future.Result()
    69  	if err != nil {
    70  		t.Errorf("GetNeighborAddrsReq error %s", err)
    71  	}
    72  	_, ok = result.(*GetNeighborAddrsRsp)
    73  	if !ok {
    74  		t.Error("GetNeighborAddrsRsp error")
    75  	}
    76  
    77  	future = p2pPID.RequestFuture(&GetConnectionStateReq{}, common.ACTOR_TIMEOUT*time.Second)
    78  	result, err = future.Result()
    79  	if err != nil {
    80  		t.Errorf("GetConnectionStateReq error %s", err)
    81  	}
    82  	_, ok = result.(*GetConnectionStateRsp)
    83  	if !ok {
    84  		t.Error("GetConnectionStateRsp error")
    85  	}
    86  
    87  	future = p2pPID.RequestFuture(&GetTimeReq{}, common.ACTOR_TIMEOUT*time.Second)
    88  	result, err = future.Result()
    89  	if err != nil {
    90  		t.Errorf("GetTimeReq error %s", err)
    91  	}
    92  	_, ok = result.(*GetTimeRsp)
    93  	if !ok {
    94  		t.Error("GetTimeRsp error")
    95  	}
    96  
    97  	future = p2pPID.RequestFuture(&GetPortReq{}, common.ACTOR_TIMEOUT*time.Second)
    98  	result, err = future.Result()
    99  	if err != nil {
   100  		t.Errorf("GetPortReq error %s", err)
   101  	}
   102  	_, ok = result.(*GetPortRsp)
   103  	if !ok {
   104  		t.Error("GetPortRsp error")
   105  	}
   106  
   107  	future = p2pPID.RequestFuture(&GetIdReq{}, common.ACTOR_TIMEOUT*time.Second)
   108  	result, err = future.Result()
   109  	if err != nil {
   110  		t.Errorf("GetIdReq error %s", err)
   111  	}
   112  	_, ok = result.(*GetIdRsp)
   113  	if !ok {
   114  		t.Error("GetIdRsp error")
   115  	}
   116  
   117  	future = p2pPID.RequestFuture(&GetRelayStateReq{}, common.ACTOR_TIMEOUT*time.Second)
   118  	result, err = future.Result()
   119  	if err != nil {
   120  		t.Errorf("GetRelayStateReq error %s", err)
   121  	}
   122  	_, ok = result.(*GetRelayStateRsp)
   123  	if !ok {
   124  		t.Error("GetRelayStateRsp error")
   125  	}
   126  
   127  	future = p2pPID.RequestFuture(&GetVersionReq{}, common.ACTOR_TIMEOUT*time.Second)
   128  	result, err = future.Result()
   129  	if err != nil {
   130  		t.Errorf("GetVersionReq error %s", err)
   131  	}
   132  	_, ok = result.(*GetVersionRsp)
   133  	if !ok {
   134  		t.Error("GetVersionRsp error")
   135  	}
   136  
   137  	future = p2pPID.RequestFuture(&GetNodeTypeReq{}, common.ACTOR_TIMEOUT*time.Second)
   138  	result, err = future.Result()
   139  	if err != nil {
   140  		t.Errorf("GetNodeTypeReq error %s", err)
   141  	}
   142  	_, ok = result.(*GetNodeTypeRsp)
   143  	if !ok {
   144  		t.Error("GetNodeTypeRsp error")
   145  	}
   146  
   147  	future = p2pPID.RequestFuture(&StopServerReq{}, common.ACTOR_TIMEOUT*time.Second)
   148  	result, err = future.Result()
   149  	if err != nil {
   150  		t.Fatalf("TestP2PActorServer: p2p halt error %s", err)
   151  	}
   152  
   153  }