Skip to content

Commit

Permalink
Add tests for non-clobbering codec registration
Browse files Browse the repository at this point in the history
This should fail, until the following commit which switches the test
to use the "nonclobbering" versions of these codecs.
  • Loading branch information
jmacd authored and mostynb committed Jun 26, 2023
1 parent 673fd2f commit 7b42e42
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
43 changes: 43 additions & 0 deletions internal/deptest/deptest_test.go
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)
}
}
52 changes: 52 additions & 0 deletions internal/deptest/testlib/testlib.go
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))
}
}
}

0 comments on commit 7b42e42

Please sign in to comment.