plugin_test.go 4.9 KB

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