persistence_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package persistence
  2. import (
  3. "encoding/hex"
  4. "testing"
  5. "github.com/langgenius/dify-plugin-daemon/internal/db"
  6. "github.com/langgenius/dify-plugin-daemon/internal/oss/local"
  7. "github.com/langgenius/dify-plugin-daemon/internal/types/app"
  8. "github.com/langgenius/dify-plugin-daemon/internal/utils/cache"
  9. "github.com/langgenius/dify-plugin-daemon/internal/utils/strings"
  10. )
  11. func TestPersistenceStoreAndLoad(t *testing.T) {
  12. err := cache.InitRedisClient("localhost:6379", "difyai123456", false)
  13. if err != nil {
  14. t.Fatalf("Failed to init redis client: %v", err)
  15. }
  16. defer cache.Close()
  17. db.Init(&app.Config{
  18. DBUsername: "postgres",
  19. DBPassword: "difyai123456",
  20. DBHost: "localhost",
  21. DBPort: 5432,
  22. DBDatabase: "dify_plugin_daemon",
  23. DBSslMode: "disable",
  24. })
  25. defer db.Close()
  26. oss := local.NewLocalStorage("./storage")
  27. InitPersistence(oss, &app.Config{
  28. PersistenceStoragePath: "./persistence_storage",
  29. PersistenceStorageMaxSize: 1024 * 1024 * 1024,
  30. })
  31. key := strings.RandomString(10)
  32. if err := persistence.Save("tenant_id", "plugin_checksum", -1, key, []byte("data")); err != nil {
  33. t.Fatalf("Failed to save data: %v", err)
  34. }
  35. data, err := persistence.Load("tenant_id", "plugin_checksum", key)
  36. if err != nil {
  37. t.Fatalf("Failed to load data: %v", err)
  38. }
  39. if string(data) != "data" {
  40. t.Fatalf("Data mismatch: %s", data)
  41. }
  42. // check if the file exists
  43. if _, err := oss.Load("./persistence_storage/tenant_id/plugin_checksum/" + key); err != nil {
  44. t.Fatalf("File not found: %v", err)
  45. }
  46. // check if cache is updated
  47. cacheData, err := cache.GetString("persistence:cache:tenant_id:plugin_checksum:" + key)
  48. if err != nil {
  49. t.Fatalf("Failed to get cache data: %v", err)
  50. }
  51. cacheDataBytes, err := hex.DecodeString(cacheData)
  52. if err != nil {
  53. t.Fatalf("Failed to decode cache data: %v", err)
  54. }
  55. if string(cacheDataBytes) != "data" {
  56. t.Fatalf("Cache data mismatch: %s", cacheData)
  57. }
  58. }
  59. func TestPersistenceSaveAndLoadWithLongKey(t *testing.T) {
  60. err := cache.InitRedisClient("localhost:6379", "difyai123456", false)
  61. if err != nil {
  62. t.Fatalf("Failed to init redis client: %v", err)
  63. }
  64. defer cache.Close()
  65. db.Init(&app.Config{
  66. DBUsername: "postgres",
  67. DBPassword: "difyai123456",
  68. DBHost: "localhost",
  69. DBPort: 5432,
  70. DBDatabase: "dify_plugin_daemon",
  71. DBSslMode: "disable",
  72. })
  73. defer db.Close()
  74. InitPersistence(local.NewLocalStorage("./storage"), &app.Config{
  75. PersistenceStoragePath: "./persistence_storage",
  76. PersistenceStorageMaxSize: 1024 * 1024 * 1024,
  77. })
  78. key := strings.RandomString(257)
  79. if err := persistence.Save("tenant_id", "plugin_checksum", -1, key, []byte("data")); err == nil {
  80. t.Fatalf("Expected error, got nil")
  81. }
  82. }
  83. func TestPersistenceDelete(t *testing.T) {
  84. err := cache.InitRedisClient("localhost:6379", "difyai123456", false)
  85. if err != nil {
  86. t.Fatalf("Failed to init redis client: %v", err)
  87. }
  88. defer cache.Close()
  89. db.Init(&app.Config{
  90. DBUsername: "postgres",
  91. DBPassword: "difyai123456",
  92. DBHost: "localhost",
  93. DBPort: 5432,
  94. DBDatabase: "dify_plugin_daemon",
  95. DBSslMode: "disable",
  96. })
  97. defer db.Close()
  98. oss := local.NewLocalStorage("./storage")
  99. InitPersistence(oss, &app.Config{
  100. PersistenceStoragePath: "./persistence_storage",
  101. PersistenceStorageMaxSize: 1024 * 1024 * 1024,
  102. })
  103. key := strings.RandomString(10)
  104. if err := persistence.Save("tenant_id", "plugin_checksum", -1, key, []byte("data")); err != nil {
  105. t.Fatalf("Failed to save data: %v", err)
  106. }
  107. if err := persistence.Delete("tenant_id", "plugin_checksum", key); err != nil {
  108. t.Fatalf("Failed to delete data: %v", err)
  109. }
  110. // check if the file exists
  111. if _, err := oss.Load("./persistence_storage/tenant_id/plugin_checksum/" + key); err == nil {
  112. t.Fatalf("File not deleted: %v", err)
  113. }
  114. // check if cache is updated
  115. _, err = cache.GetString("persistence:cache:tenant_id:plugin_checksum:" + key)
  116. if err != cache.ErrNotFound {
  117. t.Fatalf("Cache data not deleted: %v", err)
  118. }
  119. }