plugin_test.go 4.7 KB

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