forked from wormhole-foundation/wormhole
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Node: Minor tweaks and spy improvement (wormhole-foundation#3974)
* Node: Minor tweaks and spy improvement * Add tests
- Loading branch information
1 parent
fdd2382
commit 0e2ba62
Showing
11 changed files
with
175 additions
and
107 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
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,20 @@ | ||
package common | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
// ReadFromChannelWithTimeout reads events from the channel until a timeout occurs or the max maxCount is reached. | ||
func ReadFromChannelWithTimeout[T any](ctx context.Context, ch <-chan T, maxCount int) ([]T, error) { | ||
out := make([]T, 0, maxCount) | ||
for len(out) < maxCount { | ||
select { | ||
case <-ctx.Done(): | ||
return out, ctx.Err() | ||
case msg := <-ch: | ||
out = append(out, msg) | ||
} | ||
} | ||
|
||
return out, nil | ||
} |
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,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]) | ||
} |
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
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
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
Oops, something went wrong.