-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdestination_postgres_test.go
156 lines (139 loc) · 4.21 KB
/
destination_postgres_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
package opinionatedevents
import (
"context"
"database/sql"
"errors"
"testing"
"github.com/stretchr/testify/assert"
)
func TestPostgresDestination(t *testing.T) {
t.Run("inserts an event to the database", func(t *testing.T) {
db := &testDB{}
destination, err := NewPostgresDestination(nil, skipMigrations())
assert.NoError(t, err)
destination.setDB(db)
destination.setRouting(newTestRouting([]string{"default"}))
for i := 0; i < 3; i += 1 {
msg, err := NewMessage("customers.created", nil)
assert.NoError(t, err)
err = destination.Deliver(context.Background(), []*Message{msg})
assert.NoError(t, err)
}
// each message should have been sent in its own transaction
assert.Equal(t, 3, db.beginCount)
assert.Len(t, db.transactions, 3)
for _, tx := range db.transactions {
// each transaction must be committed
assert.Equal(t, 1, tx.commitCount)
assert.Equal(t, 0, tx.rollbackCount)
}
})
t.Run("uses the provided transaction from context", func(t *testing.T) {
db := &testDB{}
destination, err := NewPostgresDestination(nil, skipMigrations())
assert.NoError(t, err)
destination.setDB(db)
destination.setRouting(newTestRouting([]string{"default"}))
newDB := &testDB{}
tx, err := newDB.Begin()
assert.NoError(t, err)
ctx := context.Background()
ctx = WithTx(ctx, tx)
for i := 0; i < 3; i += 1 {
msg, err := NewMessage("customers.created", nil)
assert.NoError(t, err)
err = destination.Deliver(ctx, []*Message{msg})
assert.NoError(t, err)
}
// the internal db should not have been used...
assert.Equal(t, 0, db.beginCount)
assert.Len(t, db.transactions, 0)
// ...but the external one should have
assert.Equal(t, 1, newDB.beginCount)
assert.Len(t, newDB.transactions, 1)
// make sure that the destination did not commit the transaction
assert.Equal(t, 3, newDB.transactions[0].execCount)
assert.Equal(t, 0, newDB.transactions[0].queryCount)
assert.Equal(t, 0, newDB.transactions[0].commitCount)
assert.Equal(t, 0, newDB.transactions[0].rollbackCount)
})
t.Run("messages to all queues are sent in one transaction", func(t *testing.T) {
db := &testDB{}
destination, err := NewPostgresDestination(nil,
skipMigrations(),
)
assert.NoError(t, err)
destination.setDB(db)
destination.setRouting(newTestRouting([]string{"topic.1", "topic.2"}))
msg, err := NewMessage("customers.created", nil)
assert.NoError(t, err)
err = destination.Deliver(context.Background(), []*Message{msg})
assert.NoError(t, err)
// there should be one transaction from the internal db
assert.Equal(t, 1, db.beginCount)
assert.Len(t, db.transactions, 1)
tx := db.transactions[0]
// the messages should have been inserted in a single exec call
assert.Equal(t, 1, tx.execCount)
assert.Equal(t, 0, tx.queryCount)
assert.Equal(t, 1, tx.commitCount)
assert.Equal(t, 0, tx.rollbackCount)
})
}
func skipMigrations() postgresDestinationOption {
return func(dest *postgresDestination) error {
dest.skipMigrations = true
return nil
}
}
type testTx struct {
queries []string
execCount int
queryCount int
commitCount int
rollbackCount int
}
func (ttx *testTx) Exec(query string, args ...any) (sql.Result, error) {
ttx.queries = append(ttx.queries, query)
ttx.execCount += 1
return nil, nil
}
func (ttx *testTx) Query(query string, args ...any) (*sql.Rows, error) {
ttx.queries = append(ttx.queries, query)
ttx.queryCount += 1
return nil, nil
}
func (ttx *testTx) Commit() error {
if ttx.commitCount+ttx.rollbackCount > 0 {
panic(errors.New("cannot commit after commit or rollback"))
}
ttx.commitCount += 1
return nil
}
func (ttx *testTx) Rollback() error {
if ttx.commitCount+ttx.rollbackCount > 0 {
// a rollback after either a commit or rollback is no-op
return nil
}
ttx.rollbackCount += 1
return nil
}
type testDB struct {
beginCount int
transactions []*testTx
}
func (tdb *testDB) Begin() (sqlTx, error) {
tx := &testTx{}
tdb.transactions = append(tdb.transactions, tx)
tdb.beginCount += 1
return tx, nil
}
type testRouting struct {
_queues []string
}
func newTestRouting(queues []string) *testRouting {
return &testRouting{_queues: queues}
}
func (tr *testRouting) queues(tx sqlTx, topic string) ([]string, error) {
return tr._queues, nil
}