Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 69 additions & 8 deletions pkg/stream/integration/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"net/http"
"net/http/httptest"
"sync/atomic"
"testing"
"time"

Expand Down Expand Up @@ -50,6 +51,8 @@ const (
withGeneratedColumn = true
)

var integrationReplicationSlotCounter uint64

type mockProcessor struct {
eventChan chan *wal.Event
skipEventFn func(event *wal.Event) bool
Expand Down Expand Up @@ -93,19 +96,39 @@ func (m *mockWebhookServer) close() {
}

func runStream(t *testing.T, ctx context.Context, cfg *stream.Config) {
// start the configured stream listener/processor
t.Helper()

done := make(chan error, 1)
go func() {
err := stream.Run(ctx, testLogger(), cfg, false, nil)
require.NoError(t, err)
done <- stream.Run(ctx, testLogger(), cfg, false, nil)
}()

t.Cleanup(func() {
select {
case err := <-done:
require.NoError(t, err)
case <-time.After(10 * time.Second):
t.Error("timeout waiting for stream to stop")
}
})
}

func runSnapshot(t *testing.T, ctx context.Context, cfg *stream.Config) {
// start the configured stream listener/processor
t.Helper()

done := make(chan error, 1)
go func() {
err := stream.Snapshot(ctx, testLogger(), cfg, nil)
require.NoError(t, err)
done <- stream.Snapshot(ctx, testLogger(), cfg, nil)
}()

t.Cleanup(func() {
select {
case err := <-done:
require.NoError(t, err)
case <-time.After(10 * time.Second):
t.Error("timeout waiting for snapshot to stop")
}
})
}

func initStream(t *testing.T, ctx context.Context, url string) {
Expand Down Expand Up @@ -135,17 +158,55 @@ func testLogger() loglib.Logger {
}))
}

func testPostgresListenerCfg() stream.ListenerConfig {
func testPostgresListenerCfg(t *testing.T) stream.ListenerConfig {
t.Helper()

slotName := initReplicationSlotForTest(t, pgurl)
return stream.ListenerConfig{
Postgres: &stream.PostgresListenerConfig{
URL: pgurl,
Replication: pgreplication.Config{
PostgresURL: pgurl,
PostgresURL: pgurl,
ReplicationSlotName: slotName,
},
},
}
}

func initReplicationSlotForTest(t *testing.T, url string) string {
t.Helper()

slotName := fmt.Sprintf("pgstream_it_%d_%d", time.Now().UnixNano(), atomic.AddUint64(&integrationReplicationSlotCounter, 1))
require.NoError(t, stream.Init(context.Background(), &stream.InitConfig{
PostgresURL: url,
ReplicationSlotName: slotName,
}))

t.Cleanup(func() {
dropReplicationSlotForTest(t, url, slotName)
})

return slotName
}

func dropReplicationSlotForTest(t *testing.T, url, slotName string) {
t.Helper()

require.Eventually(t, func() bool {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

conn, err := pglib.NewConn(ctx, url)
if err != nil {
return false
}
defer conn.Close(ctx)

_, err = conn.Exec(ctx, `SELECT pg_drop_replication_slot(slot_name) FROM pg_replication_slots WHERE slot_name = $1`, slotName)
return err == nil
}, 10*time.Second, 200*time.Millisecond, "replication slot %s was not dropped", slotName)
}

func testPostgresListenerCfgWithSnapshot(sourceURL, targetURL string, tables []string) stream.ListenerConfig {
return stream.ListenerConfig{
Postgres: &stream.PostgresListenerConfig{
Expand Down
2 changes: 1 addition & 1 deletion pkg/stream/integration/pg_kafka_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func Test_PostgresToKafka(t *testing.T) {
}

cfg := &stream.Config{
Listener: testPostgresListenerCfg(),
Listener: testPostgresListenerCfg(t),
Processor: testKafkaProcessorCfg(),
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func Test_PostgresToPostgres_BatchCoalesce(t *testing.T) {
}

cfg := &stream.Config{
Listener: testPostgresListenerCfg(),
Listener: testPostgresListenerCfg(t),
Processor: stream.ProcessorConfig{
Postgres: &stream.PostgresProcessorConfig{
BatchWriter: postgres.Config{
Expand Down Expand Up @@ -195,7 +195,7 @@ func Test_PostgresToPostgres_BatchCoalesce_WithCompositeKey(t *testing.T) {
}

cfg := &stream.Config{
Listener: testPostgresListenerCfg(),
Listener: testPostgresListenerCfg(t),
Processor: stream.ProcessorConfig{
Postgres: &stream.PostgresProcessorConfig{
BatchWriter: postgres.Config{
Expand Down Expand Up @@ -270,7 +270,7 @@ func Test_PostgresToPostgres_BatchCoalesce_OnConflict(t *testing.T) {
}

cfg := &stream.Config{
Listener: testPostgresListenerCfg(),
Listener: testPostgresListenerCfg(t),
Processor: stream.ProcessorConfig{
Postgres: &stream.PostgresProcessorConfig{
BatchWriter: postgres.Config{
Expand Down
12 changes: 6 additions & 6 deletions pkg/stream/integration/pg_pg_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func Test_PostgresToPostgres(t *testing.T) {
}

cfg := &stream.Config{
Listener: testPostgresListenerCfg(),
Listener: testPostgresListenerCfg(t),
Processor: testPostgresProcessorCfg(),
}

Expand Down Expand Up @@ -315,7 +315,7 @@ func Test_PostgresToPostgres_IndexesAndConstraints(t *testing.T) {
}

cfg := &stream.Config{
Listener: testPostgresListenerCfg(),
Listener: testPostgresListenerCfg(t),
Processor: testPostgresProcessorCfg(),
}

Expand Down Expand Up @@ -423,7 +423,7 @@ func Test_PostgresToPostgres_IdentityColumns(t *testing.T) {
}

cfg := &stream.Config{
Listener: testPostgresListenerCfg(),
Listener: testPostgresListenerCfg(t),
Processor: testPostgresProcessorCfg(),
}

Expand Down Expand Up @@ -571,7 +571,7 @@ func Test_PostgresToPostgres_AlwaysIdentityUpdate(t *testing.T) {
}

cfg := &stream.Config{
Listener: testPostgresListenerCfg(),
Listener: testPostgresListenerCfg(t),
Processor: testPostgresProcessorCfg(),
}

Expand Down Expand Up @@ -653,7 +653,7 @@ func Test_PostgresToPostgres_Sequences(t *testing.T) {
}

cfg := &stream.Config{
Listener: testPostgresListenerCfg(),
Listener: testPostgresListenerCfg(t),
Processor: testPostgresProcessorCfg(),
}

Expand Down Expand Up @@ -962,7 +962,7 @@ func Test_PostgresToPostgres_LargeIntegerPrecisionWAL(t *testing.T) {
}

cfg := &stream.Config{
Listener: testPostgresListenerCfg(),
Listener: testPostgresListenerCfg(t),
Processor: testPostgresProcessorCfg(),
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func Test_PostgresToPostgres_Transformer(t *testing.T) {
}

cfg := &stream.Config{
Listener: testPostgresListenerCfg(),
Listener: testPostgresListenerCfg(t),
Processor: testPostgresProcessorCfgWithTransformer(pgurl),
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/stream/integration/pg_search_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func Test_PostgresToSearch(t *testing.T) {

t.Run("postgres to opensearch", func(t *testing.T) {
cfg := &stream.Config{
Listener: testPostgresListenerCfg(),
Listener: testPostgresListenerCfg(t),
Processor: testSearchProcessorCfg(store.Config{
OpenSearchURL: opensearchURL,
}),
Expand All @@ -145,7 +145,7 @@ func Test_PostgresToSearch(t *testing.T) {

t.Run("postgres to elasticsearch", func(t *testing.T) {
cfg := &stream.Config{
Listener: testPostgresListenerCfg(),
Listener: testPostgresListenerCfg(t),
Processor: testSearchProcessorCfg(store.Config{
ElasticsearchURL: elasticsearchURL,
}),
Expand All @@ -162,7 +162,7 @@ func Test_PostgresToSearch(t *testing.T) {

cfg := testSearchProcessorCfg(store.Config{ElasticsearchURL: elasticsearchURL})
cfg.Search.Indexer = search.IndexerConfig{HashDocIDs: true}
runStream(t, ctx, &stream.Config{Listener: testPostgresListenerCfg(), Processor: cfg})
runStream(t, ctx, &stream.Config{Listener: testPostgresListenerCfg(t), Processor: cfg})

client, err := elasticsearch.NewClient(elasticsearchURL)
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/stream/integration/pg_webhook_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func Test_PostgresToWebhook(t *testing.T) {
}

cfg := &stream.Config{
Listener: testPostgresListenerCfg(),
Listener: testPostgresListenerCfg(t),
Processor: testWebhookProcessorCfg(),
}

Expand Down
12 changes: 6 additions & 6 deletions pkg/stream/integration/snapshot_pg_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func Test_SnapshotToPostgres(t *testing.T) {
execQueryWithURL(t, ctx, snapshotPGURL, fmt.Sprintf("grant select on %s to test_role_%s", testTable, testTable))

cfg := &stream.Config{
Listener: testPostgresListenerCfgWithSnapshot(snapshotPGURL, targetPGURL, []string{"*.*"}),
Listener: testPostgresListenerCfgWithSnapshot(snapshotPGURL, targetPGURL, []string{testTable}),
Processor: testPostgresProcessorCfg(opts...),
}
initStream(t, ctx, snapshotPGURL)
Expand Down Expand Up @@ -204,7 +204,7 @@ func Test_SnapshotToPostgres_IdentityOnlyTable(t *testing.T) {
`INSERT INTO %s(id, parent_id, name) VALUES (1, 100, 'a'),(2, 200, 'b'),(3, 300, 'c')`, childTable))

cfg := &stream.Config{
Listener: testPostgresListenerCfgWithSnapshot(snapshotPGURL, targetPGURL, []string{"*.*"}),
Listener: testPostgresListenerCfgWithSnapshot(snapshotPGURL, targetPGURL, []string{parentTable, childTable}),
Processor: testPostgresProcessorCfg(opts...),
}
initStream(t, ctx, snapshotPGURL)
Expand Down Expand Up @@ -306,7 +306,7 @@ func Test_SnapshotToPostgres_CubeColumns(t *testing.T) {
testTable))

cfg := &stream.Config{
Listener: testPostgresListenerCfgWithSnapshot(snapshotPGURL, targetPGURL, []string{"*.*"}),
Listener: testPostgresListenerCfgWithSnapshot(snapshotPGURL, targetPGURL, []string{testTable}),
Processor: testPostgresProcessorCfg(opts...),
}
initStream(t, ctx, snapshotPGURL)
Expand Down Expand Up @@ -421,7 +421,7 @@ func Test_SnapshotToPostgres_LtreeColumns(t *testing.T) {
testTable))

cfg := &stream.Config{
Listener: testPostgresListenerCfgWithSnapshot(snapshotPGURL, targetPGURL, []string{"*.*"}),
Listener: testPostgresListenerCfgWithSnapshot(snapshotPGURL, targetPGURL, []string{testTable}),
Processor: testPostgresProcessorCfg(opts...),
}
initStream(t, ctx, snapshotPGURL)
Expand Down Expand Up @@ -706,7 +706,7 @@ func Test_SnapshotToPostgres_IdentityAndGeneratedColumns(t *testing.T) {
`INSERT INTO %s(id, name) VALUES (10, 'alpha'),(20, 'beta'),(30, 'gamma')`, testTable))

cfg := &stream.Config{
Listener: testPostgresListenerCfgWithSnapshot(snapshotPGURL, targetPGURL, []string{"*.*"}),
Listener: testPostgresListenerCfgWithSnapshot(snapshotPGURL, targetPGURL, []string{testTable}),
Processor: testPostgresProcessorCfg(opts...),
}
initStream(t, ctx, snapshotPGURL)
Expand Down Expand Up @@ -797,7 +797,7 @@ func Test_SnapshotToPostgres_LargeIntegerPrecision(t *testing.T) {
testTable))

cfg := &stream.Config{
Listener: testPostgresListenerCfgWithSnapshot(snapshotPGURL, targetPGURL, []string{"*.*"}),
Listener: testPostgresListenerCfgWithSnapshot(snapshotPGURL, targetPGURL, []string{testTable}),
Processor: testPostgresProcessorCfg(opts...),
}
initStream(t, ctx, snapshotPGURL)
Expand Down
Loading