|
4 | 4 | "context" |
5 | 5 | "encoding/hex" |
6 | 6 | "fmt" |
| 7 | + "github.com/go-sql-driver/mysql" |
| 8 | + "github.com/lib/pq" |
7 | 9 | "github.com/stretchr/testify/assert" |
8 | 10 | "github.com/stretchr/testify/require" |
9 | 11 | "testing" |
@@ -99,6 +101,40 @@ func TestChecksum(t *testing.T) { |
99 | 101 | } |
100 | 102 | } |
101 | 103 |
|
| 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 | + |
102 | 138 | func TestChanFromSlice(t *testing.T) { |
103 | 139 | t.Run("Nil", func(t *testing.T) { |
104 | 140 | ch := ChanFromSlice[int](nil) |
|
0 commit comments