plugin_test.go 4.7 KB

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