summaryrefslogtreecommitdiffstats
path: root/modules/queue/base_redis_with_server_test.go
blob: b73404f4e5b26c8b0eb9c7cf63cdad4d73cd4565 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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
}