Skip to content

Commit 47b4b8b

Browse files
authored
Merge branch 'main' into dapr-state-store-clickhouse
2 parents 16af996 + 0f09d25 commit 47b4b8b

File tree

5 files changed

+150
-3
lines changed

5 files changed

+150
-3
lines changed

bindings/postgres/metadata.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ limitations under the License.
1414
package postgres
1515

1616
import (
17+
"errors"
1718
"time"
1819

1920
"github.com/dapr/components-contrib/common/authentication/aws"
@@ -53,5 +54,9 @@ func (m *psqlMetadata) InitWithMetadata(meta map[string]string) error {
5354
return err
5455
}
5556

57+
if m.Timeout < 1*time.Second {
58+
return errors.New("invalid value for 'timeout': must be greater than 1s")
59+
}
60+
5661
return nil
5762
}

bindings/postgres/metadata_test.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
Copyright 2023 The Dapr Authors
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package postgres
15+
16+
import (
17+
"testing"
18+
"time"
19+
20+
"github.com/stretchr/testify/assert"
21+
"github.com/stretchr/testify/require"
22+
)
23+
24+
func TestMetadata(t *testing.T) {
25+
t.Run("missing connection string", func(t *testing.T) {
26+
m := psqlMetadata{}
27+
props := map[string]string{}
28+
29+
err := m.InitWithMetadata(props)
30+
require.Error(t, err)
31+
require.ErrorContains(t, err, "connection string")
32+
})
33+
34+
t.Run("has connection string", func(t *testing.T) {
35+
m := psqlMetadata{}
36+
props := map[string]string{
37+
"connectionString": "foo",
38+
}
39+
40+
err := m.InitWithMetadata(props)
41+
require.NoError(t, err)
42+
})
43+
44+
t.Run("default timeout", func(t *testing.T) {
45+
m := psqlMetadata{}
46+
props := map[string]string{
47+
"connectionString": "foo",
48+
}
49+
50+
err := m.InitWithMetadata(props)
51+
require.NoError(t, err)
52+
assert.Equal(t, 20*time.Second, m.Timeout)
53+
})
54+
55+
t.Run("invalid timeout", func(t *testing.T) {
56+
m := psqlMetadata{}
57+
props := map[string]string{
58+
"connectionString": "foo",
59+
"timeout": "NaN",
60+
}
61+
62+
err := m.InitWithMetadata(props)
63+
require.Error(t, err)
64+
})
65+
66+
t.Run("positive timeout", func(t *testing.T) {
67+
m := psqlMetadata{}
68+
props := map[string]string{
69+
"connectionString": "foo",
70+
"timeout": "42",
71+
}
72+
73+
err := m.InitWithMetadata(props)
74+
require.NoError(t, err)
75+
assert.Equal(t, 42*time.Second, m.Timeout)
76+
})
77+
78+
t.Run("zero timeout", func(t *testing.T) {
79+
m := psqlMetadata{}
80+
props := map[string]string{
81+
"connectionString": "foo",
82+
"timeout": "0",
83+
}
84+
85+
err := m.InitWithMetadata(props)
86+
require.Error(t, err)
87+
})
88+
}

bindings/postgres/postgres.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,20 @@ func (p *Postgres) Init(ctx context.Context, meta bindings.Metadata) error {
7373

7474
// This context doesn't control the lifetime of the connection pool, and is
7575
// only scoped to postgres creating resources at init.
76-
p.db, err = pgxpool.NewWithConfig(ctx, poolConfig)
76+
connCtx, connCancel := context.WithTimeout(ctx, m.Timeout)
77+
defer connCancel()
78+
p.db, err = pgxpool.NewWithConfig(connCtx, poolConfig)
7779
if err != nil {
7880
return fmt.Errorf("unable to connect to the DB: %w", err)
7981
}
8082

83+
pingCtx, pingCancel := context.WithTimeout(ctx, m.Timeout)
84+
defer pingCancel()
85+
err = p.db.Ping(pingCtx)
86+
if err != nil {
87+
return fmt.Errorf("failed to ping the DB: %w", err)
88+
}
89+
8190
return nil
8291
}
8392

bindings/postgres/postgres_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ package postgres
1515

1616
import (
1717
"context"
18+
"errors"
1819
"fmt"
1920
"os"
2021
"testing"
@@ -62,6 +63,10 @@ func TestPostgresIntegration(t *testing.T) {
6263
t.SkipNow()
6364
}
6465

66+
t.Run("Test init configurations", func(t *testing.T) {
67+
testInitConfiguration(t, url)
68+
})
69+
6570
// live DB test
6671
b := NewPostgres(logger.NewLogger("test")).(*Postgres)
6772
m := bindings.Metadata{Base: metadata.Base{Properties: map[string]string{"connectionString": url}}}
@@ -131,6 +136,46 @@ func TestPostgresIntegration(t *testing.T) {
131136
})
132137
}
133138

139+
// testInitConfiguration tests valid and invalid config settings.
140+
func testInitConfiguration(t *testing.T, connectionString string) {
141+
logger := logger.NewLogger("test")
142+
tests := []struct {
143+
name string
144+
props map[string]string
145+
expectedErr error
146+
}{
147+
{
148+
name: "Empty",
149+
props: map[string]string{},
150+
expectedErr: errors.New("missing connection string"),
151+
},
152+
{
153+
name: "Valid connection string",
154+
props: map[string]string{"connectionString": connectionString},
155+
expectedErr: nil,
156+
},
157+
}
158+
159+
for _, tt := range tests {
160+
t.Run(tt.name, func(t *testing.T) {
161+
p := NewPostgres(logger).(*Postgres)
162+
defer p.Close()
163+
164+
metadata := bindings.Metadata{
165+
Base: metadata.Base{Properties: tt.props},
166+
}
167+
168+
err := p.Init(context.Background(), metadata)
169+
if tt.expectedErr == nil {
170+
require.NoError(t, err)
171+
} else {
172+
require.Error(t, err)
173+
assert.Equal(t, tt.expectedErr, err)
174+
}
175+
})
176+
}
177+
}
178+
134179
func assertResponse(t *testing.T, res *bindings.InvokeResponse, err error) {
135180
require.NoError(t, err)
136181
assert.NotNil(t, res)

state/postgresql/v2/postgresql.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,16 @@ func (p *PostgreSQL) Init(ctx context.Context, meta state.Metadata) (err error)
9999
}
100100

101101
connCtx, connCancel := context.WithTimeout(ctx, p.metadata.Timeout)
102+
defer connCancel()
102103
p.db, err = pgxpool.NewWithConfig(connCtx, config)
103-
connCancel()
104104
if err != nil {
105105
err = fmt.Errorf("failed to connect to the database: %w", err)
106106
return err
107107
}
108108

109109
pingCtx, pingCancel := context.WithTimeout(ctx, p.metadata.Timeout)
110+
defer pingCancel()
110111
err = p.db.Ping(pingCtx)
111-
pingCancel()
112112
if err != nil {
113113
err = fmt.Errorf("failed to ping the database: %w", err)
114114
return err

0 commit comments

Comments
 (0)