Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bruce-riley committed Jun 8, 2024
1 parent 7e93adc commit ed0137b
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions node/pkg/common/channel_utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package common

import (
"context"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

const myDelay = time.Millisecond * 100
const myMaxSize = 2
const myQueueSize = myMaxSize * 10

func TestReadFromChannelWithTimeout_NoData(t *testing.T) {
ctx := context.Background()
myChan := make(chan int, myQueueSize)

// No data should timeout.
timeout, cancel := context.WithTimeout(ctx, myDelay)
defer cancel()
observations, err := ReadFromChannelWithTimeout[int](timeout, myChan, myMaxSize)
assert.Equal(t, err, context.DeadlineExceeded)
assert.Equal(t, 0, len(observations))
}

func TestReadFromChannelWithTimeout_SomeData(t *testing.T) {
ctx := context.Background()
myChan := make(chan int, myQueueSize)
myChan <- 1

// Some data but not enough to fill a message should timeout and return the data.
timeout, cancel := context.WithTimeout(ctx, myDelay)
defer cancel()
observations, err := ReadFromChannelWithTimeout[int](timeout, myChan, myMaxSize)
assert.Equal(t, err, context.DeadlineExceeded)
require.Equal(t, 1, len(observations))
assert.Equal(t, 1, observations[0])
}

func TestReadFromChannelWithTimeout_JustEnoughData(t *testing.T) {
ctx := context.Background()
myChan := make(chan int, myQueueSize)
myChan <- 1
myChan <- 2

// Just enough data should return the data and no error.
timeout, cancel := context.WithTimeout(ctx, myDelay)
defer cancel()
observations, err := ReadFromChannelWithTimeout[int](timeout, myChan, myMaxSize)
assert.NoError(t, err)
require.Equal(t, 2, len(observations))
assert.Equal(t, 1, observations[0])
assert.Equal(t, 2, observations[1])
}

func TestReadFromChannelWithTimeout_TooMuchData(t *testing.T) {
ctx := context.Background()
myChan := make(chan int, myQueueSize)
myChan <- 1
myChan <- 2
myChan <- 3

// If there is more data than will fit, it should immediately return a full message, then timeout and return the remainder.
timeout, cancel := context.WithTimeout(ctx, myDelay)
defer cancel()
observations, err := ReadFromChannelWithTimeout[int](timeout, myChan, myMaxSize)
assert.NoError(t, err)
require.Equal(t, 2, len(observations))
assert.Equal(t, 1, observations[0])
assert.Equal(t, 2, observations[1])

timeout2, cancel2 := context.WithTimeout(ctx, myDelay)
defer cancel2()
observations, err = ReadFromChannelWithTimeout[int](timeout2, myChan, myMaxSize)
assert.Equal(t, err, context.DeadlineExceeded)
require.Equal(t, 1, len(observations))
assert.Equal(t, 3, observations[0])
}

0 comments on commit ed0137b

Please sign in to comment.