-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdml_test.go
203 lines (183 loc) · 6.26 KB
/
pdml_test.go
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package spanner
import (
"bytes"
"context"
"io/ioutil"
"log"
"os"
"testing"
"time"
sppb "cloud.google.com/go/spanner/apiv1/spannerpb"
. "github.com/storj/exp-spanner/internal/testutil"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/encoding/gzip"
"google.golang.org/grpc/status"
)
func TestMockPartitionedUpdate(t *testing.T) {
t.Parallel()
ctx := context.Background()
_, client, teardown := setupMockedTestServer(t)
defer teardown()
stmt := NewStatement(UpdateBarSetFoo)
rowCount, err := client.PartitionedUpdate(ctx, stmt)
if err != nil {
t.Fatal(err)
}
want := int64(UpdateBarSetFooRowCount)
if rowCount != want {
t.Errorf("got %d, want %d", rowCount, want)
}
}
func TestMockPartitionedUpdateWithQuery(t *testing.T) {
t.Parallel()
ctx := context.Background()
_, client, teardown := setupMockedTestServer(t)
defer teardown()
stmt := NewStatement(SelectFooFromBar)
_, err := client.PartitionedUpdate(ctx, stmt)
wantCode := codes.InvalidArgument
var serr *Error
if !errorAs(err, &serr) {
t.Errorf("got error %v, want spanner.Error", err)
}
if ErrCode(serr) != wantCode {
t.Errorf("got error %v, want code %s", serr, wantCode)
}
}
// PDML should be retried if the transaction is aborted.
func TestPartitionedUpdate_Aborted(t *testing.T) {
t.Parallel()
ctx := context.Background()
server, client, teardown := setupMockedTestServer(t)
defer teardown()
server.TestSpanner.PutExecutionTime(MethodExecuteSql,
SimulatedExecutionTime{
Errors: []error{
status.Error(codes.Aborted, "Transaction aborted"),
status.Error(codes.Internal, "Received unexpected EOS on DATA frame from server"),
},
})
stmt := NewStatement(UpdateBarSetFoo)
rowCount, err := client.PartitionedUpdate(ctx, stmt)
if err != nil {
t.Fatal(err)
}
want := int64(UpdateBarSetFooRowCount)
if rowCount != want {
t.Errorf("Row count mismatch\ngot: %d\nwant: %d", rowCount, want)
}
gotReqs, err := shouldHaveReceived(server.TestSpanner, []interface{}{
&sppb.BatchCreateSessionsRequest{},
&sppb.BeginTransactionRequest{},
&sppb.ExecuteSqlRequest{},
&sppb.BeginTransactionRequest{},
&sppb.ExecuteSqlRequest{},
&sppb.BeginTransactionRequest{},
&sppb.ExecuteSqlRequest{},
})
if err != nil {
t.Fatal(err)
}
id1 := gotReqs[2].(*sppb.ExecuteSqlRequest).Transaction.GetId()
id2 := gotReqs[4].(*sppb.ExecuteSqlRequest).Transaction.GetId()
if bytes.Equal(id1, id2) {
t.Errorf("same transaction used twice, expected two different transactions\ngot tx1: %q\ngot tx2: %q", id1, id2)
}
}
// Test that a deadline is respected by PDML, and that the session that was
// created is also deleted, even though the update timed out.
func TestPartitionedUpdate_WithDeadline(t *testing.T) {
t.Parallel()
logger := log.New(os.Stderr, "", log.LstdFlags)
server, client, teardown := setupMockedTestServerWithConfig(t, ClientConfig{
SessionPoolConfig: DefaultSessionPoolConfig,
Logger: logger,
})
defer teardown()
ctx := context.Background()
ctx, cancel := context.WithDeadline(ctx, time.Now().Add(50*time.Millisecond))
defer cancel()
server.TestSpanner.PutExecutionTime(MethodExecuteSql,
SimulatedExecutionTime{
MinimumExecutionTime: 100 * time.Millisecond,
})
stmt := NewStatement(UpdateBarSetFoo)
// The following update will cause a 'Failed to delete session' warning to
// be logged. This is expected. To avoid spamming the log, we temporarily
// set the output to be discarded.
logger.SetOutput(ioutil.Discard)
_, err := client.PartitionedUpdate(ctx, stmt)
logger.SetOutput(os.Stderr)
if err == nil {
t.Fatalf("missing expected error")
}
wantCode := codes.DeadlineExceeded
if status.Code(err) != wantCode {
t.Fatalf("got error %v, want code %s", err, wantCode)
}
}
func TestPartitionedUpdate_QueryOptions(t *testing.T) {
for _, tt := range queryOptionsTestCases() {
t.Run(tt.name, func(t *testing.T) {
if tt.env.Options != nil {
unset := setQueryOptionsEnvVars(tt.env.Options)
defer unset()
}
ctx := context.Background()
server, client, teardown := setupMockedTestServerWithConfig(t, ClientConfig{QueryOptions: tt.client, Compression: gzip.Name})
defer teardown()
var err error
if tt.query.Options == nil {
_, err = client.PartitionedUpdate(ctx, NewStatement(UpdateBarSetFoo))
} else {
_, err = client.PartitionedUpdateWithOptions(ctx, NewStatement(UpdateBarSetFoo), tt.query)
}
if err != nil {
t.Fatalf("expect no errors, but got %v", err)
}
checkReqsForQueryOptions(t, server.TestSpanner, tt.want)
})
}
}
func TestPartitionedUpdate_Tagging(t *testing.T) {
ctx := context.Background()
server, client, teardown := setupMockedTestServer(t)
defer teardown()
_, err := client.PartitionedUpdateWithOptions(ctx, NewStatement(UpdateBarSetFoo), QueryOptions{RequestTag: "pdml-tag"})
if err != nil {
t.Fatalf("expect no errors, but got %v", err)
}
checkRequestsForExpectedRequestOptions(t, server.TestSpanner, 1, sppb.RequestOptions{RequestTag: "pdml-tag"})
}
func TestPartitionedUpdate_ExcludeTxnFromChangeStreams(t *testing.T) {
ctx := context.Background()
server, client, teardown := setupMockedTestServer(t)
defer teardown()
_, err := client.PartitionedUpdateWithOptions(ctx, NewStatement(UpdateBarSetFoo), QueryOptions{ExcludeTxnFromChangeStreams: true})
if err != nil {
t.Fatalf("expect no errors, but got %v", err)
}
requests := drainRequestsFromServer(server.TestSpanner)
if err := compareRequests([]interface{}{
&sppb.BatchCreateSessionsRequest{},
&sppb.BeginTransactionRequest{},
&sppb.ExecuteSqlRequest{}}, requests); err != nil {
t.Fatal(err)
}
if !requests[1].(*sppb.BeginTransactionRequest).GetOptions().GetExcludeTxnFromChangeStreams() {
t.Fatal("Transaction is not set to be excluded from change streams")
}
}