plugin_test.go 5.2 KB

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