message_test.go 3.5 KB

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