github.com/neatio-net/neatio@v1.7.3-0.20231114194659-f4d7a2226baa/network/p2p/message_test.go (about)

     1  package p2p
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/hex"
     6  	"fmt"
     7  	"io"
     8  	"runtime"
     9  	"strings"
    10  	"testing"
    11  	"time"
    12  )
    13  
    14  func ExampleMsgPipe() {
    15  	rw1, rw2 := MsgPipe()
    16  	go func() {
    17  		Send(rw1, 8, [][]byte{{0, 0}})
    18  		Send(rw1, 5, [][]byte{{1, 1}})
    19  		rw1.Close()
    20  	}()
    21  
    22  	for {
    23  		msg, err := rw2.ReadMsg()
    24  		if err != nil {
    25  			break
    26  		}
    27  		var data [][]byte
    28  		msg.Decode(&data)
    29  		fmt.Printf("msg: %d, %x\n", msg.Code, data[0])
    30  	}
    31  
    32  }
    33  
    34  func TestMsgPipeUnblockWrite(t *testing.T) {
    35  loop:
    36  	for i := 0; i < 100; i++ {
    37  		rw1, rw2 := MsgPipe()
    38  		done := make(chan struct{})
    39  		go func() {
    40  			if err := SendItems(rw1, 1); err == nil {
    41  				t.Error("EncodeMsg returned nil error")
    42  			} else if err != ErrPipeClosed {
    43  				t.Errorf("EncodeMsg returned wrong error: got %v, want %v", err, ErrPipeClosed)
    44  			}
    45  			close(done)
    46  		}()
    47  
    48  		runtime.Gosched()
    49  
    50  		rw2.Close()
    51  		select {
    52  		case <-done:
    53  		case <-time.After(200 * time.Millisecond):
    54  			t.Errorf("write didn't unblock")
    55  			break loop
    56  		}
    57  	}
    58  }
    59  
    60  func TestMsgPipeConcurrentClose(t *testing.T) {
    61  	rw1, _ := MsgPipe()
    62  	for i := 0; i < 10; i++ {
    63  		go rw1.Close()
    64  	}
    65  }
    66  
    67  func TestEOFSignal(t *testing.T) {
    68  	rb := make([]byte, 10)
    69  
    70  	eof := make(chan struct{}, 1)
    71  	sig := &eofSignal{new(bytes.Buffer), 0, eof}
    72  	if n, err := sig.Read(rb); n != 0 || err != io.EOF {
    73  		t.Errorf("Read returned unexpected values: (%v, %v)", n, err)
    74  	}
    75  	select {
    76  	case <-eof:
    77  	default:
    78  		t.Error("EOF chan not signaled")
    79  	}
    80  
    81  	eof = make(chan struct{}, 1)
    82  	sig = &eofSignal{bytes.NewBufferString("aaaaaaaa"), 4, eof}
    83  	if n, err := sig.Read(rb); n != 4 || err != nil {
    84  		t.Errorf("Read returned unexpected values: (%v, %v)", n, err)
    85  	}
    86  	select {
    87  	case <-eof:
    88  	default:
    89  		t.Error("EOF chan not signaled")
    90  	}
    91  
    92  	eof = make(chan struct{}, 1)
    93  	sig = &eofSignal{bytes.NewBufferString("aaaa"), 999, eof}
    94  	if n, err := sig.Read(rb); n != 4 || err != nil {
    95  		t.Errorf("Read returned unexpected values: (%v, %v)", n, err)
    96  	}
    97  	if n, err := sig.Read(rb); n != 0 || err != io.EOF {
    98  		t.Errorf("Read returned unexpected values: (%v, %v)", n, err)
    99  	}
   100  	select {
   101  	case <-eof:
   102  	default:
   103  		t.Error("EOF chan not signaled")
   104  	}
   105  
   106  	eof = make(chan struct{}, 1)
   107  	sig = &eofSignal{bytes.NewBufferString("aaaaaaaaaaaaaaaaaaaaa"), 999, eof}
   108  	if n, err := sig.Read(rb); n != 10 || err != nil {
   109  		t.Errorf("Read returned unexpected values: (%v, %v)", n, err)
   110  	}
   111  	select {
   112  	case <-eof:
   113  		t.Error("unexpected EOF signal")
   114  	default:
   115  	}
   116  }
   117  
   118  func unhex(str string) []byte {
   119  	r := strings.NewReplacer("\t", "", " ", "", "\n", "")
   120  	b, err := hex.DecodeString(r.Replace(str))
   121  	if err != nil {
   122  		panic(fmt.Sprintf("invalid hex string: %q", str))
   123  	}
   124  	return b
   125  }