filewriter_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. package filewriter
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "path/filepath"
  7. "testing"
  8. "time"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. const logdir = "testlog"
  12. func touch(dir, name string) {
  13. os.MkdirAll(dir, 0755)
  14. fp, err := os.OpenFile(filepath.Join(dir, name), os.O_CREATE, 0644)
  15. if err != nil {
  16. panic(err)
  17. }
  18. fp.Close()
  19. }
  20. func TestMain(m *testing.M) {
  21. ret := m.Run()
  22. os.RemoveAll(logdir)
  23. os.Exit(ret)
  24. }
  25. func TestParseRotate(t *testing.T) {
  26. touch := func(dir, name string) {
  27. os.MkdirAll(dir, 0755)
  28. fp, err := os.OpenFile(filepath.Join(dir, name), os.O_CREATE, 0644)
  29. if err != nil {
  30. t.Fatal(err)
  31. }
  32. fp.Close()
  33. }
  34. dir := filepath.Join(logdir, "test-parse-rotate")
  35. names := []string{"info.log.2018-11-11", "info.log.2018-11-11.001", "info.log.2018-11-11.002", "info.log." + time.Now().Format("2006-01-02") + ".005"}
  36. for _, name := range names {
  37. touch(dir, name)
  38. }
  39. l, err := parseRotateItem(dir, "info.log", "2006-01-02")
  40. if err != nil {
  41. t.Fatal(err)
  42. }
  43. assert.Equal(t, len(names), l.Len())
  44. rt := l.Front().Value.(rotateItem)
  45. assert.Equal(t, 5, rt.rotateNum)
  46. }
  47. func TestRotateExists(t *testing.T) {
  48. dir := filepath.Join(logdir, "test-rotate-exists")
  49. names := []string{"info.log." + time.Now().Format("2006-01-02") + ".005"}
  50. for _, name := range names {
  51. touch(dir, name)
  52. }
  53. fw, err := New(logdir+"/test-rotate-exists/info.log",
  54. MaxSize(1024*1024),
  55. func(opt *option) { opt.RotateInterval = time.Millisecond },
  56. )
  57. if err != nil {
  58. t.Fatal(err)
  59. }
  60. data := make([]byte, 1024)
  61. for i := range data {
  62. data[i] = byte(i)
  63. }
  64. for i := 0; i < 10; i++ {
  65. for i := 0; i < 1024; i++ {
  66. _, err = fw.Write(data)
  67. if err != nil {
  68. t.Error(err)
  69. }
  70. }
  71. time.Sleep(10 * time.Millisecond)
  72. }
  73. fw.Close()
  74. fis, err := ioutil.ReadDir(logdir + "/test-rotate-exists")
  75. if err != nil {
  76. t.Fatal(err)
  77. }
  78. var fnams []string
  79. for _, fi := range fis {
  80. fnams = append(fnams, fi.Name())
  81. }
  82. assert.Contains(t, fnams, "info.log."+time.Now().Format("2006-01-02")+".006")
  83. }
  84. func TestSizeRotate(t *testing.T) {
  85. fw, err := New(logdir+"/test-rotate/info.log",
  86. MaxSize(1024*1024),
  87. func(opt *option) { opt.RotateInterval = 1 * time.Millisecond },
  88. )
  89. if err != nil {
  90. t.Fatal(err)
  91. }
  92. data := make([]byte, 1024)
  93. for i := range data {
  94. data[i] = byte(i)
  95. }
  96. for i := 0; i < 10; i++ {
  97. for i := 0; i < 1024; i++ {
  98. _, err = fw.Write(data)
  99. if err != nil {
  100. t.Error(err)
  101. }
  102. }
  103. time.Sleep(10 * time.Millisecond)
  104. }
  105. fw.Close()
  106. fis, err := ioutil.ReadDir(logdir + "/test-rotate")
  107. if err != nil {
  108. t.Fatal(err)
  109. }
  110. assert.True(t, len(fis) > 5, "expect more than 5 file get %d", len(fis))
  111. }
  112. func TestMaxFile(t *testing.T) {
  113. fw, err := New(logdir+"/test-maxfile/info.log",
  114. MaxSize(1024*1024),
  115. MaxFile(1),
  116. func(opt *option) { opt.RotateInterval = 1 * time.Millisecond },
  117. )
  118. if err != nil {
  119. t.Fatal(err)
  120. }
  121. data := make([]byte, 1024)
  122. for i := range data {
  123. data[i] = byte(i)
  124. }
  125. for i := 0; i < 10; i++ {
  126. for i := 0; i < 1024; i++ {
  127. _, err = fw.Write(data)
  128. if err != nil {
  129. t.Error(err)
  130. }
  131. }
  132. time.Sleep(10 * time.Millisecond)
  133. }
  134. fw.Close()
  135. fis, err := ioutil.ReadDir(logdir + "/test-maxfile")
  136. if err != nil {
  137. t.Fatal(err)
  138. }
  139. assert.True(t, len(fis) <= 2, fmt.Sprintf("expect 2 file get %d", len(fis)))
  140. }
  141. func TestMaxFile2(t *testing.T) {
  142. files := []string{
  143. "info.log.2018-12-01",
  144. "info.log.2018-12-02",
  145. "info.log.2018-12-03",
  146. "info.log.2018-12-04",
  147. "info.log.2018-12-05",
  148. "info.log.2018-12-05.001",
  149. }
  150. for _, file := range files {
  151. touch(logdir+"/test-maxfile2", file)
  152. }
  153. fw, err := New(logdir+"/test-maxfile2/info.log",
  154. MaxSize(1024*1024),
  155. MaxFile(3),
  156. func(opt *option) { opt.RotateInterval = 1 * time.Millisecond },
  157. )
  158. if err != nil {
  159. t.Fatal(err)
  160. }
  161. data := make([]byte, 1024)
  162. for i := range data {
  163. data[i] = byte(i)
  164. }
  165. for i := 0; i < 10; i++ {
  166. for i := 0; i < 1024; i++ {
  167. _, err = fw.Write(data)
  168. if err != nil {
  169. t.Error(err)
  170. }
  171. }
  172. time.Sleep(10 * time.Millisecond)
  173. }
  174. fw.Close()
  175. fis, err := ioutil.ReadDir(logdir + "/test-maxfile2")
  176. if err != nil {
  177. t.Fatal(err)
  178. }
  179. assert.True(t, len(fis) == 4, fmt.Sprintf("expect 4 file get %d", len(fis)))
  180. }
  181. func TestFileWriter(t *testing.T) {
  182. fw, err := New("testlog/info.log")
  183. if err != nil {
  184. t.Fatal(err)
  185. }
  186. defer fw.Close()
  187. _, err = fw.Write([]byte("Hello World!\n"))
  188. if err != nil {
  189. t.Error(err)
  190. }
  191. }
  192. func BenchmarkFileWriter(b *testing.B) {
  193. fw, err := New("testlog/bench/info.log",
  194. func(opt *option) { opt.WriteTimeout = time.Second }, MaxSize(1024*1024*8), /*32MB*/
  195. func(opt *option) { opt.RotateInterval = 10 * time.Millisecond },
  196. )
  197. if err != nil {
  198. b.Fatal(err)
  199. }
  200. for i := 0; i < b.N; i++ {
  201. _, err = fw.Write([]byte("Hello World!\n"))
  202. if err != nil {
  203. b.Error(err)
  204. }
  205. }
  206. }