From 04620836a904bd119117d98d556a6f30e833f5d1 Mon Sep 17 00:00:00 2001 From: Steve Garrity Date: Tue, 4 Mar 2025 20:32:38 -0800 Subject: [PATCH 1/5] update server tests to use constructor convention from d02e79a --- server/resp_test.go | 7 +++---- server/server_test.go | 28 +++++++--------------------- 2 files changed, 10 insertions(+), 25 deletions(-) diff --git a/server/resp_test.go b/server/resp_test.go index 3608c60c3..ceee82704 100644 --- a/server/resp_test.go +++ b/server/resp_test.go @@ -14,10 +14,9 @@ func TestConnWriteOK(t *testing.T) { clientConn := &mockconn.MockConn{} conn := &Conn{Conn: packet.NewConn(clientConn)} - result := &mysql.Result{ - AffectedRows: 1, - InsertId: 2, - } + result := mysql.NewResultReserveResultset(0) + result.AffectedRows = 1 + result.InsertId = 2 // write ok with insertid and affectedrows set err := conn.writeOK(result) diff --git a/server/server_test.go b/server/server_test.go index 0311448f9..037ec3047 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -244,30 +244,16 @@ func (h *testHandler) handleQuery(query string, binary bool) (*mysql.Result, err if err != nil { return nil, errors.Trace(err) } else { - return &mysql.Result{ - Status: 0, - Warnings: 0, - InsertId: 0, - AffectedRows: 0, - Resultset: r, - }, nil + return mysql.NewResult(r), nil } case "insert": - return &mysql.Result{ - Status: 0, - Warnings: 0, - InsertId: 1, - AffectedRows: 0, - Resultset: nil, - }, nil + res := mysql.NewResultReserveResultset(0) + res.InsertId = 1 + return res, nil case "delete", "update", "replace": - return &mysql.Result{ - Status: 0, - Warnings: 0, - InsertId: 0, - AffectedRows: 1, - Resultset: nil, - }, nil + res := mysql.NewResultReserveResultset(0) + res.AffectedRows = 1 + return res, nil default: return nil, fmt.Errorf("invalid query %s", query) } From ef77e5a41dd06d22c6a0c26ae3ff6d40996ea850 Mon Sep 17 00:00:00 2001 From: Steve Garrity Date: Tue, 4 Mar 2025 20:55:12 -0800 Subject: [PATCH 2/5] unit test the nil case of Hasresultset --- mysql/result_test.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/mysql/result_test.go b/mysql/result_test.go index 0c9344f03..f0cd87a92 100644 --- a/mysql/result_test.go +++ b/mysql/result_test.go @@ -17,3 +17,10 @@ func TestHasResultset_true(t *testing.T) { b := r.HasResultset() require.Equal(t, true, b) } + +// this shouldn't happen after d02e79a, but test just in case +func TestHasResultset_nil(t *testing.T) { + var r *Result + b := r.HasResultset() + require.Equal(t, false, b) +} From 21883cd138a974b07c46526554dd9d8c0e8d1151 Mon Sep 17 00:00:00 2001 From: Steve Garrity Date: Tue, 4 Mar 2025 20:55:28 -0800 Subject: [PATCH 3/5] unit test WriteValue for OK vs Resultset --- server/resp_test.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/server/resp_test.go b/server/resp_test.go index ceee82704..babb8b6ea 100644 --- a/server/resp_test.go +++ b/server/resp_test.go @@ -228,3 +228,36 @@ func TestConnWriteFieldValues(t *testing.T) { // EOF require.Equal(t, []byte{1, 0, 0, 4, mysql.EOF_HEADER}, clientConn.WriteBuffered[43:]) } + +func TestWriteValue(t *testing.T) { + clientConn := &mockconn.MockConn{MultiWrite: true} + conn := &Conn{Conn: packet.NewConn(clientConn)} + + // simple OK + err := conn.WriteValue(mysql.NewResultReserveResultset(0)) + require.NoError(t, err) + expected := []byte{3, 0, 0, 0, mysql.OK_HEADER, 0, 0} + require.Equal(t, expected, clientConn.WriteBuffered) + + // reset write buffer + clientConn.WriteBuffered = []byte{} + + // resultset with no rows + rs := mysql.NewResultReserveResultset(1) + rs.Fields = []*mysql.Field{{Name: []byte("a")}} + err = conn.WriteValue(rs) + require.NoError(t, err) + expected = []byte{1, 0, 0, 1, mysql.MORE_DATE_HEADER} + require.Equal(t, expected, clientConn.WriteBuffered[:5]) + + // reset write buffer + clientConn.WriteBuffered = []byte{} + + // resultset with rows + rs.Fields = []*mysql.Field{{Name: []byte("a")}} + rs.RowDatas = []mysql.RowData{[]byte{1, 2, 3}} + err = conn.WriteValue(rs) + require.NoError(t, err) + expected = []byte{1, 0, 0, 5, mysql.MORE_DATE_HEADER} + require.Equal(t, expected, clientConn.WriteBuffered[:5]) +} From f98087c52a568b9ee798caa832dfe111ec158e41 Mon Sep 17 00:00:00 2001 From: Steve Garrity Date: Tue, 4 Mar 2025 21:27:26 -0800 Subject: [PATCH 4/5] handle nil result correctly, and test nil resultset correctly --- mysql/result.go | 3 +++ mysql/result_test.go | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/mysql/result.go b/mysql/result.go index d55cb0e27..f103194b3 100644 --- a/mysql/result.go +++ b/mysql/result.go @@ -36,6 +36,9 @@ func (r *Result) Close() { } func (r *Result) HasResultset() bool { + if r == nil { + return false + } if r.Resultset != nil && len(r.Resultset.Fields) > 0 { return true } diff --git a/mysql/result_test.go b/mysql/result_test.go index f0cd87a92..6e3b945c0 100644 --- a/mysql/result_test.go +++ b/mysql/result_test.go @@ -19,6 +19,13 @@ func TestHasResultset_true(t *testing.T) { } // this shouldn't happen after d02e79a, but test just in case +func TestHasResultset_nilset(t *testing.T) { + r := NewResultReserveResultset(0) + r.Resultset = nil + b := r.HasResultset() + require.Equal(t, false, b) +} + func TestHasResultset_nil(t *testing.T) { var r *Result b := r.HasResultset() From bc26bedf448230c1b0d0ad48d46340dd66288a0f Mon Sep 17 00:00:00 2001 From: Steve Garrity Date: Sat, 8 Mar 2025 19:36:39 -0800 Subject: [PATCH 5/5] test brevity --- mysql/result_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/mysql/result_test.go b/mysql/result_test.go index 6e3b945c0..113ffbcf3 100644 --- a/mysql/result_test.go +++ b/mysql/result_test.go @@ -9,13 +9,13 @@ import ( func TestHasResultset_false(t *testing.T) { r := NewResultReserveResultset(0) b := r.HasResultset() - require.Equal(t, false, b) + require.False(t, b) } func TestHasResultset_true(t *testing.T) { r := NewResultReserveResultset(1) b := r.HasResultset() - require.Equal(t, true, b) + require.True(t, b) } // this shouldn't happen after d02e79a, but test just in case @@ -23,11 +23,11 @@ func TestHasResultset_nilset(t *testing.T) { r := NewResultReserveResultset(0) r.Resultset = nil b := r.HasResultset() - require.Equal(t, false, b) + require.False(t, b) } func TestHasResultset_nil(t *testing.T) { var r *Result b := r.HasResultset() - require.Equal(t, false, b) + require.False(t, b) }