github.com/Unheilbar/quorum@v1.0.0/p2p/simulations/adapters/inproc_test.go (about)

     1  // Copyright 2018 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package adapters
    18  
    19  import (
    20  	"bytes"
    21  	"encoding/binary"
    22  	"fmt"
    23  	"sync"
    24  	"testing"
    25  
    26  	"github.com/ethereum/go-ethereum/p2p/simulations/pipes"
    27  )
    28  
    29  func TestTCPPipe(t *testing.T) {
    30  	c1, c2, err := pipes.TCPPipe()
    31  	if err != nil {
    32  		t.Fatal(err)
    33  	}
    34  
    35  	msgs := 50
    36  	size := 1024
    37  	for i := 0; i < msgs; i++ {
    38  		msg := make([]byte, size)
    39  		binary.PutUvarint(msg, uint64(i))
    40  		if _, err := c1.Write(msg); err != nil {
    41  			t.Fatal(err)
    42  		}
    43  	}
    44  
    45  	for i := 0; i < msgs; i++ {
    46  		msg := make([]byte, size)
    47  		binary.PutUvarint(msg, uint64(i))
    48  		out := make([]byte, size)
    49  		if _, err := c2.Read(out); err != nil {
    50  			t.Fatal(err)
    51  		}
    52  		if !bytes.Equal(msg, out) {
    53  			t.Fatalf("expected %#v, got %#v", msg, out)
    54  		}
    55  	}
    56  }
    57  
    58  func TestTCPPipeBidirections(t *testing.T) {
    59  	c1, c2, err := pipes.TCPPipe()
    60  	if err != nil {
    61  		t.Fatal(err)
    62  	}
    63  
    64  	msgs := 50
    65  	size := 7
    66  	for i := 0; i < msgs; i++ {
    67  		msg := []byte(fmt.Sprintf("ping %02d", i))
    68  		if _, err := c1.Write(msg); err != nil {
    69  			t.Fatal(err)
    70  		}
    71  	}
    72  
    73  	for i := 0; i < msgs; i++ {
    74  		expected := []byte(fmt.Sprintf("ping %02d", i))
    75  		out := make([]byte, size)
    76  		if _, err := c2.Read(out); err != nil {
    77  			t.Fatal(err)
    78  		}
    79  
    80  		if !bytes.Equal(expected, out) {
    81  			t.Fatalf("expected %#v, got %#v", out, expected)
    82  		} else {
    83  			msg := []byte(fmt.Sprintf("pong %02d", i))
    84  			if _, err := c2.Write(msg); err != nil {
    85  				t.Fatal(err)
    86  			}
    87  		}
    88  	}
    89  
    90  	for i := 0; i < msgs; i++ {
    91  		expected := []byte(fmt.Sprintf("pong %02d", i))
    92  		out := make([]byte, size)
    93  		if _, err := c1.Read(out); err != nil {
    94  			t.Fatal(err)
    95  		}
    96  		if !bytes.Equal(expected, out) {
    97  			t.Fatalf("expected %#v, got %#v", out, expected)
    98  		}
    99  	}
   100  }
   101  
   102  func TestNetPipe(t *testing.T) {
   103  	c1, c2, err := pipes.NetPipe()
   104  	if err != nil {
   105  		t.Fatal(err)
   106  	}
   107  
   108  	msgs := 50
   109  	size := 1024
   110  	var wg sync.WaitGroup
   111  	defer wg.Wait()
   112  
   113  	// netPipe is blocking, so writes are emitted asynchronously
   114  	wg.Add(1)
   115  	go func() {
   116  		defer wg.Done()
   117  
   118  		for i := 0; i < msgs; i++ {
   119  			msg := make([]byte, size)
   120  			binary.PutUvarint(msg, uint64(i))
   121  			if _, err := c1.Write(msg); err != nil {
   122  				t.Error(err)
   123  			}
   124  		}
   125  	}()
   126  
   127  	for i := 0; i < msgs; i++ {
   128  		msg := make([]byte, size)
   129  		binary.PutUvarint(msg, uint64(i))
   130  		out := make([]byte, size)
   131  		if _, err := c2.Read(out); err != nil {
   132  			t.Error(err)
   133  		}
   134  		if !bytes.Equal(msg, out) {
   135  			t.Errorf("expected %#v, got %#v", msg, out)
   136  		}
   137  	}
   138  }
   139  
   140  func TestNetPipeBidirections(t *testing.T) {
   141  	c1, c2, err := pipes.NetPipe()
   142  	if err != nil {
   143  		t.Fatal(err)
   144  	}
   145  
   146  	msgs := 1000
   147  	size := 8
   148  	pingTemplate := "ping %03d"
   149  	pongTemplate := "pong %03d"
   150  	var wg sync.WaitGroup
   151  	defer wg.Wait()
   152  
   153  	// netPipe is blocking, so writes are emitted asynchronously
   154  	wg.Add(1)
   155  	go func() {
   156  		defer wg.Done()
   157  
   158  		for i := 0; i < msgs; i++ {
   159  			msg := []byte(fmt.Sprintf(pingTemplate, i))
   160  			if _, err := c1.Write(msg); err != nil {
   161  				t.Error(err)
   162  			}
   163  		}
   164  	}()
   165  
   166  	// netPipe is blocking, so reads for pong are emitted asynchronously
   167  	wg.Add(1)
   168  	go func() {
   169  		defer wg.Done()
   170  
   171  		for i := 0; i < msgs; i++ {
   172  			expected := []byte(fmt.Sprintf(pongTemplate, i))
   173  			out := make([]byte, size)
   174  			if _, err := c1.Read(out); err != nil {
   175  				t.Error(err)
   176  			}
   177  			if !bytes.Equal(expected, out) {
   178  				t.Errorf("expected %#v, got %#v", expected, out)
   179  			}
   180  		}
   181  	}()
   182  
   183  	// expect to read pings, and respond with pongs to the alternate connection
   184  	for i := 0; i < msgs; i++ {
   185  		expected := []byte(fmt.Sprintf(pingTemplate, i))
   186  
   187  		out := make([]byte, size)
   188  		_, err := c2.Read(out)
   189  		if err != nil {
   190  			t.Fatal(err)
   191  		}
   192  
   193  		if !bytes.Equal(expected, out) {
   194  			t.Errorf("expected %#v, got %#v", expected, out)
   195  		} else {
   196  			msg := []byte(fmt.Sprintf(pongTemplate, i))
   197  			if _, err := c2.Write(msg); err != nil {
   198  				t.Fatal(err)
   199  			}
   200  		}
   201  	}
   202  }