multifilebuffer_test.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*
  2. * Public Domain Software
  3. *
  4. * I (Matthias Ladkau) am the author of the source code in this file.
  5. * I have placed the source code in this file in the public domain.
  6. *
  7. * For further information see: http://creativecommons.org/publicdomain/zero/1.0/
  8. */
  9. package fileutil
  10. import (
  11. "fmt"
  12. "io/ioutil"
  13. "os"
  14. "path"
  15. "testing"
  16. )
  17. const BUFTESTPATH = "filebuftestpath"
  18. func TestMultiFileBufferErrors(t *testing.T) {
  19. buf, err := NewMultiFileBuffer("**\x00",
  20. ConsecutiveNumberIterator(5), EmptyRolloverCondition())
  21. if buf != nil || err == nil {
  22. t.Error("Unexpected result:", buf, err)
  23. return
  24. }
  25. }
  26. func TestMultiFileBufferDateDailyDate(t *testing.T) {
  27. os.RemoveAll(BUFTESTPATH)
  28. os.Mkdir(BUFTESTPATH, 0770)
  29. defer func() {
  30. if res, _ := PathExists(BUFTESTPATH); res {
  31. os.RemoveAll(BUFTESTPATH)
  32. }
  33. }()
  34. filename := path.Join(BUFTESTPATH, "testdate.log")
  35. it := DailyDateIterator(-1, 2) // No limit on files
  36. // Fix the today day
  37. it.(*dailyDateIterator).tsFunc = func() string {
  38. return "512800001234" // 1986-04-02
  39. }
  40. buf, err := NewMultiFileBuffer(filename,
  41. it, SizeBasedRolloverCondition(3))
  42. if err != nil {
  43. t.Error(err)
  44. return
  45. }
  46. buf.Write([]byte("a"))
  47. buf.Write([]byte("b"))
  48. buf.Write([]byte("c"))
  49. if err = checkDirLayout(BUFTESTPATH, map[string]string{
  50. "testdate.log.1986-04-02": "abc",
  51. }); err != nil {
  52. t.Error(err)
  53. return
  54. }
  55. buf.Close()
  56. // Create a new buffer
  57. buf, err = NewMultiFileBuffer(filename,
  58. it, SizeBasedRolloverCondition(3))
  59. if err != nil {
  60. t.Error(err)
  61. return
  62. }
  63. buf.Write([]byte("d"))
  64. buf.Write([]byte("e"))
  65. buf.Write([]byte("fg"))
  66. buf.Write([]byte("h"))
  67. if err = checkDirLayout(BUFTESTPATH, map[string]string{
  68. "testdate.log.1986-04-02": "h",
  69. "testdate.log.1986-04-02.1": "defg",
  70. "testdate.log.1986-04-02.2": "abc",
  71. }); err != nil {
  72. t.Error(err)
  73. return
  74. }
  75. buf.Close()
  76. // A new day
  77. it.(*dailyDateIterator).tsFunc = func() string {
  78. return "512900001234" // 1986-04-03
  79. }
  80. buf.Write([]byte("123"))
  81. buf.Write([]byte("4"))
  82. if err = checkDirLayout(BUFTESTPATH, map[string]string{
  83. "testdate.log.1986-04-03": "4",
  84. "testdate.log.1986-04-03.1": "123",
  85. "testdate.log.1986-04-02": "h",
  86. "testdate.log.1986-04-02.1": "defg",
  87. "testdate.log.1986-04-02.2": "abc",
  88. }); err != nil {
  89. t.Error(err)
  90. return
  91. }
  92. buf.Close()
  93. // Test cleanup - Move months into the future
  94. it.(*dailyDateIterator).tsFunc = func() string {
  95. return "522800001234" // 1986-07-26
  96. }
  97. buf.Write([]byte("x"))
  98. if err = checkDirLayout(BUFTESTPATH, map[string]string{
  99. "testdate.log.1986-07-26": "x",
  100. "testdate.log.1986-04-03": "4",
  101. "testdate.log.1986-04-03.1": "123",
  102. }); err != nil {
  103. t.Error(err)
  104. return
  105. }
  106. buf.Close()
  107. // Last test writer without restriction
  108. buf, err = NewMultiFileBuffer(filename,
  109. it, EmptyRolloverCondition())
  110. if err != nil {
  111. t.Error(err)
  112. return
  113. }
  114. for i := 0; i < 10; i++ {
  115. buf.Write([]byte("x"))
  116. }
  117. if err = checkDirLayout(BUFTESTPATH, map[string]string{
  118. "testdate.log.1986-07-26": "xxxxxxxxxxx",
  119. "testdate.log.1986-04-03": "4",
  120. "testdate.log.1986-04-03.1": "123",
  121. }); err != nil {
  122. t.Error(err)
  123. return
  124. }
  125. buf.Close()
  126. // Write into a closed file
  127. for i := 0; i < 10; i++ {
  128. buf.Write([]byte("x"))
  129. }
  130. if err = checkDirLayout(BUFTESTPATH, map[string]string{
  131. "testdate.log.1986-07-26": "xxxxxxxxxxxxxxxxxxxxx",
  132. "testdate.log.1986-04-03": "4",
  133. "testdate.log.1986-04-03.1": "123",
  134. }); err != nil {
  135. t.Error(err)
  136. return
  137. }
  138. buf.Close()
  139. }
  140. func TestMultiFileBufferSimpleNumbering(t *testing.T) {
  141. os.RemoveAll(BUFTESTPATH)
  142. os.Mkdir(BUFTESTPATH, 0770)
  143. defer func() {
  144. if res, _ := PathExists(BUFTESTPATH); res {
  145. os.RemoveAll(BUFTESTPATH)
  146. }
  147. }()
  148. filename := path.Join(BUFTESTPATH, "test1.log")
  149. buf, err := NewMultiFileBuffer(filename,
  150. ConsecutiveNumberIterator(3), SizeBasedRolloverCondition(4))
  151. if err != nil {
  152. t.Error(err)
  153. return
  154. }
  155. buf.Write([]byte("a"))
  156. buf.Write([]byte("b"))
  157. if err = checkDirLayout(BUFTESTPATH, map[string]string{
  158. "test1.log": "ab",
  159. }); err != nil {
  160. t.Error(err)
  161. return
  162. }
  163. buf.Close()
  164. // Create a new buffer
  165. buf, err = NewMultiFileBuffer(filename,
  166. ConsecutiveNumberIterator(3), SizeBasedRolloverCondition(4))
  167. if err != nil {
  168. t.Error(err)
  169. return
  170. }
  171. buf.Write([]byte("c"))
  172. buf.Write([]byte("d"))
  173. if err = checkDirLayout(BUFTESTPATH, map[string]string{
  174. "test1.log": "abcd",
  175. }); err != nil {
  176. t.Error(err)
  177. return
  178. }
  179. // Now fill up the files
  180. for i := 0; i < 10; i++ {
  181. if _, err := buf.Write([]byte(fmt.Sprint(i))); err != nil {
  182. t.Error(err)
  183. return
  184. }
  185. }
  186. if err = checkDirLayout(BUFTESTPATH, map[string]string{
  187. "test1.log": "89",
  188. "test1.log.1": "4567",
  189. "test1.log.2": "0123",
  190. "test1.log.3": "abcd",
  191. }); err != nil {
  192. t.Error(err)
  193. return
  194. }
  195. // Fill up some more and see that the oldest entries disappear
  196. buf.Write([]byte("xxx"))
  197. for i := 0; i < 7; i++ {
  198. if _, err := buf.Write([]byte(fmt.Sprint(i))); err != nil {
  199. t.Error(err)
  200. return
  201. }
  202. }
  203. if err = checkDirLayout(BUFTESTPATH, map[string]string{
  204. "test1.log": "456",
  205. "test1.log.1": "0123",
  206. "test1.log.2": "89xxx",
  207. "test1.log.3": "4567",
  208. }); err != nil {
  209. t.Error(err)
  210. return
  211. }
  212. buf.Close()
  213. // Create a new buffer
  214. buf, _ = NewMultiFileBuffer(filename,
  215. ConsecutiveNumberIterator(3), SizeBasedRolloverCondition(4))
  216. for i := 0; i < 4; i++ {
  217. if _, err := buf.Write([]byte(fmt.Sprint(i))); err != nil {
  218. t.Error(err)
  219. return
  220. }
  221. }
  222. if err = checkDirLayout(BUFTESTPATH, map[string]string{
  223. "test1.log": "123",
  224. "test1.log.1": "4560",
  225. "test1.log.2": "0123",
  226. "test1.log.3": "89xxx",
  227. }); err != nil {
  228. t.Error(err)
  229. return
  230. }
  231. buf.Close()
  232. }
  233. func checkDirLayout(dir string, expected map[string]string) error {
  234. files, err := ioutil.ReadDir(dir)
  235. if err != nil {
  236. return err
  237. }
  238. if len(files) != len(expected) {
  239. foundFiles := make([]string, 0, len(files))
  240. for _, f := range files {
  241. foundFiles = append(foundFiles, f.Name())
  242. }
  243. return fmt.Errorf("Unexpected layout found files: %v", foundFiles)
  244. }
  245. for _, f := range files {
  246. content, err := ioutil.ReadFile(path.Join(dir, f.Name()))
  247. if err != nil {
  248. return err
  249. }
  250. expectedContent, ok := expected[f.Name()]
  251. if !ok {
  252. return fmt.Errorf("File %v not in list of expected files", f.Name())
  253. }
  254. if expectedContent != string(content) {
  255. return fmt.Errorf("Content of file %v is not as expected: %v",
  256. f.Name(), string(content))
  257. }
  258. }
  259. return nil
  260. }
  261. func TestConsecutiveNumberIterator(t *testing.T) {
  262. it := ConsecutiveNumberIterator(5)
  263. if res := it.NextName("foo"); res != "foo.1" {
  264. t.Error("Unexpected result: ", res)
  265. return
  266. }
  267. if res := it.NextName("foo.1"); res != "foo.2" {
  268. t.Error("Unexpected result: ", res)
  269. return
  270. }
  271. if res := it.NextName("foo.4"); res != "foo.5" {
  272. t.Error("Unexpected result: ", res)
  273. return
  274. }
  275. if res := it.NextName("foo.5"); res != "" {
  276. t.Error("Unexpected result: ", res)
  277. return
  278. }
  279. }
  280. func TestDailyDateIterator(t *testing.T) {
  281. it := DailyDateIterator(-1, -1)
  282. it.(*dailyDateIterator).tsFunc = func() string {
  283. return "512800001234" // 1986-04-02
  284. }
  285. filename := "foo"
  286. basename := it.Basename(filename)
  287. if res := it.NextName(basename); res != "foo.1986-04-02.1" {
  288. t.Error("Unexpected result: ", res)
  289. return
  290. }
  291. it.(*dailyDateIterator).tsFunc = func() string {
  292. return "522800001234" // 1986-07-26
  293. }
  294. basename = it.Basename(filename)
  295. if res := it.NextName(basename + ".51"); res != "foo.1986-07-26.52" {
  296. t.Error("Unexpected result: ", res)
  297. return
  298. }
  299. }