github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/network/p2p/simulations/mocker_test.go (about)

     1  package simulations
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/http"
     6  	"net/url"
     7  	"strconv"
     8  	"sync"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/neatlab/neatio/network/p2p/discover"
    13  )
    14  
    15  func TestMocker(t *testing.T) {
    16  	_, s := testHTTPServer(t)
    17  	defer s.Close()
    18  
    19  	client := NewClient(s.URL)
    20  
    21  	err := client.StartNetwork()
    22  	if err != nil {
    23  		t.Fatalf("Could not start test network: %s", err)
    24  	}
    25  	defer func() {
    26  		err = client.StopNetwork()
    27  		if err != nil {
    28  			t.Fatalf("Could not stop test network: %s", err)
    29  		}
    30  	}()
    31  
    32  	resp, err := http.Get(s.URL + "/mocker")
    33  	if err != nil {
    34  		t.Fatalf("Could not get mocker list: %s", err)
    35  	}
    36  	defer resp.Body.Close()
    37  
    38  	if resp.StatusCode != 200 {
    39  		t.Fatalf("Invalid Status Code received, expected 200, got %d", resp.StatusCode)
    40  	}
    41  
    42  	var mockerlist []string
    43  	err = json.NewDecoder(resp.Body).Decode(&mockerlist)
    44  	if err != nil {
    45  		t.Fatalf("Error decoding JSON mockerlist: %s", err)
    46  	}
    47  
    48  	if len(mockerlist) < 1 {
    49  		t.Fatalf("No mockers available")
    50  	}
    51  
    52  	nodeCount := 10
    53  	var wg sync.WaitGroup
    54  
    55  	events := make(chan *Event, 10)
    56  	var opts SubscribeOpts
    57  	sub, err := client.SubscribeNetwork(events, opts)
    58  	defer sub.Unsubscribe()
    59  	nodemap := make(map[discover.NodeID]bool)
    60  	wg.Add(1)
    61  	nodesComplete := false
    62  	connCount := 0
    63  	go func() {
    64  		for {
    65  			select {
    66  			case event := <-events:
    67  				if event.Node != nil && event.Node.Up {
    68  					nodemap[event.Node.Config.ID] = true
    69  					if len(nodemap) == nodeCount {
    70  						nodesComplete = true
    71  					}
    72  				} else if event.Conn != nil && nodesComplete {
    73  					connCount += 1
    74  					if connCount == (nodeCount-1)*2 {
    75  						wg.Done()
    76  						return
    77  					}
    78  				}
    79  			case <-time.After(30 * time.Second):
    80  				wg.Done()
    81  				t.Fatalf("Timeout waiting for nodes being started up!")
    82  			}
    83  		}
    84  	}()
    85  
    86  	mockertype := mockerlist[len(mockerlist)-1]
    87  	for _, m := range mockerlist {
    88  		if m == "probabilistic" {
    89  			mockertype = m
    90  			break
    91  		}
    92  	}
    93  	resp, err = http.PostForm(s.URL+"/mocker/start", url.Values{"mocker-type": {mockertype}, "node-count": {strconv.Itoa(nodeCount)}})
    94  	if err != nil {
    95  		t.Fatalf("Could not start mocker: %s", err)
    96  	}
    97  	if resp.StatusCode != 200 {
    98  		t.Fatalf("Invalid Status Code received for starting mocker, expected 200, got %d", resp.StatusCode)
    99  	}
   100  
   101  	wg.Wait()
   102  
   103  	nodes_info, err := client.GetNodes()
   104  	if err != nil {
   105  		t.Fatalf("Could not get nodes list: %s", err)
   106  	}
   107  
   108  	if len(nodes_info) != nodeCount {
   109  		t.Fatalf("Expected %d number of nodes, got: %d", nodeCount, len(nodes_info))
   110  	}
   111  
   112  	resp, err = http.Post(s.URL+"/mocker/stop", "", nil)
   113  	if err != nil {
   114  		t.Fatalf("Could not stop mocker: %s", err)
   115  	}
   116  	if resp.StatusCode != 200 {
   117  		t.Fatalf("Invalid Status Code received for stopping mocker, expected 200, got %d", resp.StatusCode)
   118  	}
   119  
   120  	_, err = http.Post(s.URL+"/reset", "", nil)
   121  	if err != nil {
   122  		t.Fatalf("Could not reset network: %s", err)
   123  	}
   124  
   125  	nodes_info, err = client.GetNodes()
   126  	if err != nil {
   127  		t.Fatalf("Could not get nodes list: %s", err)
   128  	}
   129  
   130  	if len(nodes_info) != 0 {
   131  		t.Fatalf("Expected empty list of nodes, got: %d", len(nodes_info))
   132  	}
   133  }