Commit 219eaf6a authored by Andreas Bigger's avatar Andreas Bigger

table-driven tests for the input threshold function

parent f208b022
...@@ -7,33 +7,96 @@ import ( ...@@ -7,33 +7,96 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
// TestInputThreshold tests the [ChannelConfig.InputThreshold] function. // TestInputThreshold tests the [ChannelConfig.InputThreshold]
// function using a table-driven testing approach.
func TestInputThreshold(t *testing.T) { func TestInputThreshold(t *testing.T) {
// Construct an empty channel config type testInput struct {
config := batcher.ChannelConfig{ TargetFrameSize uint64
SeqWindowSize: 15, TargetNumFrames int
ChannelTimeout: 40, ApproxComprRatio float64
MaxChannelDuration: 1, }
SubSafetyMargin: 4, type test struct {
MaxFrameSize: 120000, input testInput
TargetFrameSize: 100000, want uint64
TargetNumFrames: 1,
ApproxComprRatio: 0.4,
} }
// The input threshold is calculated as: (targetNumFrames * targetFrameSize) / approxComprRatio // Construct test cases that test the boundary conditions
// Here we see that 100,000 / 0.4 = 100,000 * 2.5 = 250,000 tests := []test{
inputThreshold := config.InputThreshold() {
require.Equal(t, uint64(250_000), inputThreshold) input: testInput{
TargetFrameSize: 1,
// Set the approximate compression ratio to 0 TargetNumFrames: 1,
// Logically, this represents infinite compression, ApproxComprRatio: 0.4,
// so there is no threshold on the size of the input. },
// In practice, this should never be set to 0. want: 2,
config.ApproxComprRatio = 0 },
{
input: testInput{
TargetFrameSize: 1,
TargetNumFrames: 1,
ApproxComprRatio: 1,
},
want: 1,
},
{
input: testInput{
TargetFrameSize: 1,
TargetNumFrames: 1,
ApproxComprRatio: 2,
},
want: 0,
},
{
input: testInput{
TargetFrameSize: 100000,
TargetNumFrames: 1,
ApproxComprRatio: 0.4,
},
want: 250_000,
},
{
input: testInput{
TargetFrameSize: 1,
TargetNumFrames: 100000,
ApproxComprRatio: 0.4,
},
want: 250_000,
},
{
input: testInput{
TargetFrameSize: 100000,
TargetNumFrames: 100000,
ApproxComprRatio: 0.4,
},
want: 25_000_000_000,
},
// A compression ratio of 0 means there is no input threshold
{
input: testInput{
TargetFrameSize: 100000,
TargetNumFrames: 100000,
ApproxComprRatio: 0,
},
want: uint64(0xffffffffffffffff),
},
{
input: testInput{
TargetFrameSize: 0,
TargetNumFrames: 0,
ApproxComprRatio: 0,
},
want: 0,
},
}
// The input threshold will overflow to the max uint64 value // Validate each test case
receivedThreshold := config.InputThreshold() for _, tt := range tests {
max := config.TargetNumFrames * int(config.TargetFrameSize) config := batcher.ChannelConfig{
require.True(t, receivedThreshold > uint64(max)) TargetFrameSize: tt.input.TargetFrameSize,
TargetNumFrames: tt.input.TargetNumFrames,
ApproxComprRatio: tt.input.ApproxComprRatio,
}
got := config.InputThreshold()
require.Equal(t, tt.want, got)
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment