From dd136858f1ea40ad3c94191d647487fa4f31926c Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 18 Oct 2024 20:33:49 +0200 Subject: Adding upstream version 9.0.0. Signed-off-by: Daniel Baumann --- modules/queue/base_redis_with_server_test.go | 133 +++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 modules/queue/base_redis_with_server_test.go (limited to 'modules/queue/base_redis_with_server_test.go') 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 +} -- cgit v1.2.3