forked from redpanda-data/connect
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
984 additions
and
458 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,14 +4,15 @@ import ( | |
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/benthosdev/benthos/v4/public/service" | ||
) | ||
|
||
func TestCRDBConfigParse(t *testing.T) { | ||
conf := ` | ||
crdb_changefeed: | ||
cockroach_changefeed: | ||
dsn: postgresql://dan:[email protected]:26257/defaultdb?sslmode=require&options=--cluster%3Dportly-impala-2852 | ||
tables: | ||
- strm_2 | ||
|
@@ -26,7 +27,9 @@ options: | |
selectConfig, err := spec.ParseYAML(conf, env) | ||
require.NoError(t, err) | ||
|
||
selectInput, err := newCRDBChangefeedInputFromConfig(selectConfig, nil) | ||
selectInput, err := newCRDBChangefeedInputFromConfig(selectConfig, service.MockResources()) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, "EXPERIMENTAL CHANGEFEED FOR strm_2 WITH UPDATED, CURSOR='1637953249519902405.0000000000'", selectInput.statement) | ||
require.NoError(t, selectInput.Close(context.Background())) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package crdb_test | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/Jeffail/gabs/v2" | ||
"github.com/jackc/pgx/v4/pgxpool" | ||
"github.com/ory/dockertest/v3" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
_ "github.com/lib/pq" | ||
|
||
"github.com/benthosdev/benthos/v4/internal/integration" | ||
_ "github.com/benthosdev/benthos/v4/public/components/io" | ||
_ "github.com/benthosdev/benthos/v4/public/components/pure" | ||
) | ||
|
||
func TestIntegrationExploration(t *testing.T) { | ||
integration.CheckSkip(t) | ||
t.Parallel() | ||
|
||
pool, err := dockertest.NewPool("") | ||
require.NoError(t, err) | ||
|
||
pool.MaxWait = time.Second * 30 | ||
resource, err := pool.RunWithOptions(&dockertest.RunOptions{ | ||
Repository: "cockroachdb/cockroach", | ||
Tag: "latest", | ||
Cmd: []string{"start-single-node", "--insecure"}, | ||
ExposedPorts: []string{"8080", "26257"}, | ||
}) | ||
require.NoError(t, err) | ||
t.Cleanup(func() { | ||
assert.NoError(t, pool.Purge(resource)) | ||
}) | ||
|
||
port := resource.GetPort("26257/tcp") | ||
dsn := fmt.Sprintf("postgres://root@localhost:%v/defaultdb?sslmode=disable", port) | ||
|
||
var pgpool *pgxpool.Pool | ||
require.NoError(t, resource.Expire(900)) | ||
|
||
require.NoError(t, pool.Retry(func() error { | ||
if pgpool == nil { | ||
if pgpool, err = pgxpool.Connect(context.Background(), dsn); err != nil { | ||
return err | ||
} | ||
} | ||
// Enable changefeeds | ||
if _, err = pgpool.Exec(context.Background(), "SET CLUSTER SETTING kv.rangefeed.enabled = true;"); err != nil { | ||
return err | ||
} | ||
// Create table | ||
_, err = pgpool.Exec(context.Background(), "CREATE TABLE foo (a INT PRIMARY KEY);") | ||
return err | ||
})) | ||
t.Cleanup(func() { | ||
pgpool.Close() | ||
}) | ||
|
||
cfdb, err := sql.Open("postgres", dsn) | ||
require.NoError(t, err) | ||
|
||
// Create a backlog of rows | ||
i := 0 | ||
for ; i < 100; i++ { | ||
// Insert some rows | ||
if _, err = pgpool.Exec(context.Background(), fmt.Sprintf("INSERT INTO foo VALUES (%v);", i)); err != nil { | ||
return | ||
} | ||
} | ||
|
||
rowsCtx, done := context.WithCancel(context.Background()) | ||
|
||
rows, err := cfdb.QueryContext(rowsCtx, "EXPERIMENTAL CHANGEFEED FOR foo WITH UPDATED") | ||
require.NoError(t, err) | ||
|
||
var latestCursor string | ||
for j := 0; j < 100; j++ { | ||
require.True(t, rows.Next()) | ||
|
||
var a, b, c []byte | ||
require.NoError(t, rows.Scan(&a, &b, &c)) | ||
|
||
gObj, err := gabs.ParseJSON(c) | ||
require.NoError(t, err) | ||
|
||
latestCursor, _ = gObj.S("updated").Data().(string) | ||
assert.Equal(t, float64(j), gObj.S("after", "a").Data(), gObj.String()) | ||
} | ||
|
||
done() | ||
|
||
cfdb.Close() | ||
rows.Close() | ||
|
||
// Insert some more rows | ||
for ; i < 150; i++ { | ||
if _, err = pgpool.Exec(context.Background(), fmt.Sprintf("INSERT INTO foo VALUES (%v);", i)); err != nil { | ||
t.Error(err) | ||
} | ||
} | ||
|
||
// Create a new changefeed with a cursor set to the latest updated value | ||
cfdb, err = sql.Open("postgres", dsn) | ||
require.NoError(t, err) | ||
|
||
rowsCtx, done = context.WithCancel(context.Background()) | ||
|
||
rows, err = cfdb.QueryContext(rowsCtx, "EXPERIMENTAL CHANGEFEED FOR foo WITH UPDATED, CURSOR=\""+latestCursor+"\"") | ||
require.NoError(t, err) | ||
|
||
for j := 0; j < 50; j++ { | ||
require.True(t, rows.Next()) | ||
|
||
var a, b, c []byte | ||
require.NoError(t, rows.Scan(&a, &b, &c)) | ||
|
||
gObj, err := gabs.ParseJSON(c) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, float64(j+100), gObj.S("after", "a").Data(), gObj.String()) | ||
} | ||
|
||
done() | ||
|
||
cfdb.Close() | ||
rows.Close() | ||
} |
Oops, something went wrong.