plugin_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. hashedIdentity, err := plugin.HashedIdentity()
  76. if err != nil {
  77. t.Errorf("get plugin hashed identity failed: %v", err)
  78. return
  79. }
  80. nodes, err := cluster[0].FetchPluginAvailableNodesByHashedId(hashedIdentity)
  81. if err != nil {
  82. t.Errorf("fetch plugin available nodes failed: %v", err)
  83. return
  84. }
  85. if len(nodes) != 1 {
  86. t.Errorf("plugin not scheduled")
  87. return
  88. }
  89. if nodes[0] != cluster[0].id {
  90. t.Errorf("plugin scheduled to wrong node")
  91. return
  92. }
  93. // trigger plugin stop
  94. plugin.TriggerStop()
  95. // wait for the plugin to stop
  96. time.Sleep(time.Second * 1)
  97. // check if the plugin is stopped
  98. nodes, err = cluster[0].FetchPluginAvailableNodesByHashedId(hashedIdentity)
  99. if err != nil {
  100. t.Errorf("fetch plugin available nodes failed: %v", err)
  101. return
  102. }
  103. if len(nodes) != 0 {
  104. t.Errorf("plugin not stopped")
  105. return
  106. }
  107. }
  108. func TestPluginScheduleWhenMasterClusterShutdown(t *testing.T) {
  109. plugins := []fakePlugin{
  110. getRandomPluginRuntime(),
  111. getRandomPluginRuntime(),
  112. }
  113. cluster, err := createSimulationCluster(2)
  114. if err != nil {
  115. t.Errorf("create simulation cluster failed: %v", err)
  116. return
  117. }
  118. launchSimulationCluster(cluster)
  119. defer closeSimulationCluster(cluster, t)
  120. // add plugin to the cluster
  121. for i, plugin := range plugins {
  122. err = cluster[i].RegisterPlugin(&plugin)
  123. if err != nil {
  124. t.Errorf("register plugin failed: %v", err)
  125. return
  126. }
  127. }
  128. // wait for the plugin to be scheduled
  129. time.Sleep(time.Second * 1)
  130. // close master node and wait for new master to be elected
  131. masterIdx := -1
  132. for i, node := range cluster {
  133. if node.IsMaster() {
  134. masterIdx = i
  135. // close the master node
  136. node.Close()
  137. break
  138. }
  139. }
  140. if masterIdx == -1 {
  141. t.Errorf("master node not found")
  142. return
  143. }
  144. // wait for the new master to be elected
  145. i := 0
  146. for ; i < 10; i++ {
  147. time.Sleep(time.Second * 1)
  148. found := false
  149. for i, node := range cluster {
  150. if node.IsMaster() && i != masterIdx {
  151. found = true
  152. break
  153. }
  154. }
  155. if found {
  156. break
  157. }
  158. }
  159. if i == 10 {
  160. t.Errorf("master node is not elected")
  161. return
  162. }
  163. // check if plugins[master_idx] is removed
  164. hashedIdentity, err := plugins[masterIdx].HashedIdentity()
  165. if err != nil {
  166. t.Errorf("get plugin hashed identity failed: %v", err)
  167. return
  168. }
  169. ticker := time.NewTicker(time.Second)
  170. timeout := time.NewTimer(MASTER_GC_INTERVAL * 2)
  171. done := false
  172. for !done {
  173. select {
  174. case <-ticker.C:
  175. nodes, err := cluster[masterIdx].FetchPluginAvailableNodesByHashedId(hashedIdentity)
  176. if err != nil {
  177. t.Errorf("fetch plugin available nodes failed: %v", err)
  178. return
  179. }
  180. if len(nodes) == 0 {
  181. done = true
  182. }
  183. case <-timeout.C:
  184. t.Errorf("plugin not removed")
  185. return
  186. }
  187. }
  188. // check if plugins[1-master_idx] is still scheduled
  189. hashedIdentity, err = plugins[1-masterIdx].HashedIdentity()
  190. if err != nil {
  191. t.Errorf("get plugin hashed identity failed: %v", err)
  192. return
  193. }
  194. nodes, err := cluster[1-masterIdx].FetchPluginAvailableNodesByHashedId(hashedIdentity)
  195. if err != nil {
  196. t.Errorf("fetch plugin available nodes failed: %v", err)
  197. return
  198. }
  199. if len(nodes) != 1 {
  200. t.Errorf("plugin not scheduled")
  201. return
  202. }
  203. }