-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathplugin_test.go
163 lines (120 loc) · 3.61 KB
/
plugin_test.go
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
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/internal/riverinternaltest"
"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()
pluginDriver := newDriverWithPlugin(t, riverinternaltest.TestDB(ctx, t))
client, err := NewClient(pluginDriver, newTestConfig(t, nil))
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) (*Client[pgx.Tx], *testBundle) {
t.Helper()
pluginDriver := newDriverWithPlugin(t, riverinternaltest.TestDB(ctx, t))
pluginPilot := newPilotWithPlugin(t)
pluginDriver.pilot = pluginPilot
client, err := NewClient(pluginDriver, newTestConfig(t, nil))
require.NoError(t, err)
return client, &testBundle{
pluginDriver: pluginDriver,
pluginPilot: pluginPilot,
}
}
t.Run("ServicesStart", func(t *testing.T) {
t.Parallel()
client, bundle := setup(t)
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
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 {
return []startstop.Service{d.maintenanceService}
}
func (d *TestPilotWithPlugin) PluginServices() []startstop.Service {
return []startstop.Service{d.service}
}