-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for non-clobbering codec registration
This should fail, until the following commit which switches the test to use the "nonclobbering" versions of these codecs.
- Loading branch information
Showing
2 changed files
with
95 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright 2020 Mostyn Bramley-Moore. | ||
// | ||
// 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 deptest ensures that the go-grpc-compression sub-packages will | ||
// not clobber an existing registration. | ||
package deptest | ||
|
||
import ( | ||
"testing" | ||
|
||
// This import happens first for the test, keep it ahead of | ||
// the three unnamed imports below. | ||
"github.com/mostynb/go-grpc-compression/internal/deptest/testlib" | ||
|
||
// If these were moved above, the test would fail because | ||
// testlib has the same no-clobber logic as the main packages. | ||
_ "github.com/mostynb/go-grpc-compression/lz4" | ||
_ "github.com/mostynb/go-grpc-compression/snappy" | ||
_ "github.com/mostynb/go-grpc-compression/zstd" | ||
"github.com/stretchr/testify/require" | ||
"google.golang.org/grpc/encoding" | ||
) | ||
|
||
func TestCompressorNotClobbered(t *testing.T) { | ||
// Because the deptest/lib imports first, it's init() function | ||
// registers dummy compressors. The following libraries do not | ||
// clobber, so we should find Dummy compressors. | ||
for _, name := range testlib.AllNames { | ||
_, ok := encoding.GetCompressor(name).(testlib.Dummy) | ||
require.True(t, ok) | ||
} | ||
} |
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,52 @@ | ||
// Copyright 2020 Mostyn Bramley-Moore. | ||
// | ||
// 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 testlib | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
|
||
"google.golang.org/grpc/encoding" | ||
) | ||
|
||
type Dummy string | ||
|
||
var _ encoding.Compressor = Dummy("") | ||
|
||
// AllNames lists every compressor in this repo, for testing. | ||
var AllNames = []string{"zstd", "snappy", "lz4"} | ||
|
||
func (d Dummy) Compress(io.Writer) (io.WriteCloser, error) { | ||
return nil, fmt.Errorf("not implemented") | ||
} | ||
|
||
func (d Dummy) Decompress(r io.Reader) (io.Reader, error) { | ||
return nil, fmt.Errorf("not implemented") | ||
} | ||
|
||
func (d Dummy) Name() string { | ||
return string(d) | ||
} | ||
|
||
func init() { | ||
for _, name := range AllNames { | ||
// This test will not register the dummies in case | ||
// of existing registrations, this ensures the import | ||
// order of the deptest actually tests no-clobbering. | ||
if encoding.GetCompressor(name) == nil { | ||
encoding.RegisterCompressor(Dummy(name)) | ||
} | ||
} | ||
} |