message_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Copyright 2014 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. package p2p
  17. import (
  18. "bytes"
  19. "encoding/hex"
  20. "fmt"
  21. "io"
  22. "runtime"
  23. "strings"
  24. "testing"
  25. "time"
  26. )
  27. func ExampleMsgPipe() {
  28. rw1, rw2 := MsgPipe()
  29. go func() {
  30. Send(rw1, 8, [][]byte{{0, 0}})
  31. Send(rw1, 5, [][]byte{{1, 1}})
  32. rw1.Close()
  33. }()
  34. for {
  35. msg, err := rw2.ReadMsg()
  36. if err != nil {
  37. break
  38. }
  39. var data [][]byte
  40. msg.Decode(&data)
  41. fmt.Printf("msg: %d, %x\n", msg.Code, data[0])
  42. }
  43. // Output:
  44. // msg: 8, 0000
  45. // msg: 5, 0101
  46. }
  47. func TestMsgPipeUnblockWrite(t *testing.T) {
  48. loop:
  49. for i := 0; i < 100; i++ {
  50. rw1, rw2 := MsgPipe()
  51. done := make(chan struct{})
  52. go func() {
  53. if err := SendItems(rw1, 1); err == nil {
  54. t.Error("EncodeMsg returned nil error")
  55. } else if err != ErrPipeClosed {
  56. t.Errorf("EncodeMsg returned wrong error: got %v, want %v", err, ErrPipeClosed)
  57. }
  58. close(done)
  59. }()
  60. // this call should ensure that EncodeMsg is waiting to
  61. // deliver sometimes. if this isn't done, Close is likely to
  62. // be executed before EncodeMsg starts and then we won't test
  63. // all the cases.
  64. runtime.Gosched()
  65. rw2.Close()
  66. select {
  67. case <-done:
  68. case <-time.After(200 * time.Millisecond):
  69. t.Errorf("write didn't unblock")
  70. break loop
  71. }
  72. }
  73. }
  74. // This test should panic if concurrent close isn't implemented correctly.
  75. func TestMsgPipeConcurrentClose(t *testing.T) {
  76. rw1, _ := MsgPipe()
  77. for i := 0; i < 10; i++ {
  78. go rw1.Close()
  79. }
  80. }
  81. func TestEOFSignal(t *testing.T) {
  82. rb := make([]byte, 10)
  83. // empty reader
  84. eof := make(chan struct{}, 1)
  85. sig := &eofSignal{new(bytes.Buffer), 0, eof}
  86. if n, err := sig.Read(rb); n != 0 || err != io.EOF {
  87. t.Errorf("Read returned unexpected values: (%v, %v)", n, err)
  88. }
  89. select {
  90. case <-eof:
  91. default:
  92. t.Error("EOF chan not signaled")
  93. }
  94. // count before error
  95. eof = make(chan struct{}, 1)
  96. sig = &eofSignal{bytes.NewBufferString("aaaaaaaa"), 4, eof}
  97. if n, err := sig.Read(rb); n != 4 || err != nil {
  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. // error before count
  106. eof = make(chan struct{}, 1)
  107. sig = &eofSignal{bytes.NewBufferString("aaaa"), 999, eof}
  108. if n, err := sig.Read(rb); n != 4 || err != nil {
  109. t.Errorf("Read returned unexpected values: (%v, %v)", n, err)
  110. }
  111. if n, err := sig.Read(rb); n != 0 || err != io.EOF {
  112. t.Errorf("Read returned unexpected values: (%v, %v)", n, err)
  113. }
  114. select {
  115. case <-eof:
  116. default:
  117. t.Error("EOF chan not signaled")
  118. }
  119. // no signal if neither occurs
  120. eof = make(chan struct{}, 1)
  121. sig = &eofSignal{bytes.NewBufferString("aaaaaaaaaaaaaaaaaaaaa"), 999, eof}
  122. if n, err := sig.Read(rb); n != 10 || err != nil {
  123. t.Errorf("Read returned unexpected values: (%v, %v)", n, err)
  124. }
  125. select {
  126. case <-eof:
  127. t.Error("unexpected EOF signal")
  128. default:
  129. }
  130. }
  131. func unhex(str string) []byte {
  132. r := strings.NewReplacer("\t", "", " ", "", "\n", "")
  133. b, err := hex.DecodeString(r.Replace(str))
  134. if err != nil {
  135. panic(fmt.Sprintf("invalid hex string: %q", str))
  136. }
  137. return b
  138. }