Repository navigation
Add comparable errors for target_session_attrs. #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,8 +3,11 @@ package fbconn | |
| import ( | ||
| "context" | ||
| "errors" | ||
| "os" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
|
|
@@ -21,3 +24,47 @@ func TestSyncConnClosedConnErrorIsErrConnClosed(t *testing.T) { | |
| require.Error(t, err) | ||
| require.ErrorIs(t, err, ErrConnClosed) | ||
| } | ||
|
|
||
| func TestConnectWithValidateConnectSessionAttributeErrors(t *testing.T) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure what this test is intended to verify. It opens a real connection, but the callback returns each sentinel directly, so none of the actual target_session_attrs validators are exercised. The read-only/read-write paths are already covered in fbconn_test.go, while the message and NotPreferredError checks can be unit tests without a database. |
||
| tests := []struct { | ||
| name string | ||
| err error | ||
| msg string | ||
| }{ | ||
| {"read-only connection", ErrReadOnlyConnection, "read only connection"}, | ||
| {"read-write connection", ErrReadWriteConnection, "connection is not read only"}, | ||
| {"primary", ErrPrimary, "not replica"}, | ||
| {"replica", ErrReplica, "not primary"}, | ||
| {"replication not configured", ErrReplicationNotConfigured, "replication is not configured"}, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second) | ||
| defer cancel() | ||
|
|
||
| config, err := ParseConfig(os.Getenv("FBX_TEST_DATABASE")) | ||
| require.NoError(t, err) | ||
| config.ValidateConnect = func(context.Context, *FbConn) error { | ||
| return tt.err | ||
| } | ||
|
|
||
| conn, err := ConnectConfig(ctx, config) | ||
| if !assert.NotNil(t, err) { | ||
| _ = conn.Close(ctx) | ||
| } | ||
| require.ErrorIs(t, err, tt.err) | ||
| var connectErr *ConnectError | ||
| require.ErrorAs(t, err, &connectErr) | ||
| require.ErrorContains(t, err, "ValidateConnect failed: "+tt.msg) | ||
| require.EqualError(t, tt.err, tt.msg) | ||
| }) | ||
| } | ||
|
|
||
| t.Run("prefer-replica", func(t *testing.T) { | ||
| err := &NotPreferredError{err: ErrPrimary} | ||
| require.ErrorIs(t, err, ErrPrimary) | ||
| require.NotErrorIs(t, err, ErrReplica) | ||
| require.EqualError(t, err, "standby server not found: not replica") | ||
| }) | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems more consistent to use ErrPrimaryConnection and ErrReplicaConnection here. This follows public naming pattern and matches ErrReadOnlyConnection and ErrReadWriteConnection above.