github.com/karalabe/go-ethereum@v0.8.5/p2p/message_test.go (about)

     1  package p2p
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io"
     7  	"io/ioutil"
     8  	"runtime"
     9  	"testing"
    10  	"time"
    11  )
    12  
    13  func TestNewMsg(t *testing.T) {
    14  	msg := NewMsg(3, 1, "000")
    15  	if msg.Code != 3 {
    16  		t.Errorf("incorrect code %d, want %d", msg.Code)
    17  	}
    18  	if msg.Size != 5 {
    19  		t.Errorf("incorrect size %d, want %d", msg.Size, 5)
    20  	}
    21  	pl, _ := ioutil.ReadAll(msg.Payload)
    22  	expect := []byte{0x01, 0x83, 0x30, 0x30, 0x30}
    23  	if !bytes.Equal(pl, expect) {
    24  		t.Errorf("incorrect payload content, got %x, want %x", pl, expect)
    25  	}
    26  }
    27  
    28  // func TestEncodeDecodeMsg(t *testing.T) {
    29  // 	msg := NewMsg(3, 1, "000")
    30  // 	buf := new(bytes.Buffer)
    31  // 	if err := writeMsg(buf, msg); err != nil {
    32  // 		t.Fatalf("encodeMsg error: %v", err)
    33  // 	}
    34  // 	// t.Logf("encoded: %x", buf.Bytes())
    35  
    36  // 	decmsg, err := readMsg(buf)
    37  // 	if err != nil {
    38  // 		t.Fatalf("readMsg error: %v", err)
    39  // 	}
    40  // 	if decmsg.Code != 3 {
    41  // 		t.Errorf("incorrect code %d, want %d", decmsg.Code, 3)
    42  // 	}
    43  // 	if decmsg.Size != 5 {
    44  // 		t.Errorf("incorrect size %d, want %d", decmsg.Size, 5)
    45  // 	}
    46  
    47  // 	var data struct {
    48  // 		I uint
    49  // 		S string
    50  // 	}
    51  // 	if err := decmsg.Decode(&data); err != nil {
    52  // 		t.Fatalf("Decode error: %v", err)
    53  // 	}
    54  // 	if data.I != 1 {
    55  // 		t.Errorf("incorrect data.I: got %v, expected %d", data.I, 1)
    56  // 	}
    57  // 	if data.S != "000" {
    58  // 		t.Errorf("incorrect data.S: got %q, expected %q", data.S, "000")
    59  // 	}
    60  // }
    61  
    62  // func TestDecodeRealMsg(t *testing.T) {
    63  // 	data := ethutil.Hex2Bytes("2240089100000080f87e8002b5457468657265756d282b2b292f5065657220536572766572204f6e652f76302e372e382f52656c656173652f4c696e75782f672b2bc082765fb84086dd80b7aefd6a6d2e3b93f4f300a86bfb6ef7bdc97cb03f793db6bb")
    64  // 	msg, err := readMsg(bytes.NewReader(data))
    65  // 	if err != nil {
    66  // 		t.Fatalf("unexpected error: %v", err)
    67  // 	}
    68  
    69  // 	if msg.Code != 0 {
    70  // 		t.Errorf("incorrect code %d, want %d", msg.Code, 0)
    71  // 	}
    72  // }
    73  
    74  func ExampleMsgPipe() {
    75  	rw1, rw2 := MsgPipe()
    76  	go func() {
    77  		EncodeMsg(rw1, 8, []byte{0, 0})
    78  		EncodeMsg(rw1, 5, []byte{1, 1})
    79  		rw1.Close()
    80  	}()
    81  
    82  	for {
    83  		msg, err := rw2.ReadMsg()
    84  		if err != nil {
    85  			break
    86  		}
    87  		var data [1][]byte
    88  		msg.Decode(&data)
    89  		fmt.Printf("msg: %d, %x\n", msg.Code, data[0])
    90  	}
    91  	// Output:
    92  	// msg: 8, 0000
    93  	// msg: 5, 0101
    94  }
    95  
    96  func TestMsgPipeUnblockWrite(t *testing.T) {
    97  loop:
    98  	for i := 0; i < 100; i++ {
    99  		rw1, rw2 := MsgPipe()
   100  		done := make(chan struct{})
   101  		go func() {
   102  			if err := EncodeMsg(rw1, 1); err == nil {
   103  				t.Error("EncodeMsg returned nil error")
   104  			} else if err != ErrPipeClosed {
   105  				t.Error("EncodeMsg returned wrong error: got %v, want %v", err, ErrPipeClosed)
   106  			}
   107  			close(done)
   108  		}()
   109  
   110  		// this call should ensure that EncodeMsg is waiting to
   111  		// deliver sometimes. if this isn't done, Close is likely to
   112  		// be executed before EncodeMsg starts and then we won't test
   113  		// all the cases.
   114  		runtime.Gosched()
   115  
   116  		rw2.Close()
   117  		select {
   118  		case <-done:
   119  		case <-time.After(200 * time.Millisecond):
   120  			t.Errorf("write didn't unblock")
   121  			break loop
   122  		}
   123  	}
   124  }
   125  
   126  // This test should panic if concurrent close isn't implemented correctly.
   127  func TestMsgPipeConcurrentClose(t *testing.T) {
   128  	rw1, _ := MsgPipe()
   129  	for i := 0; i < 10; i++ {
   130  		go rw1.Close()
   131  	}
   132  }
   133  
   134  func TestEOFSignal(t *testing.T) {
   135  	rb := make([]byte, 10)
   136  
   137  	// empty reader
   138  	eof := make(chan struct{}, 1)
   139  	sig := &eofSignal{new(bytes.Buffer), 0, eof}
   140  	if n, err := sig.Read(rb); n != 0 || err != io.EOF {
   141  		t.Errorf("Read returned unexpected values: (%v, %v)", n, err)
   142  	}
   143  	select {
   144  	case <-eof:
   145  	default:
   146  		t.Error("EOF chan not signaled")
   147  	}
   148  
   149  	// count before error
   150  	eof = make(chan struct{}, 1)
   151  	sig = &eofSignal{bytes.NewBufferString("aaaaaaaa"), 4, eof}
   152  	if n, err := sig.Read(rb); n != 4 || err != nil {
   153  		t.Errorf("Read returned unexpected values: (%v, %v)", n, err)
   154  	}
   155  	select {
   156  	case <-eof:
   157  	default:
   158  		t.Error("EOF chan not signaled")
   159  	}
   160  
   161  	// error before count
   162  	eof = make(chan struct{}, 1)
   163  	sig = &eofSignal{bytes.NewBufferString("aaaa"), 999, eof}
   164  	if n, err := sig.Read(rb); n != 4 || err != nil {
   165  		t.Errorf("Read returned unexpected values: (%v, %v)", n, err)
   166  	}
   167  	if n, err := sig.Read(rb); n != 0 || err != io.EOF {
   168  		t.Errorf("Read returned unexpected values: (%v, %v)", n, err)
   169  	}
   170  	select {
   171  	case <-eof:
   172  	default:
   173  		t.Error("EOF chan not signaled")
   174  	}
   175  
   176  	// no signal if neither occurs
   177  	eof = make(chan struct{}, 1)
   178  	sig = &eofSignal{bytes.NewBufferString("aaaaaaaaaaaaaaaaaaaaa"), 999, eof}
   179  	if n, err := sig.Read(rb); n != 10 || err != nil {
   180  		t.Errorf("Read returned unexpected values: (%v, %v)", n, err)
   181  	}
   182  	select {
   183  	case <-eof:
   184  		t.Error("unexpected EOF signal")
   185  	default:
   186  	}
   187  }