plugin_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. package cluster
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/google/uuid"
  6. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager/positive_manager"
  7. "github.com/langgenius/dify-plugin-daemon/internal/types/entities"
  8. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
  9. )
  10. type fakePlugin struct {
  11. entities.PluginRuntime
  12. positive_manager.PositivePluginRuntime
  13. }
  14. func (r *fakePlugin) InitEnvironment() error {
  15. return nil
  16. }
  17. func (r *fakePlugin) Checksum() string {
  18. return ""
  19. }
  20. func (r *fakePlugin) Identity() (string, error) {
  21. return "", nil
  22. }
  23. func (r *fakePlugin) StartPlugin() error {
  24. return nil
  25. }
  26. func (r *fakePlugin) Type() entities.PluginRuntimeType {
  27. return entities.PLUGIN_RUNTIME_TYPE_LOCAL
  28. }
  29. func (r *fakePlugin) Wait() (<-chan bool, error) {
  30. return nil, nil
  31. }
  32. func getRandomPluginRuntime() fakePlugin {
  33. return fakePlugin{
  34. PluginRuntime: entities.PluginRuntime{
  35. Config: plugin_entities.PluginDeclaration{
  36. Name: uuid.New().String(),
  37. Version: "0.0.1",
  38. Type: plugin_entities.PluginType,
  39. Author: "Yeuoly",
  40. CreatedAt: time.Now(),
  41. Plugins: []string{"test"},
  42. },
  43. },
  44. }
  45. }
  46. func TestPluginScheduleLifetime(t *testing.T) {
  47. plugin := getRandomPluginRuntime()
  48. cluster, err := createSimulationCluster(1)
  49. if err != nil {
  50. t.Errorf("create simulation cluster failed: %v", err)
  51. return
  52. }
  53. launchSimulationCluster(cluster, t)
  54. defer closeSimulationCluster(cluster, t)
  55. time.Sleep(time.Second * 1)
  56. // add plugin to the cluster
  57. err = cluster[0].RegisterPlugin(&plugin)
  58. if err != nil {
  59. t.Errorf("register plugin failed: %v", err)
  60. return
  61. }
  62. hashed_identity, err := plugin.HashedIdentity()
  63. if err != nil {
  64. t.Errorf("get plugin hashed identity failed: %v", err)
  65. return
  66. }
  67. nodes, err := cluster[0].FetchPluginAvailableNodesByHashedId(hashed_identity)
  68. if err != nil {
  69. t.Errorf("fetch plugin available nodes failed: %v", err)
  70. return
  71. }
  72. if len(nodes) != 1 {
  73. t.Errorf("plugin not scheduled")
  74. return
  75. }
  76. if nodes[0] != cluster[0].id {
  77. t.Errorf("plugin scheduled to wrong node")
  78. return
  79. }
  80. // trigger plugin stop
  81. plugin.TriggerStop()
  82. // wait for the plugin to stop
  83. time.Sleep(time.Second * 1)
  84. // check if the plugin is stopped
  85. nodes, err = cluster[0].FetchPluginAvailableNodesByHashedId(hashed_identity)
  86. if err != nil {
  87. t.Errorf("fetch plugin available nodes failed: %v", err)
  88. return
  89. }
  90. if len(nodes) != 0 {
  91. t.Errorf("plugin not stopped")
  92. return
  93. }
  94. }
  95. func TestPluginScheduleWhenMasterClusterShutdown(t *testing.T) {
  96. plugins := []fakePlugin{
  97. getRandomPluginRuntime(),
  98. getRandomPluginRuntime(),
  99. }
  100. cluster, err := createSimulationCluster(2)
  101. if err != nil {
  102. t.Errorf("create simulation cluster failed: %v", err)
  103. return
  104. }
  105. launchSimulationCluster(cluster, t)
  106. defer closeSimulationCluster(cluster, t)
  107. // add plugin to the cluster
  108. for i, plugin := range plugins {
  109. err = cluster[i].RegisterPlugin(&plugin)
  110. if err != nil {
  111. t.Errorf("register plugin failed: %v", err)
  112. return
  113. }
  114. }
  115. // wait for the plugin to be scheduled
  116. time.Sleep(time.Second * 1)
  117. // close master node and wait for new master to be elected
  118. master_idx := -1
  119. for i, node := range cluster {
  120. if node.IsMaster() {
  121. master_idx = i
  122. // close the master node
  123. node.Close()
  124. break
  125. }
  126. }
  127. if master_idx == -1 {
  128. t.Errorf("master node not found")
  129. return
  130. }
  131. // wait for the new master to be elected
  132. i := 0
  133. for ; i < 10; i++ {
  134. time.Sleep(time.Second * 1)
  135. found := false
  136. for i, node := range cluster {
  137. if node.IsMaster() && i != master_idx {
  138. found = true
  139. break
  140. }
  141. }
  142. if found {
  143. break
  144. }
  145. }
  146. if i == 10 {
  147. t.Errorf("master node is not elected")
  148. return
  149. }
  150. // check if plugins[master_idx] is removed
  151. hashed_identity, err := plugins[master_idx].HashedIdentity()
  152. if err != nil {
  153. t.Errorf("get plugin hashed identity failed: %v", err)
  154. return
  155. }
  156. ticker := time.NewTicker(time.Second)
  157. timeout := time.NewTimer(MASTER_GC_INTERVAL * 2)
  158. done := false
  159. for !done {
  160. select {
  161. case <-ticker.C:
  162. nodes, err := cluster[master_idx].FetchPluginAvailableNodesByHashedId(hashed_identity)
  163. if err != nil {
  164. t.Errorf("fetch plugin available nodes failed: %v", err)
  165. return
  166. }
  167. if len(nodes) == 0 {
  168. done = true
  169. }
  170. case <-timeout.C:
  171. t.Errorf("plugin not removed")
  172. return
  173. }
  174. }
  175. // check if plugins[1-master_idx] is still scheduled
  176. hashed_identity, err = plugins[1-master_idx].HashedIdentity()
  177. if err != nil {
  178. t.Errorf("get plugin hashed identity failed: %v", err)
  179. return
  180. }
  181. nodes, err := cluster[1-master_idx].FetchPluginAvailableNodesByHashedId(hashed_identity)
  182. if err != nil {
  183. t.Errorf("fetch plugin available nodes failed: %v", err)
  184. return
  185. }
  186. if len(nodes) != 1 {
  187. t.Errorf("plugin not scheduled")
  188. return
  189. }
  190. }