summaryrefslogtreecommitdiff
path: root/tests/plugins/redis/redis_plugin_test.go
blob: eba05752cc5c7d36810256f469ea25b8dbbe7ac9 (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
package redis

import (
	"fmt"
	"os"
	"os/signal"
	"sync"
	"syscall"
	"testing"

	"github.com/alicebob/miniredis/v2"
	"github.com/golang/mock/gomock"
	"github.com/spiral/endure"
	"github.com/spiral/roadrunner/v2/plugins/config"
	"github.com/spiral/roadrunner/v2/plugins/redis"
	"github.com/spiral/roadrunner/v2/tests/mocks"
	"github.com/stretchr/testify/assert"
)

func redisConfig(port string) string {
	cfg := `
redis:
  addrs:
    - 'localhost:%s'
  master_name: ''
  username: ''
  password: ''
  db: 0
  sentinel_password: ''
  route_by_latency: false
  route_randomly: false
  dial_timeout: 0
  max_retries: 1
  min_retry_backoff: 0
  max_retry_backoff: 0
  pool_size: 0
  min_idle_conns: 0
  max_conn_age: 0
  read_timeout: 0
  write_timeout: 0
  pool_timeout: 0
  idle_timeout: 0
  idle_check_freq: 0
  read_only: false
`
	return fmt.Sprintf(cfg, port)
}

func TestRedisInit(t *testing.T) {
	cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.ErrorLevel))
	if err != nil {
		t.Fatal(err)
	}

	s, err := miniredis.Run()
	assert.NoError(t, err)

	c := redisConfig(s.Port())

	cfg := &config.Viper{}
	cfg.Type = "yaml"
	cfg.ReadInCfg = []byte(c)

	controller := gomock.NewController(t)
	mockLogger := mocks.NewMockLogger(controller)

	err = cont.RegisterAll(
		cfg,
		mockLogger,
		&redis.Plugin{},
		&Plugin1{},
	)
	assert.NoError(t, err)

	err = cont.Init()
	if err != nil {
		t.Fatal(err)
	}

	ch, err := cont.Serve()
	assert.NoError(t, err)

	sig := make(chan os.Signal, 1)
	signal.Notify(sig, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)

	wg := &sync.WaitGroup{}
	wg.Add(1)

	stopCh := make(chan struct{}, 1)

	go func() {
		defer wg.Done()
		for {
			select {
			case e := <-ch:
				assert.Fail(t, "error", e.Error.Error())
				err = cont.Stop()
				if err != nil {
					assert.FailNow(t, "error", err.Error())
				}
			case <-sig:
				err = cont.Stop()
				if err != nil {
					assert.FailNow(t, "error", err.Error())
				}
				return
			case <-stopCh:
				// timeout
				err = cont.Stop()
				if err != nil {
					assert.FailNow(t, "error", err.Error())
				}
				return
			}
		}
	}()

	stopCh <- struct{}{}
	wg.Wait()
}