persistence_test.go 4.1 KB

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