-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Matt Lord <[email protected]>
- Loading branch information
Showing
3 changed files
with
131 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
go/vt/vttablet/tabletmanager/vreplication/utils_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
Copyright 2021 The Vitess Authors. | ||
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 vreplication | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"vitess.io/vitess/go/sqltypes" | ||
"vitess.io/vitess/go/vt/binlog/binlogplayer" | ||
"vitess.io/vitess/go/vt/log" | ||
"vitess.io/vitess/go/vt/sqlparser" | ||
|
||
binlogdatapb "vitess.io/vitess/go/vt/proto/binlogdata" | ||
) | ||
|
||
func TestInsertLogTruncation(t *testing.T) { | ||
dbClient := binlogplayer.NewMockDBClient(t) | ||
defer dbClient.Close() | ||
dbClient.RemoveInvariant("insert into _vt.vreplication_log") | ||
stats := binlogplayer.NewStats() | ||
defer stats.Stop() | ||
vdbClient := newVDBClient(dbClient, stats) | ||
defer vdbClient.Close() | ||
vrID := int32(1) | ||
typ := "Testing" | ||
state := binlogdatapb.VReplicationWorkflowState_Error.String() | ||
maxMessageLen := 65535 | ||
truncationStr := fmt.Sprintf(" ... %s ... ", sqlparser.TruncationText) | ||
|
||
insertStmtf := "insert into _vt.vreplication_log(vrepl_id, type, state, message) values(%d, '%s', '%s', %s)" | ||
|
||
tests := []struct { | ||
message string | ||
expectTruncation bool | ||
}{ | ||
{ | ||
message: "Simple message that's not truncated", | ||
}, | ||
{ | ||
message: "Simple message that needs to be truncated " + strings.Repeat("a", 80000) + " cuz it's long", | ||
expectTruncation: true, | ||
}, | ||
{ | ||
message: "Simple message that doesn't need to be truncated " + strings.Repeat("b", 64000) + " cuz it's not quite too long", | ||
}, | ||
{ | ||
message: "Message that is just barely short enough " + strings.Repeat("c", maxMessageLen-(len("Message that is just barely short enough ")+len(" so it doesn't get truncated"))) + " so it doesn't get truncated", | ||
}, | ||
{ | ||
message: "Message that is just barely too long " + strings.Repeat("d", maxMessageLen-(len("Message that is just barely too long ")+len(" so it gets truncated"))+1) + " so it gets truncated", | ||
expectTruncation: true, | ||
}, | ||
{ | ||
message: "Super long message brosef wut r ya doin " + strings.Repeat("e", 60000) + strings.Repeat("f", 60000) + " so maybe don't do that to yourself and your friends", | ||
expectTruncation: true, | ||
}, | ||
{ | ||
message: "Super duper long message brosef wut r ya doin " + strings.Repeat("g", 120602) + strings.Repeat("h", 120001) + " so maybe really don't do that to yourself and your friends", | ||
expectTruncation: true, | ||
}, | ||
} | ||
for _, tc := range tests { | ||
t.Run("insertLog", func(t *testing.T) { | ||
var messageOut string | ||
if tc.expectTruncation { | ||
log.Errorf("BEFORE:: Message length: %d", len(tc.message)) | ||
mid := (len(tc.message) / 2) - len(truncationStr) | ||
for mid > (maxMessageLen / 2) { | ||
mid = mid / 2 | ||
} | ||
tail := (len(tc.message) - (mid + len(truncationStr))) + 1 | ||
messageOut = fmt.Sprintf("%s%s%s", tc.message[:mid], truncationStr, tc.message[tail:]) | ||
require.True(t, strings.HasPrefix(messageOut, tc.message[:10])) // Confirm we still have the same beginning | ||
require.True(t, strings.HasSuffix(messageOut, tc.message[len(tc.message)-10:])) // Confirm we still have the same end | ||
require.True(t, strings.Contains(messageOut, truncationStr)) // Confirm we have the truncation text | ||
} else { | ||
messageOut = tc.message | ||
} | ||
require.LessOrEqual(t, len(messageOut), 65535) | ||
dbClient.ExpectRequest(fmt.Sprintf(insertStmtf, vrID, typ, state, encodeString(messageOut)), &sqltypes.Result{}, nil) | ||
err := insertLog(vdbClient, typ, vrID, state, tc.message) | ||
require.NoError(t, err) | ||
dbClient.Wait() | ||
}) | ||
} | ||
} |