mirror of
https://github.com/langgenius/dify-plugin-daemon.git
synced 2026-07-22 17:56:00 -04:00
888ad788bc
* refactor: introduce local plugin control panel and cleanup environment setup process * fix: args * refactor: new local runtime * temp: stash work for refactor on RemotePluginServer * refactor: unify local runtime lifetime and sperate init environment process * chore: add missing files * stash * refactor: local plugin lifetime control * refactor: complete installation process of control panel * refactor: adapt service layer to new controlpanel * refactor: pluginManager.Install * fix: add routine wrap to InstallServerless, avoid blocking main thread * feat: reinstall serverless runtime * chore: add comments to Reinstall and update confusing naming * refactor: unify install plugin service * refactor: add labels to debugging runtime * refactor: add getters to plugin manager * refactor: split install service to decode/install_task/install service * ??? * refactor: adapt controllers * refactor: session write * refactor: session runtime * Refine install task orchestration (#501) * refactor: installing task * refactor cluster management, decouple lifetime management and cluster * fix cli test command * fix: cleanup TODO comments and implement GracefulStop for instance * feat: add logger to control panel * fix: multiple nil references * refactor: better lifetime control * refactor: better cycle interval * fix(LocalPluginRuntime): prevent returning err when it's not error * fix: avoid adding empty PipExtraArgs * fix: missing errors in Environment init * fix: add truncateMessage to avoid db explosion * cleanup: better lifecycle management * fix: init status at the beginning of installation * optimize: GracefulStop for pluginInstance * refactor: tests * refactor: centralize routine labels (#504) * cleanup: RoutineKey * fix: init routine pool * fix: correctly handle cluster register error * fix: memory leak * fix: add \n to instance write * fix(installer.go): set success to true after succeed for defer func * refactor * fix: missing cwd in testutils * fix: scaleup default runtime nums to 1 when testing * fix: localruntime appconfig in testing module * Update internal/core/local_runtime/load_balancing.go Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * fix: more efficiency implement in installer_local.go * fix: returns after failing in onDebuggingRuntimeDisconnected * fix: returns after failing in onDebuggingRuntimeDisconnected * fix: splits tests * refactor: naming * refactor: manifest.VersionX * fix: adapt SetDefault to tests * fix: enforce use constants in DBType * fix: generate * fix: linter * cleanup tests * refactor: change package to * cleanup: useless codes * Update internal/cluster/plugin.go Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * cleanup * refactor: decouple connection_key management from debugging_time * refactor: confused naming * feat: recycle resources to adapt to https://github.com/langgenius/dify-plugin-daemon/pull/500 * refactor: confusing redirecting * fix: support get serverless runtime * fix: race condition in Launching * fix: avoid ManifestValidate in first step of debugging handshake * fix: adding ReleaseAllLocks to finalizers * wtf: what a beautiful code * refactor: rename Stream.Async to Stream.Process * fix: kill process if daed instance was detected * fix: correctly handle failures * fix: consistence of difference interfaces * fix: add stacktrace to panic * fix: only trigger once event * fix: ensure plugin runtime was shutdown * feat: cleanup install tasks * fix: add scale logs --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
355 lines
6.8 KiB
Go
355 lines
6.8 KiB
Go
package cache
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
"sync"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/redis/go-redis/v9"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
const (
|
|
TEST_PREFIX = "test"
|
|
)
|
|
|
|
func getRedisConnection() error {
|
|
return InitRedisClient("0.0.0.0:6379", "", "difyai123456", false, 0)
|
|
}
|
|
|
|
func TestRedisConnection(t *testing.T) {
|
|
// get redis connection
|
|
if err := getRedisConnection(); err != nil {
|
|
t.Errorf("get redis connection failed: %v", err)
|
|
return
|
|
}
|
|
|
|
// close
|
|
if err := Close(); err != nil {
|
|
t.Errorf("close redis client failed: %v", err)
|
|
return
|
|
}
|
|
}
|
|
|
|
func TestRedisTransaction(t *testing.T) {
|
|
// get redis connection
|
|
if err := getRedisConnection(); err != nil {
|
|
t.Errorf("get redis connection failed: %v", err)
|
|
return
|
|
}
|
|
defer Close()
|
|
|
|
// test transaction
|
|
err := Transaction(func(p redis.Pipeliner) error {
|
|
// set key
|
|
if err := Store(
|
|
strings.Join([]string{TEST_PREFIX, "key"}, ":"),
|
|
"value",
|
|
time.Second,
|
|
p,
|
|
); err != nil {
|
|
t.Errorf("store key failed: %v", err)
|
|
return err
|
|
}
|
|
|
|
return errors.New("test transaction error")
|
|
})
|
|
|
|
if err == nil {
|
|
t.Errorf("transaction should return error")
|
|
return
|
|
}
|
|
|
|
// get key
|
|
value, err := GetString(
|
|
strings.Join([]string{TEST_PREFIX, "key"}, ":"),
|
|
)
|
|
|
|
if err != ErrNotFound {
|
|
t.Errorf("key should not exist")
|
|
return
|
|
}
|
|
|
|
if value != "" {
|
|
t.Errorf("value should be empty")
|
|
return
|
|
}
|
|
|
|
// test success transaction
|
|
err = Transaction(func(p redis.Pipeliner) error {
|
|
// set key
|
|
if err := Store(
|
|
strings.Join([]string{TEST_PREFIX, "key"}, ":"),
|
|
"value",
|
|
time.Second,
|
|
p,
|
|
); err != nil {
|
|
t.Errorf("store key failed: %v", err)
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
})
|
|
|
|
if err != nil {
|
|
t.Errorf("transaction should not return error")
|
|
return
|
|
}
|
|
|
|
defer Del(strings.Join([]string{TEST_PREFIX, "key"}, ":"))
|
|
|
|
// get key
|
|
value, err = GetString(
|
|
strings.Join([]string{TEST_PREFIX, "key"}, ":"),
|
|
)
|
|
|
|
if err != nil {
|
|
t.Errorf("get key failed: %v", err)
|
|
return
|
|
}
|
|
|
|
if value != "value" {
|
|
t.Errorf("value should be value")
|
|
return
|
|
}
|
|
}
|
|
|
|
func TestRedisScanMap(t *testing.T) {
|
|
// get redis connection
|
|
if err := getRedisConnection(); err != nil {
|
|
t.Errorf("get redis connection failed: %v", err)
|
|
return
|
|
}
|
|
defer Close()
|
|
|
|
type s struct {
|
|
Field string `json:"field"`
|
|
}
|
|
|
|
err := SetMapOneField(strings.Join([]string{TEST_PREFIX, "map"}, ":"), "key1", s{Field: "value1"})
|
|
if err != nil {
|
|
t.Errorf("set map failed: %v", err)
|
|
return
|
|
}
|
|
defer Del(strings.Join([]string{TEST_PREFIX, "map"}, ":"))
|
|
err = SetMapOneField(strings.Join([]string{TEST_PREFIX, "map"}, ":"), "key2", s{Field: "value2"})
|
|
if err != nil {
|
|
t.Errorf("set map failed: %v", err)
|
|
return
|
|
}
|
|
err = SetMapOneField(strings.Join([]string{TEST_PREFIX, "map"}, ":"), "key3", s{Field: "value3"})
|
|
if err != nil {
|
|
t.Errorf("set map failed: %v", err)
|
|
return
|
|
}
|
|
err = SetMapOneField(strings.Join([]string{TEST_PREFIX, "map"}, ":"), "4", s{Field: "value4"})
|
|
if err != nil {
|
|
t.Errorf("set map failed: %v", err)
|
|
return
|
|
}
|
|
|
|
data, err := ScanMap[s](strings.Join([]string{TEST_PREFIX, "map"}, ":"), "key*")
|
|
if err != nil {
|
|
t.Errorf("scan map failed: %v", err)
|
|
return
|
|
}
|
|
|
|
if len(data) != 3 {
|
|
t.Errorf("scan map should return 3")
|
|
return
|
|
}
|
|
|
|
if data["key1"].Field != "value1" {
|
|
t.Errorf("scan map should return value1")
|
|
return
|
|
}
|
|
|
|
if data["key2"].Field != "value2" {
|
|
t.Errorf("scan map should return value2")
|
|
return
|
|
}
|
|
|
|
if data["key3"].Field != "value3" {
|
|
t.Errorf("scan map should return value3")
|
|
return
|
|
}
|
|
|
|
err = ScanMapAsync[s](strings.Join([]string{TEST_PREFIX, "map"}, ":"), "4", func(m map[string]s) error {
|
|
if len(m) != 1 {
|
|
t.Errorf("scan map async should return 1")
|
|
return errors.New("scan map async should return 1")
|
|
}
|
|
|
|
if m["4"].Field != "value4" {
|
|
t.Errorf("scan map async should return value4")
|
|
return errors.New("scan map async should return value4")
|
|
}
|
|
|
|
return nil
|
|
})
|
|
|
|
if err != nil {
|
|
t.Errorf("scan map async failed: %v", err)
|
|
return
|
|
}
|
|
}
|
|
|
|
func TestRedisP2PPubsub(t *testing.T) {
|
|
// get redis connection
|
|
if err := getRedisConnection(); err != nil {
|
|
t.Errorf("get redis connection failed: %v", err)
|
|
return
|
|
}
|
|
defer Close()
|
|
|
|
ch := "test-channel"
|
|
|
|
type s struct{}
|
|
|
|
sub, cancel := Subscribe[s](ch)
|
|
defer cancel()
|
|
|
|
wg := sync.WaitGroup{}
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
<-sub
|
|
wg.Done()
|
|
}()
|
|
|
|
// test pubsub
|
|
err := Publish(ch, s{})
|
|
if err != nil {
|
|
t.Errorf("publish failed: %v", err)
|
|
return
|
|
}
|
|
|
|
wg.Wait()
|
|
}
|
|
|
|
func TestRedisP2ARedis(t *testing.T) {
|
|
// get redis connection
|
|
if err := getRedisConnection(); err != nil {
|
|
t.Errorf("get redis connection failed: %v", err)
|
|
return
|
|
}
|
|
defer Close()
|
|
|
|
ch := "test-channel-p2a"
|
|
|
|
type s struct{}
|
|
|
|
wg := sync.WaitGroup{}
|
|
wg.Add(3)
|
|
|
|
swg := sync.WaitGroup{}
|
|
swg.Add(3)
|
|
|
|
for i := 0; i < 3; i++ {
|
|
go func() {
|
|
sub, cancel := Subscribe[s](ch)
|
|
swg.Done()
|
|
defer cancel()
|
|
<-sub
|
|
wg.Done()
|
|
}()
|
|
}
|
|
|
|
swg.Wait()
|
|
|
|
// test pubsub
|
|
err := Publish(ch, s{})
|
|
if err != nil {
|
|
t.Errorf("publish failed: %v", err)
|
|
return
|
|
}
|
|
|
|
wg.Wait()
|
|
}
|
|
|
|
func TestGetRedisOptions(t *testing.T) {
|
|
opts := getRedisOptions("dummy:6379", "", "password", false, 0)
|
|
if opts.TLSConfig != nil {
|
|
t.Errorf("TLSConfig should not be set")
|
|
return
|
|
}
|
|
|
|
opts = getRedisOptions("dummy:6379", "", "password", true, 0)
|
|
if opts.TLSConfig == nil {
|
|
t.Errorf("TLSConfig should be set")
|
|
return
|
|
}
|
|
}
|
|
|
|
func TestSetAndGet(t *testing.T) {
|
|
if err := InitRedisClient("127.0.0.1:6379", "", "difyai123456", false, 0); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer Close()
|
|
|
|
m := map[string]string{
|
|
"key": "hello",
|
|
}
|
|
|
|
err := Store(strings.Join([]string{TEST_PREFIX, "get-test"}, ":"), m, time.Minute)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
val, err := Get[map[string]string](strings.Join([]string{TEST_PREFIX, "get-test"}, ":"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if (*val)["key"] != "hello" {
|
|
t.Fatalf("Get[\"key\"] should be \"hello\"")
|
|
}
|
|
_, err = Del(strings.Join([]string{TEST_PREFIX, "get-test"}, ":"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
val, err = Get[map[string]string](strings.Join([]string{TEST_PREFIX, "get-test"}, ":"))
|
|
if err != ErrNotFound {
|
|
t.Fatalf("Get[\"key\"] should be ErrNotFound")
|
|
}
|
|
}
|
|
|
|
func TestLock(t *testing.T) {
|
|
if err := InitRedisClient("127.0.0.1:6379", "", "difyai123456", false, 0); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer Close()
|
|
|
|
const CONCURRENCY = 10
|
|
const SINGLE_TURN_TIME = 100
|
|
|
|
wg := sync.WaitGroup{}
|
|
wg.Add(CONCURRENCY)
|
|
|
|
waitMilliseconds := int32(0)
|
|
|
|
foo := func() {
|
|
Lock("test-lock", SINGLE_TURN_TIME*time.Millisecond*1000, SINGLE_TURN_TIME*time.Millisecond*1000)
|
|
started := time.Now()
|
|
time.Sleep(SINGLE_TURN_TIME * time.Millisecond)
|
|
defer func() {
|
|
Unlock("test-lock")
|
|
atomic.AddInt32(&waitMilliseconds, int32(time.Since(started).Milliseconds()))
|
|
wg.Done()
|
|
}()
|
|
}
|
|
|
|
for range CONCURRENCY {
|
|
go foo()
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
fmt.Println("waitSeconds", waitMilliseconds)
|
|
|
|
assert.GreaterOrEqual(t, waitMilliseconds, int32(100*CONCURRENCY))
|
|
}
|