diff options
author | Daniel Baumann <daniel@debian.org> | 2024-10-18 20:33:49 +0200 |
---|---|---|
committer | Daniel Baumann <daniel@debian.org> | 2024-12-12 23:57:56 +0100 |
commit | e68b9d00a6e05b3a941f63ffb696f91e554ac5ec (patch) | |
tree | 97775d6c13b0f416af55314eb6a89ef792474615 /modules/queue/base_redis_with_server_test.go | |
parent | Initial commit. (diff) | |
download | forgejo-e68b9d00a6e05b3a941f63ffb696f91e554ac5ec.tar.xz forgejo-e68b9d00a6e05b3a941f63ffb696f91e554ac5ec.zip |
Adding upstream version 9.0.3.
Signed-off-by: Daniel Baumann <daniel@debian.org>
Diffstat (limited to 'modules/queue/base_redis_with_server_test.go')
-rw-r--r-- | modules/queue/base_redis_with_server_test.go | 133 |
1 files changed, 133 insertions, 0 deletions
diff --git a/modules/queue/base_redis_with_server_test.go b/modules/queue/base_redis_with_server_test.go new file mode 100644 index 0000000..b73404f --- /dev/null +++ b/modules/queue/base_redis_with_server_test.go @@ -0,0 +1,133 @@ +package queue + +import ( + "context" + "os" + "os/exec" + "testing" + "time" + + "code.gitea.io/gitea/modules/nosql" + "code.gitea.io/gitea/modules/setting" + + "github.com/stretchr/testify/suite" +) + +const defaultTestRedisServer = "127.0.0.1:6379" + +type baseRedisWithServerTestSuite struct { + suite.Suite +} + +func TestBaseRedisWithServer(t *testing.T) { + suite.Run(t, &baseRedisWithServerTestSuite{}) +} + +func (suite *baseRedisWithServerTestSuite) TestNormal() { + redisAddress := "redis://" + suite.testRedisHost() + "/0" + queueSettings := setting.QueueSettings{ + Length: 10, + ConnStr: redisAddress, + } + + redisServer, accessible := suite.startRedisServer(redisAddress) + + // If it's accessible, but redisServer command is nil, that means we are using + // an already running redis server. + if redisServer == nil && !accessible { + suite.T().Skip("redis-server not found in Forgejo test yet") + + return + } + + defer func() { + if redisServer != nil { + _ = redisServer.Process.Signal(os.Interrupt) + _ = redisServer.Wait() + } + }() + + testQueueBasic(suite.T(), newBaseRedisSimple, toBaseConfig("baseRedis", queueSettings), false) + testQueueBasic(suite.T(), newBaseRedisUnique, toBaseConfig("baseRedisUnique", queueSettings), true) +} + +func (suite *baseRedisWithServerTestSuite) TestWithPrefix() { + redisAddress := "redis://" + suite.testRedisHost() + "/0?prefix=forgejo:queue:" + queueSettings := setting.QueueSettings{ + Length: 10, + ConnStr: redisAddress, + } + + redisServer, accessible := suite.startRedisServer(redisAddress) + + // If it's accessible, but redisServer command is nil, that means we are using + // an already running redis server. + if redisServer == nil && !accessible { + suite.T().Skip("redis-server not found in Forgejo test yet") + + return + } + + defer func() { + if redisServer != nil { + _ = redisServer.Process.Signal(os.Interrupt) + _ = redisServer.Wait() + } + }() + + testQueueBasic(suite.T(), newBaseRedisSimple, toBaseConfig("baseRedis", queueSettings), false) + testQueueBasic(suite.T(), newBaseRedisUnique, toBaseConfig("baseRedisUnique", queueSettings), true) +} + +func (suite *baseRedisWithServerTestSuite) startRedisServer(address string) (*exec.Cmd, bool) { + var redisServer *exec.Cmd + + if !suite.waitRedisReady(address, 0) { + redisServerProg, err := exec.LookPath("redis-server") + if err != nil { + return nil, false + } + redisServer = &exec.Cmd{ + Path: redisServerProg, + Args: []string{redisServerProg, "--bind", "127.0.0.1", "--port", "6379"}, + Dir: suite.T().TempDir(), + Stdin: os.Stdin, + Stdout: os.Stdout, + Stderr: os.Stderr, + } + + suite.Require().NoError(redisServer.Start()) + + if !suite.True(suite.waitRedisReady(address, 5*time.Second), "start redis-server") { + // Return with redis server even if it's not available. It was started, + // even if it's not reachable for any reasons, it's still started, the + // parent will close it. + return redisServer, false + } + } + + return redisServer, true +} + +func (suite *baseRedisWithServerTestSuite) waitRedisReady(conn string, dur time.Duration) (ready bool) { + ctxTimed, cancel := context.WithTimeout(context.Background(), time.Second*5) + defer cancel() + for t := time.Now(); ; time.Sleep(50 * time.Millisecond) { + ret := nosql.GetManager().GetRedisClient(conn).Ping(ctxTimed) + if ret.Err() == nil { + return true + } + if time.Since(t) > dur { + return false + } + } +} + +func (suite *baseRedisWithServerTestSuite) testRedisHost() string { + value := os.Getenv("TEST_REDIS_SERVER") + if value != "" { + return value + } + + return defaultTestRedisServer +} |