process_test.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright 2017 fatedier, fatedier@gmail.com
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package tcp
  15. import (
  16. "io"
  17. "testing"
  18. "github.com/stretchr/testify/assert"
  19. )
  20. func TestJoin(t *testing.T) {
  21. assert := assert.New(t)
  22. var (
  23. n int
  24. err error
  25. )
  26. text1 := "A document that gives tips for writing clear, idiomatic Go code. A must read for any new Go programmer. It augments the tour and the language specification, both of which should be read first."
  27. text2 := "A document that specifies the conditions under which reads of a variable in one goroutine can be guaranteed to observe values produced by writes to the same variable in a different goroutine."
  28. // Forward bytes directly.
  29. pr, pw := io.Pipe()
  30. pr2, pw2 := io.Pipe()
  31. pr3, pw3 := io.Pipe()
  32. pr4, pw4 := io.Pipe()
  33. conn1 := WrapReadWriteCloser(pr, pw2)
  34. conn2 := WrapReadWriteCloser(pr2, pw)
  35. conn3 := WrapReadWriteCloser(pr3, pw4)
  36. conn4 := WrapReadWriteCloser(pr4, pw3)
  37. go func() {
  38. Join(conn2, conn3)
  39. }()
  40. buf1 := make([]byte, 1024)
  41. buf2 := make([]byte, 1024)
  42. conn1.Write([]byte(text1))
  43. conn4.Write([]byte(text2))
  44. n, err = conn4.Read(buf1)
  45. assert.NoError(err)
  46. assert.Equal(text1, string(buf1[:n]))
  47. n, err = conn1.Read(buf2)
  48. assert.NoError(err)
  49. assert.Equal(text2, string(buf2[:n]))
  50. conn1.Close()
  51. conn2.Close()
  52. conn3.Close()
  53. conn4.Close()
  54. }