-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_cov6_test.go
More file actions
158 lines (123 loc) · 4.57 KB
/
lambda_cov6_test.go
File metadata and controls
158 lines (123 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package dynamorm
import (
"context"
"errors"
"sync"
"testing"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/stretchr/testify/require"
"github.com/pay-theory/dynamorm/pkg/session"
)
func TestBenchmarkColdStart_CoversSuccessAndErrorPaths_COV6(t *testing.T) {
origLoad := benchmarkLoadDefaultConfig
origNewClient := benchmarkNewDynamoDBClient
t.Cleanup(func() {
globalLambdaDB = nil
lambdaOnce = sync.Once{}
benchmarkLoadDefaultConfig = origLoad
benchmarkNewDynamoDBClient = origNewClient
})
resetGlobals := func() {
globalLambdaDB = nil
lambdaOnce = sync.Once{}
}
t.Run("config load error", func(t *testing.T) {
resetGlobals()
benchmarkLoadDefaultConfig = func(context.Context, ...func(*config.LoadOptions) error) (aws.Config, error) {
return aws.Config{}, errors.New("boom")
}
metrics := BenchmarkColdStart()
require.NotEmpty(t, metrics.Phases["aws_config_error"])
})
t.Run("dynamorm setup error", func(t *testing.T) {
resetGlobals()
benchmarkLoadDefaultConfig = func(context.Context, ...func(*config.LoadOptions) error) (aws.Config, error) {
return minimalAWSConfig(nil), nil
}
stubSessionConfigLoad(t, func(context.Context, ...func(*config.LoadOptions) error) (aws.Config, error) {
return aws.Config{}, errors.New("session boom")
})
metrics := BenchmarkColdStart()
require.NotEmpty(t, metrics.Phases["dynamorm_setup_error"])
})
t.Run("model registration error", func(t *testing.T) {
resetGlobals()
benchmarkLoadDefaultConfig = func(context.Context, ...func(*config.LoadOptions) error) (aws.Config, error) {
return minimalAWSConfig(nil), nil
}
stubSessionConfigLoad(t, func(context.Context, ...func(*config.LoadOptions) error) (aws.Config, error) {
return minimalAWSConfig(nil), nil
})
metrics := BenchmarkColdStart(123)
require.NotEmpty(t, metrics.Phases["model_registration_error"])
})
t.Run("success", func(t *testing.T) {
resetGlobals()
httpClient := newCapturingHTTPClient(map[string]string{
"DynamoDB_20120810.ListTables": `{"TableNames":[]}`,
})
benchmarkLoadDefaultConfig = func(context.Context, ...func(*config.LoadOptions) error) (aws.Config, error) {
return minimalAWSConfig(httpClient), nil
}
benchmarkNewDynamoDBClient = dynamodb.NewFromConfig
stubSessionConfigLoad(t, func(context.Context, ...func(*config.LoadOptions) error) (aws.Config, error) {
return minimalAWSConfig(httpClient), nil
})
metrics := BenchmarkColdStart(&cov4LambdaModel{})
require.NotEmpty(t, metrics.Phases["aws_config"])
require.NotEmpty(t, metrics.Phases["dynamodb_client"])
require.NotEmpty(t, metrics.Phases["dynamorm_setup"])
require.NotEmpty(t, metrics.Phases["model_registration"])
require.NotEmpty(t, metrics.Phases["first_connection"])
require.Greater(t, metrics.TotalDuration, time.Duration(0))
})
}
func TestNewLambdaOptimized_WarmStartReturnsGlobal_COV6(t *testing.T) {
t.Cleanup(func() {
globalLambdaDB = nil
lambdaOnce = sync.Once{}
})
globalLambdaDB = &LambdaDB{}
lambdaOnce = sync.Once{}
got, err := NewLambdaOptimized()
require.NoError(t, err)
require.Same(t, globalLambdaDB, got)
}
func TestLambdaDB_WithLambdaTimeout_NoDeadlineReturnsSame_COV6(t *testing.T) {
ldb := &LambdaDB{db: &DB{}}
require.Same(t, ldb, ldb.WithLambdaTimeout(context.Background()))
}
func TestLambdaDB_WithLambdaTimeout_SetsAdjustedDeadline_COV6(t *testing.T) {
httpClient := newCapturingHTTPClient(nil)
stubSessionConfigLoad(t, func(context.Context, ...func(*config.LoadOptions) error) (aws.Config, error) {
return minimalAWSConfig(httpClient), nil
})
dbAny, err := New(session.Config{Region: "us-east-1"})
require.NoError(t, err)
db := mustDB(t, dbAny)
ldb := &LambdaDB{
ExtendedDB: db,
db: db,
modelCache: &sync.Map{},
}
deadline := time.Now().Add(5 * time.Second)
ctx, cancel := context.WithDeadline(context.Background(), deadline)
t.Cleanup(cancel)
newDB := ldb.WithLambdaTimeout(ctx)
require.NotNil(t, newDB)
require.Same(t, ldb.modelCache, newDB.modelCache)
require.NotNil(t, newDB.db)
require.Equal(t, ctx, newDB.db.ctx)
require.WithinDuration(t, deadline.Add(-1*time.Second), newDB.db.lambdaDeadline, 25*time.Millisecond)
}
func TestGetLambdaMemoryMB_HandlesEmptyAndInvalidValues_COV6(t *testing.T) {
t.Setenv("AWS_LAMBDA_FUNCTION_MEMORY_SIZE", "")
require.Equal(t, 0, GetLambdaMemoryMB())
t.Setenv("AWS_LAMBDA_FUNCTION_MEMORY_SIZE", "not-a-number")
require.Equal(t, 0, GetLambdaMemoryMB())
t.Setenv("AWS_LAMBDA_FUNCTION_MEMORY_SIZE", "256")
require.Equal(t, 256, GetLambdaMemoryMB())
}