plugin_test.go 4.5 KB

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