buffer_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package gbytes_test
  2. import (
  3. "io"
  4. "time"
  5. . "github.com/onsi/gomega/gbytes"
  6. . "github.com/onsi/ginkgo"
  7. . "github.com/onsi/gomega"
  8. )
  9. var _ = Describe("Buffer", func() {
  10. var buffer *Buffer
  11. BeforeEach(func() {
  12. buffer = NewBuffer()
  13. })
  14. Describe("dumping the entire contents of the buffer", func() {
  15. It("should return everything that's been written", func() {
  16. buffer.Write([]byte("abc"))
  17. buffer.Write([]byte("def"))
  18. Ω(buffer.Contents()).Should(Equal([]byte("abcdef")))
  19. Ω(buffer).Should(Say("bcd"))
  20. Ω(buffer.Contents()).Should(Equal([]byte("abcdef")))
  21. })
  22. })
  23. Describe("creating a buffer with bytes", func() {
  24. It("should create the buffer with the cursor set to the beginning", func() {
  25. buffer := BufferWithBytes([]byte("abcdef"))
  26. Ω(buffer.Contents()).Should(Equal([]byte("abcdef")))
  27. Ω(buffer).Should(Say("abc"))
  28. Ω(buffer).ShouldNot(Say("abc"))
  29. Ω(buffer).Should(Say("def"))
  30. })
  31. })
  32. Describe("reading from a buffer", func() {
  33. It("should read the current contents of the buffer", func() {
  34. buffer := BufferWithBytes([]byte("abcde"))
  35. dest := make([]byte, 3)
  36. n, err := buffer.Read(dest)
  37. Ω(err).ShouldNot(HaveOccurred())
  38. Ω(n).Should(Equal(3))
  39. Ω(string(dest)).Should(Equal("abc"))
  40. dest = make([]byte, 3)
  41. n, err = buffer.Read(dest)
  42. Ω(err).ShouldNot(HaveOccurred())
  43. Ω(n).Should(Equal(2))
  44. Ω(string(dest[:n])).Should(Equal("de"))
  45. n, err = buffer.Read(dest)
  46. Ω(err).Should(Equal(io.EOF))
  47. Ω(n).Should(Equal(0))
  48. })
  49. Context("after the buffer has been closed", func() {
  50. It("returns an error", func() {
  51. buffer := BufferWithBytes([]byte("abcde"))
  52. buffer.Close()
  53. dest := make([]byte, 3)
  54. n, err := buffer.Read(dest)
  55. Ω(err).Should(HaveOccurred())
  56. Ω(n).Should(Equal(0))
  57. })
  58. })
  59. })
  60. Describe("detecting regular expressions", func() {
  61. It("should fire the appropriate channel when the passed in pattern matches, then close it", func(done Done) {
  62. go func() {
  63. time.Sleep(10 * time.Millisecond)
  64. buffer.Write([]byte("abcde"))
  65. }()
  66. A := buffer.Detect("%s", "a.c")
  67. B := buffer.Detect("def")
  68. var gotIt bool
  69. select {
  70. case gotIt = <-A:
  71. case <-B:
  72. Fail("should not have gotten here")
  73. }
  74. Ω(gotIt).Should(BeTrue())
  75. Eventually(A).Should(BeClosed())
  76. buffer.Write([]byte("f"))
  77. Eventually(B).Should(Receive())
  78. Eventually(B).Should(BeClosed())
  79. close(done)
  80. })
  81. It("should fast-forward the buffer upon detection", func(done Done) {
  82. buffer.Write([]byte("abcde"))
  83. <-buffer.Detect("abc")
  84. Ω(buffer).ShouldNot(Say("abc"))
  85. Ω(buffer).Should(Say("de"))
  86. close(done)
  87. })
  88. It("should only fast-forward the buffer when the channel is read, and only if doing so would not rewind it", func(done Done) {
  89. buffer.Write([]byte("abcde"))
  90. A := buffer.Detect("abc")
  91. time.Sleep(20 * time.Millisecond) //give the goroutine a chance to detect and write to the channel
  92. Ω(buffer).Should(Say("abcd"))
  93. <-A
  94. Ω(buffer).ShouldNot(Say("d"))
  95. Ω(buffer).Should(Say("e"))
  96. Eventually(A).Should(BeClosed())
  97. close(done)
  98. })
  99. It("should be possible to cancel a detection", func(done Done) {
  100. A := buffer.Detect("abc")
  101. B := buffer.Detect("def")
  102. buffer.CancelDetects()
  103. buffer.Write([]byte("abcdef"))
  104. Eventually(A).Should(BeClosed())
  105. Eventually(B).Should(BeClosed())
  106. Ω(buffer).Should(Say("bcde"))
  107. <-buffer.Detect("f")
  108. close(done)
  109. })
  110. })
  111. Describe("closing the buffer", func() {
  112. It("should error when further write attempts are made", func() {
  113. _, err := buffer.Write([]byte("abc"))
  114. Ω(err).ShouldNot(HaveOccurred())
  115. buffer.Close()
  116. _, err = buffer.Write([]byte("def"))
  117. Ω(err).Should(HaveOccurred())
  118. Ω(buffer.Contents()).Should(Equal([]byte("abc")))
  119. })
  120. It("should be closed", func() {
  121. Ω(buffer.Closed()).Should(BeFalse())
  122. buffer.Close()
  123. Ω(buffer.Closed()).Should(BeTrue())
  124. })
  125. })
  126. })