Skip to content

Commit 413ede9

Browse files
committed
Add Azure config methods and tests
1 parent d595884 commit 413ede9

File tree

3 files changed

+103
-27
lines changed

3 files changed

+103
-27
lines changed

pbm/storage/azure/azure.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const (
2525
defaultUploadBuff = 10 << 20 // 10Mb
2626

2727
defaultMaxRetries = 3
28+
defaultMinRetryDelay = 800 * time.Millisecond
2829
defaultMaxRetryDelay = 60 * time.Second
2930

3031
maxBlocks = 50_000

pbm/storage/azure/config.go

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ type Credentials struct {
2525
Key string `bson:"key" json:"key,omitempty" yaml:"key,omitempty"`
2626
}
2727

28+
// Retryer is configuration for retry behavior described:
29+
// https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azcore@v1.19.1/policy#RetryOptions
2830
type Retryer struct {
2931
NumMaxRetries int32 `bson:"numMaxRetries" json:"numMaxRetries" yaml:"numMaxRetries"`
3032
MinRetryDelay time.Duration `bson:"minRetryDelay" json:"minRetryDelay" yaml:"minRetryDelay"`
@@ -38,6 +40,10 @@ func (cfg *Config) Clone() *Config {
3840

3941
rv := *cfg
4042
rv.EndpointURLMap = maps.Clone(cfg.EndpointURLMap)
43+
if cfg.Retryer != nil {
44+
v := *cfg.Retryer
45+
rv.Retryer = &v
46+
}
4147
if cfg.MaxObjSizeGB != nil {
4248
v := *cfg.MaxObjSizeGB
4349
rv.MaxObjSizeGB = &v
@@ -46,33 +52,7 @@ func (cfg *Config) Clone() *Config {
4652
}
4753

4854
func (cfg *Config) Equal(other *Config) bool {
49-
if cfg == nil || other == nil {
50-
return cfg == other
51-
}
52-
53-
if cfg.Account != other.Account {
54-
return false
55-
}
56-
if cfg.Container != other.Container {
57-
return false
58-
}
59-
if cfg.EndpointURL != other.EndpointURL {
60-
return false
61-
}
62-
if !maps.Equal(cfg.EndpointURLMap, other.EndpointURLMap) {
63-
return false
64-
}
65-
if cfg.Prefix != other.Prefix {
66-
return false
67-
}
68-
if cfg.Credentials.Key != other.Credentials.Key {
69-
return false
70-
}
71-
if !reflect.DeepEqual(cfg.MaxObjSizeGB, other.MaxObjSizeGB) {
72-
return false
73-
}
74-
75-
return true
55+
return reflect.DeepEqual(cfg, other)
7656
}
7757

7858
// IsSameStorage identifies the same instance of the Azure storage.
@@ -101,12 +81,16 @@ func (cfg *Config) Cast() error {
10181
if cfg.Retryer == nil {
10282
cfg.Retryer = &Retryer{
10383
NumMaxRetries: defaultMaxRetries,
84+
MinRetryDelay: defaultMinRetryDelay,
10485
MaxRetryDelay: defaultMaxRetryDelay,
10586
}
10687
} else {
10788
if cfg.Retryer.NumMaxRetries == 0 {
10889
cfg.Retryer.NumMaxRetries = defaultMaxRetries
10990
}
91+
if cfg.Retryer.MinRetryDelay == 0 {
92+
cfg.Retryer.MinRetryDelay = defaultMinRetryDelay
93+
}
11094
if cfg.Retryer.MaxRetryDelay == 0 {
11195
cfg.Retryer.MaxRetryDelay = defaultMaxRetryDelay
11296
}

pbm/storage/azure/config_test.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package azure
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
"time"
7+
8+
"github.com/google/go-cmp/cmp"
9+
)
10+
11+
func TestClone(t *testing.T) {
12+
f := 1.1
13+
c1 := &Config{
14+
Account: "acc",
15+
Container: "cnt",
16+
EndpointURL: "ep.com",
17+
EndpointURLMap: map[string]string{"n1": "ep1", "n2": "ep2"},
18+
Prefix: "p1",
19+
Credentials: Credentials{
20+
Key: "k1",
21+
},
22+
MaxObjSizeGB: &f,
23+
Retryer: &Retryer{
24+
NumMaxRetries: 5,
25+
MinRetryDelay: 10 * time.Second,
26+
MaxRetryDelay: 20 * time.Second,
27+
},
28+
}
29+
30+
c2 := c1.Clone()
31+
32+
if &c1.EndpointURLMap == &c2.EndpointURLMap ||
33+
c1.MaxObjSizeGB == c2.MaxObjSizeGB ||
34+
c1.Retryer == c2.Retryer {
35+
t.Fatal("Deep copy of pointer fields is missing")
36+
}
37+
if !reflect.DeepEqual(c1, c2) {
38+
t.Fatalf("Clone is not performed, diff=%s", cmp.Diff(*c1, *c2))
39+
}
40+
}
41+
42+
func TestEqual(t *testing.T) {
43+
f := 1.1
44+
c1 := &Config{
45+
Account: "acc",
46+
Container: "cnt",
47+
EndpointURL: "ep.com",
48+
EndpointURLMap: map[string]string{"n1": "ep1", "n2": "ep2"},
49+
Prefix: "p1",
50+
Credentials: Credentials{
51+
Key: "k1",
52+
},
53+
MaxObjSizeGB: &f,
54+
Retryer: &Retryer{
55+
NumMaxRetries: 5,
56+
MinRetryDelay: 10 * time.Second,
57+
MaxRetryDelay: 20 * time.Second,
58+
},
59+
}
60+
61+
c2 := c1.Clone()
62+
63+
if !c1.Equal(c2) {
64+
t.Fatalf("cfg should be equal, diff=%s", cmp.Diff(*c1, *c2))
65+
}
66+
}
67+
68+
func TestCast(t *testing.T) {
69+
var c *Config
70+
err := c.Cast()
71+
if err == nil {
72+
t.Fatal("sigsegv should have happened instead")
73+
}
74+
75+
c = &Config{}
76+
err = c.Cast()
77+
if err != nil {
78+
t.Fatalf("got error during Cast: %v", err)
79+
}
80+
want := &Config{
81+
Retryer: &Retryer{
82+
NumMaxRetries: defaultMaxRetries,
83+
MinRetryDelay: defaultMinRetryDelay,
84+
MaxRetryDelay: defaultMaxRetryDelay,
85+
},
86+
}
87+
88+
if !c.Equal(want) {
89+
t.Fatalf("wrong config after Cast, diff=%s", cmp.Diff(*c, *want))
90+
}
91+
}

0 commit comments

Comments
 (0)