jyfs_rpc_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package test
  2. import (
  3. "context"
  4. "log"
  5. "testing"
  6. "time"
  7. "app.yhyue.com/moapp/jyfs/rpc/filesystem"
  8. "app.yhyue.com/moapp/jyfs/rpc/filesystemclient"
  9. "app.yhyue.com/moapp/jyfs/rpc/internal/config"
  10. "github.com/tal-tech/go-zero/core/conf"
  11. "github.com/tal-tech/go-zero/zrpc"
  12. )
  13. var C config.Config
  14. func init() {
  15. conf.MustLoad("./filesystem.yaml", &C)
  16. }
  17. // 创建域
  18. func Test_CreateDomain(t *testing.T) {
  19. ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
  20. FileSystem := filesystemclient.NewFileSystem(zrpc.MustNewClient(C.FileSystemConf))
  21. req := &filesystem.DomainReq{Name: "xzh", MetaFields: []string{"docName", "docSuffix", "docSize"}}
  22. res, err := FileSystem.CreateDomain(ctx, req)
  23. log.Println("err ", err)
  24. log.Println("req ", res)
  25. }
  26. // 删除域
  27. func Test_DeleteDomain(t *testing.T) {
  28. ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
  29. FileSystem := filesystemclient.NewFileSystem(zrpc.MustNewClient(C.FileSystemConf))
  30. req := &filesystem.DomainReq{Name: "xzh", MetaFields: []string{"docName", "docSuffix", "docSize"}}
  31. res, err := FileSystem.DeleteDomain(ctx, req)
  32. log.Println("err ", err)
  33. log.Println("req ", res)
  34. }
  35. // 删除文件
  36. func Test_DeleteFile(t *testing.T) {
  37. ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
  38. FileSystem := filesystemclient.NewFileSystem(zrpc.MustNewClient(C.FileSystemConf))
  39. req := &filesystem.LoadFileReq{Domain: "xzh", FileId: "que.txt"}
  40. res, err := FileSystem.DeleteFile(ctx, req)
  41. log.Println("err ", err)
  42. log.Println("req ", res)
  43. }
  44. // 获取文件
  45. func Test_GetFile(t *testing.T) {
  46. ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
  47. FileSystem := filesystemclient.NewFileSystem(zrpc.MustNewClient(C.FileSystemConf))
  48. req := &filesystem.LoadFileReq{Domain: "xzh", FileId: "que.txt"}
  49. res, err := FileSystem.GetFile(ctx, req)
  50. log.Println("err ", err)
  51. log.Println("req ", res)
  52. }
  53. // 获取文件元数据
  54. func Test_GetFileMeta(t *testing.T) {
  55. ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
  56. FileSystem := filesystemclient.NewFileSystem(zrpc.MustNewClient(C.FileSystemConf))
  57. req := &filesystem.LoadFileReq{Domain: "xzh", FileId: "que.txt"}
  58. res, err := FileSystem.GetFileMeta(ctx, req)
  59. log.Println("err ", err)
  60. log.Println("req ", res)
  61. }
  62. // 保存文件
  63. func Test_SaveFile(t *testing.T) {
  64. ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
  65. FileSystem := filesystemclient.NewFileSystem(zrpc.MustNewClient(C.FileSystemConf))
  66. file, err := os.Open("./111.txt")
  67. if err != nil {
  68. return nil, err
  69. }
  70. defer file.Close()
  71. // FileInfo:
  72. stats, err := file.Stat()
  73. if err != nil {
  74. return nil, err
  75. }
  76. // []byte
  77. data := make([]byte, stats.Size())
  78. count, err := file.Read(data)
  79. if err != nil {
  80. return nil, err
  81. }
  82. fmt.Println(count)
  83. req := &filesystem.SaveFileReq{
  84. Domain: "xzh",
  85. FileId: "111.txt",
  86. Meta: map[string]string{"docName": "111", "docSuffix": ".txt", "docSize": "1024"},
  87. RawFileContent: data,
  88. }
  89. res, err := FileSystem.SaveFile(ctx, req)
  90. log.Println("err ", err)
  91. log.Println("req ", res)
  92. }
  93. // 更新文件元数据
  94. func Test_UpdateFileMeta(t *testing.T) {
  95. ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
  96. FileSystem := filesystemclient.NewFileSystem(zrpc.MustNewClient(C.FileSystemConf))
  97. req := &filesystem.UpdateFileMetaReq{
  98. Domain: "xzh",
  99. FileId: "que.txt",
  100. Meta: map[string]string{"docName": "que", "docSuffix": ".txt", "docSize": "2048"},
  101. }
  102. res, err := FileSystem.UpdateFileMeta(ctx, req)
  103. log.Println("err ", err)
  104. log.Println("req ", res)
  105. }