-
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.
Use proper zstd decoder pool for binlog event compression handling (#…
…17042) Signed-off-by: Matt Lord <[email protected]>
- Loading branch information
1 parent
736e54d
commit e957e2f
Showing
2 changed files
with
121 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
Copyright 2024 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 mysql | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"testing" | ||
|
||
"github.com/klauspost/compress/zstd" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestDecoderPool(t *testing.T) { | ||
validateDecoder := func(t *testing.T, err error, decoder *zstd.Decoder) { | ||
require.NoError(t, err) | ||
require.NotNil(t, decoder) | ||
require.IsType(t, &zstd.Decoder{}, decoder) | ||
} | ||
tests := []struct { | ||
name string | ||
reader io.Reader | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "happy path", | ||
reader: bytes.NewReader([]byte{0x68, 0x61, 0x70, 0x70, 0x79}), | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
// It's not guaranteed that we get the same decoder back from the pool | ||
// that we just put in, so we use a loop and ensure that it worked at | ||
// least one of the times. Without doing this the test would be flaky. | ||
poolingUsed := false | ||
|
||
for i := 0; i < 20; i++ { | ||
decoder, err := statefulDecoderPool.Get(tt.reader) | ||
validateDecoder(t, err, decoder) | ||
statefulDecoderPool.Put(decoder) | ||
|
||
decoder2, err := statefulDecoderPool.Get(tt.reader) | ||
validateDecoder(t, err, decoder2) | ||
if decoder2 == decoder { | ||
poolingUsed = true | ||
} | ||
statefulDecoderPool.Put(decoder2) | ||
|
||
decoder3, err := statefulDecoderPool.Get(tt.reader) | ||
validateDecoder(t, err, decoder3) | ||
if decoder3 == decoder || decoder3 == decoder2 { | ||
poolingUsed = true | ||
} | ||
statefulDecoderPool.Put(decoder3) | ||
} | ||
|
||
require.True(t, poolingUsed) | ||
}) | ||
} | ||
} |