-
Notifications
You must be signed in to change notification settings - Fork 185
Expand file tree
/
Copy pathplugin_test.go
More file actions
198 lines (148 loc) · 4.56 KB
/
Copy pathplugin_test.go
File metadata and controls
198 lines (148 loc) · 4.56 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package river
import (
"context"
"testing"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/stretchr/testify/require"
"github.com/riverqueue/river/riverdbtest"
"github.com/riverqueue/river/riverdriver/riverpgxv5"
"github.com/riverqueue/river/rivershared/baseservice"
"github.com/riverqueue/river/rivershared/riverpilot"
"github.com/riverqueue/river/rivershared/riversharedtest"
"github.com/riverqueue/river/rivershared/startstop"
)
func TestClientDriverPlugin(t *testing.T) {
t.Parallel()
ctx := context.Background()
type testBundle struct {
pluginDriver *TestDriverWithPlugin
}
setup := func(t *testing.T) (*Client[pgx.Tx], *testBundle) {
t.Helper()
var (
dbPool = riversharedtest.DBPool(ctx, t)
driver = riverpgxv5.New(dbPool)
schema = riverdbtest.TestSchema(ctx, t, driver, nil)
config = newTestConfig(t, schema)
)
pluginDriver := newDriverWithPlugin(t, dbPool)
client, err := NewClient(pluginDriver, config)
require.NoError(t, err)
return client, &testBundle{
pluginDriver: pluginDriver,
}
}
t.Run("InitCalled", func(t *testing.T) {
t.Parallel()
client, bundle := setup(t)
startClient(ctx, t, client)
require.True(t, bundle.pluginDriver.initCalled)
})
}
var _ driverPlugin[pgx.Tx] = &TestDriverWithPlugin{}
type TestDriverWithPlugin struct {
*riverpgxv5.Driver
initCalled bool
pilot riverpilot.Pilot
}
func newDriverWithPlugin(t *testing.T, dbPool *pgxpool.Pool) *TestDriverWithPlugin {
t.Helper()
return &TestDriverWithPlugin{
Driver: riverpgxv5.New(dbPool),
}
}
func (d *TestDriverWithPlugin) PluginInit(archetype *baseservice.Archetype) {
d.initCalled = true
}
func (d *TestDriverWithPlugin) PluginPilot() riverpilot.Pilot {
if !d.initCalled {
panic("expected PluginInit to be called before this function")
}
return d.pilot
}
func TestClientPilotPlugin(t *testing.T) {
t.Parallel()
ctx := context.Background()
type testBundle struct {
pluginDriver *TestDriverWithPlugin
pluginPilot *TestPilotWithPlugin
}
setup := func(t *testing.T, leaderElectionDisabled bool) (*Client[pgx.Tx], *testBundle) {
t.Helper()
var (
dbPool = riversharedtest.DBPool(ctx, t)
driver = riverpgxv5.New(dbPool)
schema = riverdbtest.TestSchema(ctx, t, driver, nil)
config = newTestConfig(t, schema)
pluginDriver = newDriverWithPlugin(t, dbPool)
pluginPilot = newPilotWithPlugin(t)
)
config.LeaderElectionDisabled = leaderElectionDisabled
pluginDriver.pilot = pluginPilot
client, err := NewClient(pluginDriver, config)
require.NoError(t, err)
return client, &testBundle{
pluginDriver: pluginDriver,
pluginPilot: pluginPilot,
}
}
t.Run("LeaderElectionDisabled", func(t *testing.T) {
t.Parallel()
client, bundle := setup(t, true)
startClient(ctx, t, client)
riversharedtest.WaitOrTimeout(t, client.baseStartStop.Started())
riversharedtest.WaitOrTimeout(t, bundle.pluginPilot.service.Started())
require.False(t, bundle.pluginPilot.maintenanceServicesCalled)
select {
case <-bundle.pluginPilot.maintenanceService.Started():
t.Fatal("plugin maintenance service should not have started")
default:
}
})
t.Run("ServicesStart", func(t *testing.T) {
t.Parallel()
client, bundle := setup(t, false)
startClient(ctx, t, client)
riversharedtest.WaitOrTimeout(t, startstop.WaitAllStartedC(
bundle.pluginPilot.maintenanceService,
bundle.pluginPilot.service,
))
})
}
var _ pilotPlugin = &TestPilotWithPlugin{}
type TestPilotWithPlugin struct {
riverpilot.StandardPilot
maintenanceService startstop.Service
maintenanceServicesCalled bool
service startstop.Service
}
func newPilotWithPlugin(t *testing.T) *TestPilotWithPlugin {
t.Helper()
newService := func(name string) startstop.Service {
return startstop.StartStopFunc(func(ctx context.Context, shouldStart bool, started, stopped func()) error {
if !shouldStart {
return nil
}
go func() {
started()
defer stopped() // this defer should come first so it's last out
t.Logf("Test service started: %s", name)
<-ctx.Done()
}()
return nil
})
}
return &TestPilotWithPlugin{
StandardPilot: riverpilot.StandardPilot{},
maintenanceService: newService("maintenance service"),
service: newService("other service"),
}
}
func (d *TestPilotWithPlugin) PluginMaintenanceServices() []startstop.Service {
d.maintenanceServicesCalled = true
return []startstop.Service{d.maintenanceService}
}
func (d *TestPilotWithPlugin) PluginServices() []startstop.Service {
return []startstop.Service{d.service}
}