pipeconns_test.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. package fasthttputil
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "net"
  7. "testing"
  8. "time"
  9. )
  10. func TestPipeConnsWriteTimeout(t *testing.T) {
  11. t.Parallel()
  12. pc := NewPipeConns()
  13. c1 := pc.Conn1()
  14. deadline := time.Now().Add(time.Millisecond)
  15. if err := c1.SetWriteDeadline(deadline); err != nil {
  16. t.Fatalf("unexpected error: %v", err)
  17. }
  18. data := []byte("foobar")
  19. for {
  20. _, err := c1.Write(data)
  21. if err != nil {
  22. if err == ErrTimeout {
  23. break
  24. }
  25. t.Fatalf("unexpected error: %v", err)
  26. }
  27. }
  28. for i := 0; i < 10; i++ {
  29. _, err := c1.Write(data)
  30. if err == nil {
  31. t.Fatalf("expecting error")
  32. }
  33. if err != ErrTimeout {
  34. t.Fatalf("unexpected error: %v. Expecting %v", err, ErrTimeout)
  35. }
  36. }
  37. // read the written data
  38. c2 := pc.Conn2()
  39. if err := c2.SetReadDeadline(time.Now().Add(10 * time.Millisecond)); err != nil {
  40. t.Fatalf("unexpected error: %v", err)
  41. }
  42. for {
  43. _, err := c2.Read(data)
  44. if err != nil {
  45. if err == ErrTimeout {
  46. break
  47. }
  48. t.Fatalf("unexpected error: %v", err)
  49. }
  50. }
  51. for i := 0; i < 10; i++ {
  52. _, err := c2.Read(data)
  53. if err == nil {
  54. t.Fatalf("expecting error")
  55. }
  56. if err != ErrTimeout {
  57. t.Fatalf("unexpected error: %v. Expecting %v", err, ErrTimeout)
  58. }
  59. }
  60. }
  61. func TestPipeConnsPositiveReadTimeout(t *testing.T) {
  62. t.Parallel()
  63. testPipeConnsReadTimeout(t, time.Millisecond)
  64. }
  65. func TestPipeConnsNegativeReadTimeout(t *testing.T) {
  66. t.Parallel()
  67. testPipeConnsReadTimeout(t, -time.Second)
  68. }
  69. var zeroTime time.Time
  70. func testPipeConnsReadTimeout(t *testing.T, timeout time.Duration) {
  71. pc := NewPipeConns()
  72. c1 := pc.Conn1()
  73. deadline := time.Now().Add(timeout)
  74. if err := c1.SetReadDeadline(deadline); err != nil {
  75. t.Fatalf("unexpected error: %v", err)
  76. }
  77. var buf [1]byte
  78. for i := 0; i < 10; i++ {
  79. _, err := c1.Read(buf[:])
  80. if err == nil {
  81. t.Fatalf("expecting error on iteration %d", i)
  82. }
  83. if err != ErrTimeout {
  84. t.Fatalf("unexpected error on iteration %d: %v. Expecting %v", i, err, ErrTimeout)
  85. }
  86. }
  87. // disable deadline and send data from c2 to c1
  88. if err := c1.SetReadDeadline(zeroTime); err != nil {
  89. t.Fatalf("unexpected error: %v", err)
  90. }
  91. data := []byte("foobar")
  92. c2 := pc.Conn2()
  93. if _, err := c2.Write(data); err != nil {
  94. t.Fatalf("unexpected error: %v", err)
  95. }
  96. dataBuf := make([]byte, len(data))
  97. if _, err := io.ReadFull(c1, dataBuf); err != nil {
  98. t.Fatalf("unexpected error: %v", err)
  99. }
  100. if !bytes.Equal(data, dataBuf) {
  101. t.Fatalf("unexpected data received: %q. Expecting %q", dataBuf, data)
  102. }
  103. }
  104. func TestPipeConnsCloseWhileReadWriteConcurrent(t *testing.T) {
  105. t.Parallel()
  106. concurrency := 4
  107. ch := make(chan struct{}, concurrency)
  108. for i := 0; i < concurrency; i++ {
  109. go func() {
  110. testPipeConnsCloseWhileReadWriteSerial(t)
  111. ch <- struct{}{}
  112. }()
  113. }
  114. for i := 0; i < concurrency; i++ {
  115. select {
  116. case <-ch:
  117. case <-time.After(5 * time.Second):
  118. t.Fatalf("timeout")
  119. }
  120. }
  121. }
  122. func TestPipeConnsCloseWhileReadWriteSerial(t *testing.T) {
  123. t.Parallel()
  124. testPipeConnsCloseWhileReadWriteSerial(t)
  125. }
  126. func testPipeConnsCloseWhileReadWriteSerial(t *testing.T) {
  127. for i := 0; i < 10; i++ {
  128. testPipeConnsCloseWhileReadWrite(t)
  129. }
  130. }
  131. func testPipeConnsCloseWhileReadWrite(t *testing.T) {
  132. pc := NewPipeConns()
  133. c1 := pc.Conn1()
  134. c2 := pc.Conn2()
  135. readCh := make(chan error)
  136. go func() {
  137. var err error
  138. if _, err = io.Copy(io.Discard, c1); err != nil {
  139. if err != errConnectionClosed {
  140. err = fmt.Errorf("unexpected error: %w", err)
  141. } else {
  142. err = nil
  143. }
  144. }
  145. readCh <- err
  146. }()
  147. writeCh := make(chan error)
  148. go func() {
  149. var err error
  150. for {
  151. if _, err = c2.Write([]byte("foobar")); err != nil {
  152. if err != errConnectionClosed {
  153. err = fmt.Errorf("unexpected error: %w", err)
  154. } else {
  155. err = nil
  156. }
  157. break
  158. }
  159. }
  160. writeCh <- err
  161. }()
  162. time.Sleep(10 * time.Millisecond)
  163. if err := c1.Close(); err != nil {
  164. t.Fatalf("unexpected error: %v", err)
  165. }
  166. if err := c2.Close(); err != nil {
  167. t.Fatalf("unexpected error: %v", err)
  168. }
  169. select {
  170. case err := <-readCh:
  171. if err != nil {
  172. t.Fatalf("unexpected error in reader: %v", err)
  173. }
  174. case <-time.After(time.Second):
  175. t.Fatalf("timeout")
  176. }
  177. select {
  178. case err := <-writeCh:
  179. if err != nil {
  180. t.Fatalf("unexpected error in writer: %v", err)
  181. }
  182. case <-time.After(time.Second):
  183. t.Fatalf("timeout")
  184. }
  185. }
  186. func TestPipeConnsReadWriteSerial(t *testing.T) {
  187. t.Parallel()
  188. testPipeConnsReadWriteSerial(t)
  189. }
  190. func TestPipeConnsReadWriteConcurrent(t *testing.T) {
  191. t.Parallel()
  192. testConcurrency(t, 10, testPipeConnsReadWriteSerial)
  193. }
  194. func testPipeConnsReadWriteSerial(t *testing.T) {
  195. pc := NewPipeConns()
  196. testPipeConnsReadWrite(t, pc.Conn1(), pc.Conn2())
  197. pc = NewPipeConns()
  198. testPipeConnsReadWrite(t, pc.Conn2(), pc.Conn1())
  199. }
  200. func testPipeConnsReadWrite(t *testing.T, c1, c2 net.Conn) {
  201. defer c1.Close()
  202. defer c2.Close()
  203. var buf [32]byte
  204. for i := 0; i < 10; i++ {
  205. // The first write
  206. s1 := fmt.Sprintf("foo_%d", i)
  207. n, err := c1.Write([]byte(s1))
  208. if err != nil {
  209. t.Fatalf("unexpected error: %v", err)
  210. }
  211. if n != len(s1) {
  212. t.Fatalf("unexpected number of bytes written: %d. Expecting %d", n, len(s1))
  213. }
  214. // The second write
  215. s2 := fmt.Sprintf("bar_%d", i)
  216. n, err = c1.Write([]byte(s2))
  217. if err != nil {
  218. t.Fatalf("unexpected error: %v", err)
  219. }
  220. if n != len(s2) {
  221. t.Fatalf("unexpected number of bytes written: %d. Expecting %d", n, len(s2))
  222. }
  223. // Read data written above in two writes
  224. s := s1 + s2
  225. n, err = c2.Read(buf[:])
  226. if err != nil {
  227. t.Fatalf("unexpected error: %v", err)
  228. }
  229. if n != len(s) {
  230. t.Fatalf("unexpected number of bytes read: %d. Expecting %d", n, len(s))
  231. }
  232. if string(buf[:n]) != s {
  233. t.Fatalf("unexpected string read: %q. Expecting %q", buf[:n], s)
  234. }
  235. }
  236. }
  237. func TestPipeConnsCloseSerial(t *testing.T) {
  238. t.Parallel()
  239. testPipeConnsCloseSerial(t)
  240. }
  241. func TestPipeConnsCloseConcurrent(t *testing.T) {
  242. t.Parallel()
  243. testConcurrency(t, 10, testPipeConnsCloseSerial)
  244. }
  245. func testPipeConnsCloseSerial(t *testing.T) {
  246. pc := NewPipeConns()
  247. testPipeConnsClose(t, pc.Conn1(), pc.Conn2())
  248. pc = NewPipeConns()
  249. testPipeConnsClose(t, pc.Conn2(), pc.Conn1())
  250. }
  251. func testPipeConnsClose(t *testing.T, c1, c2 net.Conn) {
  252. if err := c1.Close(); err != nil {
  253. t.Fatalf("unexpected error: %v", err)
  254. }
  255. var buf [10]byte
  256. // attempt writing to closed conn
  257. for i := 0; i < 10; i++ {
  258. n, err := c1.Write(buf[:])
  259. if err == nil {
  260. t.Fatalf("expecting error")
  261. }
  262. if n != 0 {
  263. t.Fatalf("unexpected number of bytes written: %d. Expecting 0", n)
  264. }
  265. }
  266. // attempt reading from closed conn
  267. for i := 0; i < 10; i++ {
  268. n, err := c2.Read(buf[:])
  269. if err == nil {
  270. t.Fatalf("expecting error")
  271. }
  272. if err != io.EOF {
  273. t.Fatalf("unexpected error: %v. Expecting %v", err, io.EOF)
  274. }
  275. if n != 0 {
  276. t.Fatalf("unexpected number of bytes read: %d. Expecting 0", n)
  277. }
  278. }
  279. if err := c2.Close(); err != nil {
  280. t.Fatalf("unexpected error: %v", err)
  281. }
  282. // attempt closing already closed conns
  283. for i := 0; i < 10; i++ {
  284. if err := c1.Close(); err != nil {
  285. t.Fatalf("unexpected error: %v", err)
  286. }
  287. if err := c2.Close(); err != nil {
  288. t.Fatalf("unexpected error: %v", err)
  289. }
  290. }
  291. }
  292. func testConcurrency(t *testing.T, concurrency int, f func(*testing.T)) {
  293. ch := make(chan struct{}, concurrency)
  294. for i := 0; i < concurrency; i++ {
  295. go func() {
  296. f(t)
  297. ch <- struct{}{}
  298. }()
  299. }
  300. for i := 0; i < concurrency; i++ {
  301. select {
  302. case <-ch:
  303. case <-time.After(time.Second):
  304. t.Fatalf("timeout")
  305. }
  306. }
  307. }
  308. func TestPipeConnsAddrDefault(t *testing.T) {
  309. t.Parallel()
  310. pc := NewPipeConns()
  311. c1 := pc.Conn1()
  312. if c1.LocalAddr() != pipeAddr(0) {
  313. t.Fatalf("unexpected local address: %v", c1.LocalAddr())
  314. }
  315. if c1.RemoteAddr() != pipeAddr(0) {
  316. t.Fatalf("unexpected remote address: %v", c1.RemoteAddr())
  317. }
  318. }
  319. func TestPipeConnsAddrCustom(t *testing.T) {
  320. t.Parallel()
  321. pc := NewPipeConns()
  322. addr1 := &net.TCPAddr{IP: net.IPv4(1, 2, 3, 4), Port: 1234}
  323. addr2 := &net.TCPAddr{IP: net.IPv4(5, 6, 7, 8), Port: 5678}
  324. addr3 := &net.TCPAddr{IP: net.IPv4(9, 10, 11, 12), Port: 9012}
  325. addr4 := &net.TCPAddr{IP: net.IPv4(13, 14, 15, 16), Port: 3456}
  326. pc.SetAddresses(addr1, addr2, addr3, addr4)
  327. c1 := pc.Conn1()
  328. if c1.LocalAddr() != addr1 {
  329. t.Fatalf("unexpected local address: %v", c1.LocalAddr())
  330. }
  331. if c1.RemoteAddr() != addr2 {
  332. t.Fatalf("unexpected remote address: %v", c1.RemoteAddr())
  333. }
  334. c2 := pc.Conn1()
  335. if c2.LocalAddr() != addr1 {
  336. t.Fatalf("unexpected local address: %v", c2.LocalAddr())
  337. }
  338. if c2.RemoteAddr() != addr2 {
  339. t.Fatalf("unexpected remote address: %v", c2.RemoteAddr())
  340. }
  341. }