-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpgelect_test.go
85 lines (65 loc) · 1.62 KB
/
pgelect_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
package pgelect_test
import (
"context"
"errors"
"log/slog"
"net/url"
"os"
"os/signal"
"syscall"
"testing"
"time"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/sync/errgroup"
"github.com/davidsbond/pgelect"
)
func TestRun(t *testing.T) {
db := testDB(t)
ctx, cancel := signal.NotifyContext(t.Context(), syscall.SIGINT, syscall.SIGTERM, syscall.SIGKILL)
defer cancel()
updates := make(chan pgelect.CandidateStatus, 1)
gotUpdate := false
group, ctx := errgroup.WithContext(ctx)
group.Go(func() error {
for status := range updates {
assert.NotEqual(t, pgelect.CandidateStatusUnknown, status)
gotUpdate = true
}
return nil
})
group.Go(func() error {
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug}))
ctx, cancel := context.WithTimeout(ctx, time.Second*10)
defer cancel()
defer close(updates)
return pgelect.Run(ctx, db, updates,
pgelect.WithElectionInterval(time.Second),
pgelect.WithLockID(69),
pgelect.WithLogger(logger),
)
})
err := group.Wait()
if !errors.Is(err, context.DeadlineExceeded) {
require.NoError(t, err)
}
assert.True(t, gotUpdate)
}
func testDB(t *testing.T) *pgxpool.Pool {
t.Helper()
u := &url.URL{
Scheme: "postgres",
Host: "localhost:5432",
User: url.UserPassword("postgres", "postgres"),
Path: "postgres",
RawQuery: "sslmode=disable",
}
db, err := pgxpool.New(t.Context(), u.String())
require.NoError(t, err)
t.Cleanup(func() {
db.Close()
})
require.NoError(t, db.Ping(t.Context()))
return db
}