github.com/codingfuture/orig-energi3@v0.8.4/p2p/simulations/adapters/inproc_test.go (about)

     1  // Copyright 2018 The Energi Core Authors
     2  // Copyright 2018 The go-ethereum Authors
     3  // This file is part of the Energi Core library.
     4  //
     5  // The Energi Core library 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 Energi Core library 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 Energi Core library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package adapters
    19  
    20  import (
    21  	"bytes"
    22  	"encoding/binary"
    23  	"fmt"
    24  	"testing"
    25  	"time"
    26  
    27  	"github.com/ethereum/go-ethereum/p2p/simulations/pipes"
    28  )
    29  
    30  func TestTCPPipe(t *testing.T) {
    31  	c1, c2, err := pipes.TCPPipe()
    32  	if err != nil {
    33  		t.Fatal(err)
    34  	}
    35  
    36  	done := make(chan struct{})
    37  
    38  	go func() {
    39  		msgs := 50
    40  		size := 1024
    41  		for i := 0; i < msgs; i++ {
    42  			msg := make([]byte, size)
    43  			_ = binary.PutUvarint(msg, uint64(i))
    44  
    45  			_, err := c1.Write(msg)
    46  			if err != nil {
    47  				t.Fatal(err)
    48  			}
    49  		}
    50  
    51  		for i := 0; i < msgs; i++ {
    52  			msg := make([]byte, size)
    53  			_ = binary.PutUvarint(msg, uint64(i))
    54  
    55  			out := make([]byte, size)
    56  			_, err := c2.Read(out)
    57  			if err != nil {
    58  				t.Fatal(err)
    59  			}
    60  
    61  			if !bytes.Equal(msg, out) {
    62  				t.Fatalf("expected %#v, got %#v", msg, out)
    63  			}
    64  		}
    65  		done <- struct{}{}
    66  	}()
    67  
    68  	select {
    69  	case <-done:
    70  	case <-time.After(5 * time.Second):
    71  		t.Fatal("test timeout")
    72  	}
    73  }
    74  
    75  func TestTCPPipeBidirections(t *testing.T) {
    76  	c1, c2, err := pipes.TCPPipe()
    77  	if err != nil {
    78  		t.Fatal(err)
    79  	}
    80  
    81  	done := make(chan struct{})
    82  
    83  	go func() {
    84  		msgs := 50
    85  		size := 7
    86  		for i := 0; i < msgs; i++ {
    87  			msg := []byte(fmt.Sprintf("ping %02d", i))
    88  
    89  			_, err := c1.Write(msg)
    90  			if err != nil {
    91  				t.Fatal(err)
    92  			}
    93  		}
    94  
    95  		for i := 0; i < msgs; i++ {
    96  			expected := []byte(fmt.Sprintf("ping %02d", i))
    97  
    98  			out := make([]byte, size)
    99  			_, err := c2.Read(out)
   100  			if err != nil {
   101  				t.Fatal(err)
   102  			}
   103  
   104  			if !bytes.Equal(expected, out) {
   105  				t.Fatalf("expected %#v, got %#v", out, expected)
   106  			} else {
   107  				msg := []byte(fmt.Sprintf("pong %02d", i))
   108  				_, err := c2.Write(msg)
   109  				if err != nil {
   110  					t.Fatal(err)
   111  				}
   112  			}
   113  		}
   114  
   115  		for i := 0; i < msgs; i++ {
   116  			expected := []byte(fmt.Sprintf("pong %02d", i))
   117  
   118  			out := make([]byte, size)
   119  			_, err := c1.Read(out)
   120  			if err != nil {
   121  				t.Fatal(err)
   122  			}
   123  
   124  			if !bytes.Equal(expected, out) {
   125  				t.Fatalf("expected %#v, got %#v", out, expected)
   126  			}
   127  		}
   128  		done <- struct{}{}
   129  	}()
   130  
   131  	select {
   132  	case <-done:
   133  	case <-time.After(5 * time.Second):
   134  		t.Fatal("test timeout")
   135  	}
   136  }
   137  
   138  func TestNetPipe(t *testing.T) {
   139  	c1, c2, err := pipes.NetPipe()
   140  	if err != nil {
   141  		t.Fatal(err)
   142  	}
   143  
   144  	done := make(chan struct{})
   145  
   146  	go func() {
   147  		msgs := 50
   148  		size := 1024
   149  		// netPipe is blocking, so writes are emitted asynchronously
   150  		go func() {
   151  			for i := 0; i < msgs; i++ {
   152  				msg := make([]byte, size)
   153  				_ = binary.PutUvarint(msg, uint64(i))
   154  
   155  				_, err := c1.Write(msg)
   156  				if err != nil {
   157  					t.Fatal(err)
   158  				}
   159  			}
   160  		}()
   161  
   162  		for i := 0; i < msgs; i++ {
   163  			msg := make([]byte, size)
   164  			_ = binary.PutUvarint(msg, uint64(i))
   165  
   166  			out := make([]byte, size)
   167  			_, err := c2.Read(out)
   168  			if err != nil {
   169  				t.Fatal(err)
   170  			}
   171  
   172  			if !bytes.Equal(msg, out) {
   173  				t.Fatalf("expected %#v, got %#v", msg, out)
   174  			}
   175  		}
   176  
   177  		done <- struct{}{}
   178  	}()
   179  
   180  	select {
   181  	case <-done:
   182  	case <-time.After(5 * time.Second):
   183  		t.Fatal("test timeout")
   184  	}
   185  }
   186  
   187  func TestNetPipeBidirections(t *testing.T) {
   188  	c1, c2, err := pipes.NetPipe()
   189  	if err != nil {
   190  		t.Fatal(err)
   191  	}
   192  
   193  	done := make(chan struct{})
   194  
   195  	go func() {
   196  		msgs := 1000
   197  		size := 8
   198  		pingTemplate := "ping %03d"
   199  		pongTemplate := "pong %03d"
   200  
   201  		// netPipe is blocking, so writes are emitted asynchronously
   202  		go func() {
   203  			for i := 0; i < msgs; i++ {
   204  				msg := []byte(fmt.Sprintf(pingTemplate, i))
   205  
   206  				_, err := c1.Write(msg)
   207  				if err != nil {
   208  					t.Fatal(err)
   209  				}
   210  			}
   211  		}()
   212  
   213  		// netPipe is blocking, so reads for pong are emitted asynchronously
   214  		go func() {
   215  			for i := 0; i < msgs; i++ {
   216  				expected := []byte(fmt.Sprintf(pongTemplate, i))
   217  
   218  				out := make([]byte, size)
   219  				_, err := c1.Read(out)
   220  				if err != nil {
   221  					t.Fatal(err)
   222  				}
   223  
   224  				if !bytes.Equal(expected, out) {
   225  					t.Fatalf("expected %#v, got %#v", expected, out)
   226  				}
   227  			}
   228  
   229  			done <- struct{}{}
   230  		}()
   231  
   232  		// expect to read pings, and respond with pongs to the alternate connection
   233  		for i := 0; i < msgs; i++ {
   234  			expected := []byte(fmt.Sprintf(pingTemplate, i))
   235  
   236  			out := make([]byte, size)
   237  			_, err := c2.Read(out)
   238  			if err != nil {
   239  				t.Fatal(err)
   240  			}
   241  
   242  			if !bytes.Equal(expected, out) {
   243  				t.Fatalf("expected %#v, got %#v", expected, out)
   244  			} else {
   245  				msg := []byte(fmt.Sprintf(pongTemplate, i))
   246  
   247  				_, err := c2.Write(msg)
   248  				if err != nil {
   249  					t.Fatal(err)
   250  				}
   251  			}
   252  		}
   253  	}()
   254  
   255  	select {
   256  	case <-done:
   257  	case <-time.After(5 * time.Second):
   258  		t.Fatal("test timeout")
   259  	}
   260  }