Skip to content

Commit 85dff71

Browse files
committed
Test utils.IsDeadlock()
1 parent 74ebc80 commit 85dff71

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

utils/utils_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"context"
55
"encoding/hex"
66
"fmt"
7+
"github.com/go-sql-driver/mysql"
8+
"github.com/lib/pq"
79
"github.com/stretchr/testify/assert"
810
"github.com/stretchr/testify/require"
911
"testing"
@@ -99,6 +101,40 @@ func TestChecksum(t *testing.T) {
99101
}
100102
}
101103

104+
func TestIsDeadlock(t *testing.T) {
105+
msg := "Unsuccessful attempt of confusing the tested code."
106+
code := [5]byte{0, 23, 42, 77, 255}
107+
108+
subtests := []struct {
109+
name string
110+
input error
111+
output bool
112+
}{
113+
{"nil", nil, false},
114+
{"deadline", context.DeadlineExceeded, false},
115+
{"mysql1204", &mysql.MySQLError{Number: 1204}, false},
116+
{"mysql1205", &mysql.MySQLError{Number: 1205}, true},
117+
{"mysql1205_with_crap", &mysql.MySQLError{Number: 1205, SQLState: code, Message: msg}, true},
118+
{"mysql1206", &mysql.MySQLError{Number: 1206}, false},
119+
{"mysql1212", &mysql.MySQLError{Number: 1212}, false},
120+
{"mysql1213", &mysql.MySQLError{Number: 1213}, true},
121+
{"mysql1213_with_crap", &mysql.MySQLError{Number: 1213, SQLState: code, Message: msg}, true},
122+
{"mysql1214", &mysql.MySQLError{Number: 1214}, false},
123+
{"postgres40000", &pq.Error{Code: "40000"}, false},
124+
{"postgres40001", &pq.Error{Code: "40001"}, true},
125+
{"postgres40001_with_crap", &pq.Error{Code: "40001", Message: msg}, true},
126+
{"postgres40002", &pq.Error{Code: "40002"}, false},
127+
{"postgres40P01", &pq.Error{Code: "40P01"}, true},
128+
{"postgres40P01_with_crap", &pq.Error{Code: "40P01", Message: msg}, true},
129+
}
130+
131+
for _, st := range subtests {
132+
t.Run(st.name, func(t *testing.T) {
133+
require.Equal(t, st.output, IsDeadlock(st.input))
134+
})
135+
}
136+
}
137+
102138
func TestChanFromSlice(t *testing.T) {
103139
t.Run("Nil", func(t *testing.T) {
104140
ch := ChanFromSlice[int](nil)

0 commit comments

Comments
 (0)