-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy patherr_count_test.go
150 lines (121 loc) · 4 KB
/
err_count_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
// Copyright 2016 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package sql_test
import (
"context"
gosql "database/sql"
"testing"
"github.com/cockroachdb/cockroach/pkg/server/telemetry"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
"github.com/cockroachdb/cockroach/pkg/sql/tests"
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/errors"
"github.com/lib/pq"
)
func TestErrorCounts(t *testing.T) {
defer leaktest.AfterTest(t)()
telemetry.GetFeatureCounts(telemetry.Raw, telemetry.ResetCounts)
params, _ := tests.CreateTestServerParams()
s, db, _ := serverutils.StartServer(t, params)
defer s.Stopper().Stop(context.TODO())
count1 := telemetry.GetRawFeatureCounts()["errorcodes."+pgcode.Syntax]
_, err := db.Query("SELECT 1+")
if err == nil {
t.Fatal("expected error, got no error")
}
count2 := telemetry.GetRawFeatureCounts()["errorcodes."+pgcode.Syntax]
if count2-count1 != 1 {
t.Fatalf("expected 1 syntax error, got %d", count2-count1)
}
rows, err := db.Query(`SHOW SYNTAX 'SELECT 1+'`)
if err != nil {
t.Fatalf("%+v", err)
}
for rows.Next() {
// Do nothing. SHOW SYNTAX itself is tested elsewhere.
// We just check the counts below.
}
rows.Close()
count3 := telemetry.GetRawFeatureCounts()["errorcodes."+pgcode.Syntax]
if count3-count2 != 1 {
t.Fatalf("expected 1 syntax error, got %d", count3-count2)
}
}
func TestUnimplementedCounts(t *testing.T) {
defer leaktest.AfterTest(t)()
telemetry.GetFeatureCounts(telemetry.Raw, telemetry.ResetCounts)
params, _ := tests.CreateTestServerParams()
s, db, _ := serverutils.StartServer(t, params)
defer s.Stopper().Stop(context.TODO())
if _, err := db.Exec("CREATE TABLE t(x INT8)"); err != nil {
t.Fatal(err)
}
if _, err := db.Exec("ALTER TABLE t ALTER COLUMN x SET DATA TYPE STRING USING x::STRING"); err == nil {
t.Fatal("expected error, got no error")
}
if telemetry.GetRawFeatureCounts()["unimplemented.#9851.INT8->STRING"] == 0 {
t.Fatal("expected unimplemented telemetry, got nothing")
}
}
func TestTransactionRetryErrorCounts(t *testing.T) {
defer leaktest.AfterTest(t)()
telemetry.GetFeatureCounts(telemetry.Raw, telemetry.ResetCounts)
// Transaction retry errors aren't given a pg error code until deep
// in pgwire (pgwire.convertToErrWithPGCode). Make sure we're
// reporting errors at a level that allows this code to be recorded.
params, _ := tests.CreateTestServerParams()
s, db, _ := serverutils.StartServer(t, params)
defer s.Stopper().Stop(context.TODO())
if _, err := db.Exec("CREATE TABLE accounts (id INT8 PRIMARY KEY, balance INT8)"); err != nil {
t.Fatal(err)
}
if _, err := db.Exec("INSERT INTO accounts VALUES (1, 100)"); err != nil {
t.Fatal(err)
}
txn1, err := db.Begin()
if err != nil {
t.Fatal(err)
}
txn2, err := db.Begin()
if err != nil {
t.Fatal(err)
}
for _, txn := range []*gosql.Tx{txn1, txn2} {
rows, err := txn.Query("SELECT * FROM accounts WHERE id = 1")
if err != nil {
t.Fatal(err)
}
for rows.Next() {
}
rows.Close()
}
for _, txn := range []*gosql.Tx{txn1, txn2} {
if _, err := txn.Exec("UPDATE accounts SET balance = balance - 100 WHERE id = 1"); err != nil {
if pqErr := (*pq.Error)(nil); errors.As(err, &pqErr) &&
pqErr.Code == pgcode.SerializationFailure {
if err := txn.Rollback(); err != nil {
t.Fatal(err)
}
return
}
t.Fatal(err)
}
if err := txn.Commit(); err != nil {
if pqErr := (*pq.Error)(nil); !errors.As(err, &pqErr) ||
pqErr.Code != pgcode.SerializationFailure {
t.Fatal(err)
}
}
}
if telemetry.GetRawFeatureCounts()["errorcodes.40001"] == 0 {
t.Fatal("expected error code telemetry, got nothing")
}
}