-
Notifications
You must be signed in to change notification settings - Fork 94
/
plugin_test.go
119 lines (89 loc) · 2.77 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
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("ServicesStart", func(t *testing.T) {
t.Parallel()
client, bundle := setup(t)
startClient(ctx, t, client)
riversharedtest.WaitOrTimeout(t, startstop.WaitAllStartedC(
bundle.pluginDriver.maintenanceService,
bundle.pluginDriver.service,
))
})
}
var _ driverPlugin[pgx.Tx] = &TestDriverWithPlugin{}
type TestDriverWithPlugin struct {
*riverpgxv5.Driver
initCalled bool
maintenanceService startstop.Service
service startstop.Service
}
func newDriverWithPlugin(t *testing.T, dbPool *pgxpool.Pool) *TestDriverWithPlugin {
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 &TestDriverWithPlugin{
Driver: riverpgxv5.New(dbPool),
maintenanceService: newService("maintenance service"),
service: newService("other service"),
}
}
func (d *TestDriverWithPlugin) PluginInit(archetype *baseservice.Archetype, client *Client[pgx.Tx]) {
d.initCalled = true
}
func (d *TestDriverWithPlugin) PluginMaintenanceServices() []startstop.Service {
if !d.initCalled {
panic("expected PluginInit to be called before this function")
}
return []startstop.Service{d.maintenanceService}
}
func (d *TestDriverWithPlugin) PluginPilot() riverpilot.Pilot {
if !d.initCalled {
panic("expected PluginInit to be called before this function")
}
return nil
}
func (d *TestDriverWithPlugin) PluginServices() []startstop.Service {
if !d.initCalled {
panic("expected PluginInit to be called before this function")
}
return []startstop.Service{d.service}
}