mirror of
https://github.com/luxfi/p2p.git
synced 2026-10-30 15:22:55 +00:00
Modules declare go 1.27.1 and the build images follow. go fix applied the modernizers; omitzero is not applied, since it changes what the JSON says. Co-Authored-By: Claude Opus 5 <[email protected]>
79 lines
1.6 KiB
Go
79 lines
1.6 KiB
Go
// Copyright (C) 2019-2025, Lux Industries, Inc. All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
// Copyright (C) 2019-2025, Lux Industries Inc. All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
package peer
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/luxfi/log"
|
|
"github.com/luxfi/p2p/message"
|
|
)
|
|
|
|
func TestMessageQueue(t *testing.T) {
|
|
require := require.New(t)
|
|
|
|
expectFail := false
|
|
q := NewBlockingMessageQueue(
|
|
SendFailedFunc(func(message.OutboundMessage) {
|
|
require.True(expectFail)
|
|
}),
|
|
log.NoLog{},
|
|
0,
|
|
)
|
|
|
|
mc := newMessageCreator(t)
|
|
msgs := []message.OutboundMessage{}
|
|
numToSend := 10
|
|
|
|
// Assert that the messages are popped in the same order they were pushed
|
|
for i := range numToSend {
|
|
m, err := mc.Ping(uint32(i))
|
|
require.NoError(err)
|
|
msgs = append(msgs, m)
|
|
}
|
|
|
|
go func() {
|
|
for i := range numToSend {
|
|
q.Push(context.Background(), msgs[i])
|
|
}
|
|
}()
|
|
|
|
for i := range numToSend {
|
|
msg, ok := q.Pop()
|
|
require.True(ok)
|
|
require.Equal(msgs[i], msg)
|
|
}
|
|
|
|
// Assert that PopNow returns false when the queue is empty
|
|
_, ok := q.PopNow()
|
|
require.False(ok)
|
|
|
|
// Assert that Push returns false when the context is canceled
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
cancel()
|
|
expectFail = true
|
|
gotOk := make(chan bool)
|
|
go func() {
|
|
gotOk <- q.Push(ctx, msgs[0])
|
|
}()
|
|
require.False(<-gotOk)
|
|
|
|
// Assert that Push returns false when the queue is closed
|
|
go func() {
|
|
gotOk <- q.Push(context.Background(), msgs[0])
|
|
}()
|
|
q.Close()
|
|
require.False(<-gotOk)
|
|
|
|
// Assert Pop returns false when the queue is closed
|
|
_, ok = q.Pop()
|
|
require.False(ok)
|
|
}
|