rocksdb_store_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // +build rocksdb
  2. package rocksdb
  3. import (
  4. "context"
  5. "fmt"
  6. "io/ioutil"
  7. "os"
  8. "testing"
  9. "time"
  10. "github.com/chrislusf/seaweedfs/weed/filer"
  11. "github.com/chrislusf/seaweedfs/weed/util"
  12. )
  13. func TestCreateAndFind(t *testing.T) {
  14. testFiler := filer.NewFiler(nil, nil, "", 0, "", "", "", nil)
  15. dir, _ := ioutil.TempDir("", "seaweedfs_filer_test")
  16. defer os.RemoveAll(dir)
  17. store := &RocksDBStore{}
  18. store.initialize(dir)
  19. testFiler.SetStore(store)
  20. fullpath := util.FullPath("/home/chris/this/is/one/file1.jpg")
  21. ctx := context.Background()
  22. entry1 := &filer.Entry{
  23. FullPath: fullpath,
  24. Attr: filer.Attr{
  25. Mode: 0440,
  26. Uid: 1234,
  27. Gid: 5678,
  28. },
  29. }
  30. if err := testFiler.CreateEntry(ctx, entry1, false, false, nil); err != nil {
  31. t.Errorf("create entry %v: %v", entry1.FullPath, err)
  32. return
  33. }
  34. entry, err := testFiler.FindEntry(ctx, fullpath)
  35. if err != nil {
  36. t.Errorf("find entry: %v", err)
  37. return
  38. }
  39. if entry.FullPath != entry1.FullPath {
  40. t.Errorf("find wrong entry: %v", entry.FullPath)
  41. return
  42. }
  43. // checking one upper directory
  44. entries, _, _ := testFiler.ListDirectoryEntries(ctx, util.FullPath("/home/chris/this/is/one"), "", false, 100, "", "")
  45. if len(entries) != 1 {
  46. t.Errorf("list entries count: %v", len(entries))
  47. return
  48. }
  49. // checking one upper directory
  50. entries, _, _ = testFiler.ListDirectoryEntries(ctx, util.FullPath("/"), "", false, 100, "", "")
  51. if len(entries) != 1 {
  52. t.Errorf("list entries count: %v", len(entries))
  53. return
  54. }
  55. }
  56. func TestEmptyRoot(t *testing.T) {
  57. testFiler := filer.NewFiler(nil, nil, "", 0, "", "", "", nil)
  58. dir, _ := ioutil.TempDir("", "seaweedfs_filer_test2")
  59. defer os.RemoveAll(dir)
  60. store := &RocksDBStore{}
  61. store.initialize(dir)
  62. testFiler.SetStore(store)
  63. ctx := context.Background()
  64. // checking one upper directory
  65. entries, _, err := testFiler.ListDirectoryEntries(ctx, util.FullPath("/"), "", false, 100, "", "")
  66. if err != nil {
  67. t.Errorf("list entries: %v", err)
  68. return
  69. }
  70. if len(entries) != 0 {
  71. t.Errorf("list entries count: %v", len(entries))
  72. return
  73. }
  74. }
  75. func BenchmarkInsertEntry(b *testing.B) {
  76. testFiler := filer.NewFiler(nil, nil, "", 0, "", "", "", nil)
  77. dir, _ := ioutil.TempDir("", "seaweedfs_filer_bench")
  78. defer os.RemoveAll(dir)
  79. store := &RocksDBStore{}
  80. store.initialize(dir)
  81. testFiler.SetStore(store)
  82. ctx := context.Background()
  83. b.ReportAllocs()
  84. for i := 0; i < b.N; i++ {
  85. entry := &filer.Entry{
  86. FullPath: util.FullPath(fmt.Sprintf("/file%d.txt", i)),
  87. Attr: filer.Attr{
  88. Crtime: time.Now(),
  89. Mtime: time.Now(),
  90. Mode: os.FileMode(0644),
  91. },
  92. }
  93. store.InsertEntry(ctx, entry)
  94. }
  95. }