persistence_test.go 4.0 KB

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