plugin_test.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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/basic_runtime"
  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. basic_runtime.BasicChecksum
  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. // TODO: I need to implement this test, now it's randomly working
  110. // func TestPluginScheduleWhenMasterClusterShutdown(t *testing.T) {
  111. // plugins := []fakePlugin{
  112. // getRandomPluginRuntime(),
  113. // getRandomPluginRuntime(),
  114. // }
  115. // cluster, err := createSimulationCluster(2)
  116. // if err != nil {
  117. // t.Errorf("create simulation cluster failed: %v", err)
  118. // return
  119. // }
  120. // // set master gc interval to 1 second
  121. // for _, node := range cluster {
  122. // node.nodeDisconnectedTimeout = time.Second * 2
  123. // node.masterGcInterval = time.Second * 1
  124. // node.pluginSchedulerInterval = time.Second * 1
  125. // node.pluginSchedulerTickerInterval = time.Second * 1
  126. // node.updateNodeStatusInterval = time.Second * 1
  127. // node.pluginDeactivatedTimeout = time.Second * 2
  128. // node.showLog = true
  129. // }
  130. // launchSimulationCluster(cluster)
  131. // defer closeSimulationCluster(cluster, t)
  132. // // add plugin to the cluster
  133. // for i, plugin := range plugins {
  134. // err = cluster[i].RegisterPlugin(&plugin)
  135. // if err != nil {
  136. // t.Errorf("register plugin failed: %v", err)
  137. // return
  138. // }
  139. // }
  140. // // wait for the plugin to be scheduled
  141. // time.Sleep(time.Second * 1)
  142. // // close master node and wait for new master to be elected
  143. // masterIdx := -1
  144. // for i, node := range cluster {
  145. // if node.IsMaster() {
  146. // masterIdx = i
  147. // // close the master node
  148. // node.Close()
  149. // break
  150. // }
  151. // }
  152. // if masterIdx == -1 {
  153. // t.Errorf("master node not found")
  154. // return
  155. // }
  156. // // wait for the new master to be elected
  157. // i := 0
  158. // for ; i < 10; i++ {
  159. // time.Sleep(time.Second * 1)
  160. // found := false
  161. // for i, node := range cluster {
  162. // if node.IsMaster() && i != masterIdx {
  163. // found = true
  164. // break
  165. // }
  166. // }
  167. // if found {
  168. // break
  169. // }
  170. // }
  171. // if i == 10 {
  172. // t.Errorf("master node is not elected")
  173. // return
  174. // }
  175. // // check if plugins[master_idx] is removed
  176. // identity, err := plugins[masterIdx].Identity()
  177. // if err != nil {
  178. // t.Errorf("get plugin identity failed: %v", err)
  179. // return
  180. // }
  181. // hashedIdentity := plugin_entities.HashedIdentity(identity.String())
  182. // ticker := time.NewTicker(time.Second)
  183. // timeout := time.NewTimer(time.Second * 20)
  184. // done := false
  185. // for !done {
  186. // select {
  187. // case <-ticker.C:
  188. // nodes, err := cluster[masterIdx].FetchPluginAvailableNodesByHashedId(hashedIdentity)
  189. // if err != nil {
  190. // t.Errorf("fetch plugin available nodes failed: %v", err)
  191. // return
  192. // }
  193. // if len(nodes) == 0 {
  194. // done = true
  195. // }
  196. // case <-timeout.C:
  197. // t.Errorf("plugin not removed")
  198. // return
  199. // }
  200. // }
  201. // // check if plugins[1-master_idx] is still scheduled
  202. // identity, err = plugins[1-masterIdx].Identity()
  203. // if err != nil {
  204. // t.Errorf("get plugin identity failed: %v", err)
  205. // return
  206. // }
  207. // hashedIdentity = plugin_entities.HashedIdentity(identity.String())
  208. // nodes, err := cluster[1-masterIdx].FetchPluginAvailableNodesByHashedId(hashedIdentity)
  209. // if err != nil {
  210. // t.Errorf("fetch plugin available nodes failed: %v", err)
  211. // return
  212. // }
  213. // if len(nodes) != 1 {
  214. // t.Errorf("plugin not scheduled")
  215. // return
  216. // }
  217. // }