plugin_test.go 4.6 KB

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