plugin_test.go 5.4 KB

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