gitea源码

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package queue
  4. import (
  5. "context"
  6. "os"
  7. "os/exec"
  8. "testing"
  9. "time"
  10. "code.gitea.io/gitea/modules/nosql"
  11. "code.gitea.io/gitea/modules/setting"
  12. "github.com/stretchr/testify/assert"
  13. "github.com/stretchr/testify/require"
  14. )
  15. func waitRedisReady(conn string, dur time.Duration) (ready bool) {
  16. ctxTimed, cancel := context.WithTimeout(context.Background(), time.Second*5)
  17. defer cancel()
  18. for t := time.Now(); ; time.Sleep(50 * time.Millisecond) {
  19. ret := nosql.GetManager().GetRedisClient(conn).Ping(ctxTimed)
  20. if ret.Err() == nil {
  21. return true
  22. }
  23. if time.Since(t) > dur {
  24. return false
  25. }
  26. }
  27. }
  28. func redisServerCmd(t *testing.T) *exec.Cmd {
  29. redisServerProg, err := exec.LookPath("redis-server")
  30. if err != nil {
  31. return nil
  32. }
  33. c := &exec.Cmd{
  34. Path: redisServerProg,
  35. Args: []string{redisServerProg, "--bind", "127.0.0.1", "--port", "6379"},
  36. Dir: t.TempDir(),
  37. Stdin: os.Stdin,
  38. Stdout: os.Stdout,
  39. Stderr: os.Stderr,
  40. }
  41. return c
  42. }
  43. func TestBaseRedis(t *testing.T) {
  44. var redisServer *exec.Cmd
  45. defer func() {
  46. if redisServer != nil {
  47. _ = redisServer.Process.Signal(os.Interrupt)
  48. _ = redisServer.Wait()
  49. }
  50. }()
  51. if !waitRedisReady("redis://127.0.0.1:6379/0", 0) {
  52. redisServer = redisServerCmd(t)
  53. if redisServer == nil && os.Getenv("CI") == "" {
  54. t.Skip("redis-server not found")
  55. return
  56. }
  57. assert.NoError(t, redisServer.Start())
  58. require.True(t, waitRedisReady("redis://127.0.0.1:6379/0", 5*time.Second), "start redis-server")
  59. }
  60. testQueueBasic(t, newBaseRedisSimple, toBaseConfig("baseRedis", setting.QueueSettings{Length: 10}), false)
  61. testQueueBasic(t, newBaseRedisUnique, toBaseConfig("baseRedisUnique", setting.QueueSettings{Length: 10}), true)
  62. }