You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

321 lines
8.8 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. package autofile
  2. import (
  3. "context"
  4. "io"
  5. "os"
  6. "path/filepath"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/require"
  10. "github.com/tendermint/tendermint/libs/log"
  11. tmos "github.com/tendermint/tendermint/libs/os"
  12. tmrand "github.com/tendermint/tendermint/libs/rand"
  13. )
  14. func createTestGroupWithHeadSizeLimit(ctx context.Context, t *testing.T, logger log.Logger, headSizeLimit int64) *Group {
  15. testID := tmrand.Str(12)
  16. testDir := "_test_" + testID
  17. err := tmos.EnsureDir(testDir, 0700)
  18. require.NoError(t, err, "Error creating dir")
  19. headPath := testDir + "/myfile"
  20. g, err := OpenGroup(ctx, logger, headPath, GroupHeadSizeLimit(headSizeLimit))
  21. require.NoError(t, err, "Error opening Group")
  22. require.NotEqual(t, nil, g, "Failed to create Group")
  23. return g
  24. }
  25. func destroyTestGroup(t *testing.T, g *Group) {
  26. g.Close()
  27. err := os.RemoveAll(g.Dir)
  28. require.NoError(t, err, "Error removing test Group directory")
  29. }
  30. func assertGroupInfo(t *testing.T, gInfo GroupInfo, minIndex, maxIndex int, totalSize, headSize int64) {
  31. assert.Equal(t, minIndex, gInfo.MinIndex)
  32. assert.Equal(t, maxIndex, gInfo.MaxIndex)
  33. assert.Equal(t, totalSize, gInfo.TotalSize)
  34. assert.Equal(t, headSize, gInfo.HeadSize)
  35. }
  36. func TestCheckHeadSizeLimit(t *testing.T) {
  37. ctx, cancel := context.WithCancel(context.Background())
  38. defer cancel()
  39. logger := log.NewNopLogger()
  40. g := createTestGroupWithHeadSizeLimit(ctx, t, logger, 1000*1000)
  41. // At first, there are no files.
  42. assertGroupInfo(t, g.ReadGroupInfo(), 0, 0, 0, 0)
  43. // Write 1000 bytes 999 times.
  44. for i := 0; i < 999; i++ {
  45. err := g.WriteLine(tmrand.Str(999))
  46. require.NoError(t, err, "Error appending to head")
  47. }
  48. err := g.FlushAndSync()
  49. require.NoError(t, err)
  50. assertGroupInfo(t, g.ReadGroupInfo(), 0, 0, 999000, 999000)
  51. // Even calling checkHeadSizeLimit manually won't rotate it.
  52. g.checkHeadSizeLimit(ctx)
  53. assertGroupInfo(t, g.ReadGroupInfo(), 0, 0, 999000, 999000)
  54. // Write 1000 more bytes.
  55. err = g.WriteLine(tmrand.Str(999))
  56. require.NoError(t, err, "Error appending to head")
  57. err = g.FlushAndSync()
  58. require.NoError(t, err)
  59. // Calling checkHeadSizeLimit this time rolls it.
  60. g.checkHeadSizeLimit(ctx)
  61. assertGroupInfo(t, g.ReadGroupInfo(), 0, 1, 1000000, 0)
  62. // Write 1000 more bytes.
  63. err = g.WriteLine(tmrand.Str(999))
  64. require.NoError(t, err, "Error appending to head")
  65. err = g.FlushAndSync()
  66. require.NoError(t, err)
  67. // Calling checkHeadSizeLimit does nothing.
  68. g.checkHeadSizeLimit(ctx)
  69. assertGroupInfo(t, g.ReadGroupInfo(), 0, 1, 1001000, 1000)
  70. // Write 1000 bytes 999 times.
  71. for i := 0; i < 999; i++ {
  72. err = g.WriteLine(tmrand.Str(999))
  73. require.NoError(t, err, "Error appending to head")
  74. }
  75. err = g.FlushAndSync()
  76. require.NoError(t, err)
  77. assertGroupInfo(t, g.ReadGroupInfo(), 0, 1, 2000000, 1000000)
  78. // Calling checkHeadSizeLimit rolls it again.
  79. g.checkHeadSizeLimit(ctx)
  80. assertGroupInfo(t, g.ReadGroupInfo(), 0, 2, 2000000, 0)
  81. // Write 1000 more bytes.
  82. _, err = g.Head.Write([]byte(tmrand.Str(999) + "\n"))
  83. require.NoError(t, err, "Error appending to head")
  84. err = g.FlushAndSync()
  85. require.NoError(t, err)
  86. assertGroupInfo(t, g.ReadGroupInfo(), 0, 2, 2001000, 1000)
  87. // Calling checkHeadSizeLimit does nothing.
  88. g.checkHeadSizeLimit(ctx)
  89. assertGroupInfo(t, g.ReadGroupInfo(), 0, 2, 2001000, 1000)
  90. // Cleanup
  91. destroyTestGroup(t, g)
  92. }
  93. func TestRotateFile(t *testing.T) {
  94. logger := log.NewNopLogger()
  95. ctx, cancel := context.WithCancel(context.Background())
  96. defer cancel()
  97. g := createTestGroupWithHeadSizeLimit(ctx, t, logger, 0)
  98. // Create a different temporary directory and move into it, to make sure
  99. // relative paths are resolved at Group creation
  100. origDir, err := os.Getwd()
  101. require.NoError(t, err)
  102. defer func() {
  103. if err := os.Chdir(origDir); err != nil {
  104. t.Error(err)
  105. }
  106. }()
  107. dir := t.TempDir()
  108. require.NoError(t, os.Chdir(dir))
  109. require.True(t, filepath.IsAbs(g.Head.Path))
  110. require.True(t, filepath.IsAbs(g.Dir))
  111. // Create and rotate files
  112. err = g.WriteLine("Line 1")
  113. require.NoError(t, err)
  114. err = g.WriteLine("Line 2")
  115. require.NoError(t, err)
  116. err = g.WriteLine("Line 3")
  117. require.NoError(t, err)
  118. err = g.FlushAndSync()
  119. require.NoError(t, err)
  120. g.rotateFile(ctx)
  121. err = g.WriteLine("Line 4")
  122. require.NoError(t, err)
  123. err = g.WriteLine("Line 5")
  124. require.NoError(t, err)
  125. err = g.WriteLine("Line 6")
  126. require.NoError(t, err)
  127. err = g.FlushAndSync()
  128. require.NoError(t, err)
  129. // Read g.Head.Path+"000"
  130. body1, err := os.ReadFile(g.Head.Path + ".000")
  131. assert.NoError(t, err, "Failed to read first rolled file")
  132. if string(body1) != "Line 1\nLine 2\nLine 3\n" {
  133. t.Errorf("got unexpected contents: [%v]", string(body1))
  134. }
  135. // Read g.Head.Path
  136. body2, err := os.ReadFile(g.Head.Path)
  137. assert.NoError(t, err, "Failed to read first rolled file")
  138. if string(body2) != "Line 4\nLine 5\nLine 6\n" {
  139. t.Errorf("got unexpected contents: [%v]", string(body2))
  140. }
  141. // Make sure there are no files in the current, temporary directory
  142. files, err := os.ReadDir(".")
  143. require.NoError(t, err)
  144. assert.Empty(t, files)
  145. // Cleanup
  146. destroyTestGroup(t, g)
  147. }
  148. func TestWrite(t *testing.T) {
  149. logger := log.NewNopLogger()
  150. ctx, cancel := context.WithCancel(context.Background())
  151. defer cancel()
  152. g := createTestGroupWithHeadSizeLimit(ctx, t, logger, 0)
  153. written := []byte("Medusa")
  154. _, err := g.Write(written)
  155. require.NoError(t, err)
  156. err = g.FlushAndSync()
  157. require.NoError(t, err)
  158. read := make([]byte, len(written))
  159. gr, err := g.NewReader(0)
  160. require.NoError(t, err, "failed to create reader")
  161. _, err = gr.Read(read)
  162. assert.NoError(t, err, "failed to read data")
  163. assert.Equal(t, written, read)
  164. // Cleanup
  165. destroyTestGroup(t, g)
  166. }
  167. // test that Read reads the required amount of bytes from all the files in the
  168. // group and returns no error if n == size of the given slice.
  169. func TestGroupReaderRead(t *testing.T) {
  170. logger := log.NewNopLogger()
  171. ctx, cancel := context.WithCancel(context.Background())
  172. defer cancel()
  173. g := createTestGroupWithHeadSizeLimit(ctx, t, logger, 0)
  174. professor := []byte("Professor Monster")
  175. _, err := g.Write(professor)
  176. require.NoError(t, err)
  177. err = g.FlushAndSync()
  178. require.NoError(t, err)
  179. g.rotateFile(ctx)
  180. frankenstein := []byte("Frankenstein's Monster")
  181. _, err = g.Write(frankenstein)
  182. require.NoError(t, err)
  183. err = g.FlushAndSync()
  184. require.NoError(t, err)
  185. totalWrittenLength := len(professor) + len(frankenstein)
  186. read := make([]byte, totalWrittenLength)
  187. gr, err := g.NewReader(0)
  188. require.NoError(t, err, "failed to create reader")
  189. n, err := gr.Read(read)
  190. assert.NoError(t, err, "failed to read data")
  191. assert.Equal(t, totalWrittenLength, n, "not enough bytes read")
  192. professorPlusFrankenstein := professor
  193. professorPlusFrankenstein = append(professorPlusFrankenstein, frankenstein...)
  194. assert.Equal(t, professorPlusFrankenstein, read)
  195. // Cleanup
  196. destroyTestGroup(t, g)
  197. }
  198. // test that Read returns an error if number of bytes read < size of
  199. // the given slice. Subsequent call should return 0, io.EOF.
  200. func TestGroupReaderRead2(t *testing.T) {
  201. logger := log.NewNopLogger()
  202. ctx, cancel := context.WithCancel(context.Background())
  203. defer cancel()
  204. g := createTestGroupWithHeadSizeLimit(ctx, t, logger, 0)
  205. professor := []byte("Professor Monster")
  206. _, err := g.Write(professor)
  207. require.NoError(t, err)
  208. err = g.FlushAndSync()
  209. require.NoError(t, err)
  210. g.rotateFile(ctx)
  211. frankenstein := []byte("Frankenstein's Monster")
  212. frankensteinPart := []byte("Frankenstein")
  213. _, err = g.Write(frankensteinPart) // note writing only a part
  214. require.NoError(t, err)
  215. err = g.FlushAndSync()
  216. require.NoError(t, err)
  217. totalLength := len(professor) + len(frankenstein)
  218. read := make([]byte, totalLength)
  219. gr, err := g.NewReader(0)
  220. require.NoError(t, err, "failed to create reader")
  221. // 1) n < (size of the given slice), io.EOF
  222. n, err := gr.Read(read)
  223. assert.Equal(t, io.EOF, err)
  224. assert.Equal(t, len(professor)+len(frankensteinPart), n, "Read more/less bytes than it is in the group")
  225. // 2) 0, io.EOF
  226. n, err = gr.Read([]byte("0"))
  227. assert.Equal(t, io.EOF, err)
  228. assert.Equal(t, 0, n)
  229. // Cleanup
  230. destroyTestGroup(t, g)
  231. }
  232. func TestMinIndex(t *testing.T) {
  233. logger := log.NewNopLogger()
  234. ctx, cancel := context.WithCancel(context.Background())
  235. defer cancel()
  236. g := createTestGroupWithHeadSizeLimit(ctx, t, logger, 0)
  237. assert.Zero(t, g.MinIndex(), "MinIndex should be zero at the beginning")
  238. // Cleanup
  239. destroyTestGroup(t, g)
  240. }
  241. func TestMaxIndex(t *testing.T) {
  242. logger := log.NewNopLogger()
  243. ctx, cancel := context.WithCancel(context.Background())
  244. defer cancel()
  245. g := createTestGroupWithHeadSizeLimit(ctx, t, logger, 0)
  246. assert.Zero(t, g.MaxIndex(), "MaxIndex should be zero at the beginning")
  247. err := g.WriteLine("Line 1")
  248. require.NoError(t, err)
  249. err = g.FlushAndSync()
  250. require.NoError(t, err)
  251. g.rotateFile(ctx)
  252. assert.Equal(t, 1, g.MaxIndex(), "MaxIndex should point to the last file")
  253. // Cleanup
  254. destroyTestGroup(t, g)
  255. }