2024-08-01 13:15:05 +00:00
|
|
|
package publish
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol"
|
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/pb"
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFifoQueue(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
queue := NewMessageQueue(10, false)
|
|
|
|
go queue.Start(ctx)
|
|
|
|
|
2024-08-06 20:06:53 +00:00
|
|
|
err := queue.Push(ctx, protocol.NewEnvelope(&pb.WakuMessage{}, 0, "A"))
|
|
|
|
require.NoError(t, err)
|
2024-08-01 13:15:05 +00:00
|
|
|
|
2024-08-06 20:06:53 +00:00
|
|
|
err = queue.Push(ctx, protocol.NewEnvelope(&pb.WakuMessage{}, 0, "B"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = queue.Push(ctx, protocol.NewEnvelope(&pb.WakuMessage{}, 0, "C"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
envelope, ok := <-queue.Pop(ctx)
|
2024-08-01 13:15:05 +00:00
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, "A", envelope.PubsubTopic())
|
|
|
|
|
2024-08-06 20:06:53 +00:00
|
|
|
envelope, ok = <-queue.Pop(ctx)
|
2024-08-01 13:15:05 +00:00
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, "B", envelope.PubsubTopic())
|
|
|
|
|
2024-08-06 20:06:53 +00:00
|
|
|
envelope, ok = <-queue.Pop(ctx)
|
2024-08-01 13:15:05 +00:00
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, "C", envelope.PubsubTopic())
|
|
|
|
|
|
|
|
cancel()
|
|
|
|
|
2024-08-06 20:06:53 +00:00
|
|
|
_, ok = <-queue.Pop(ctx)
|
2024-08-01 13:15:05 +00:00
|
|
|
require.False(t, ok)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPriorityQueue(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
queue := NewMessageQueue(10, true)
|
|
|
|
go queue.Start(ctx)
|
|
|
|
|
2024-08-06 20:06:53 +00:00
|
|
|
err := queue.Push(ctx, protocol.NewEnvelope(&pb.WakuMessage{Timestamp: proto.Int64(0)}, 0, "A"), LowPriority)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = queue.Push(ctx, protocol.NewEnvelope(&pb.WakuMessage{Timestamp: proto.Int64(1)}, 0, "B"), LowPriority)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = queue.Push(ctx, protocol.NewEnvelope(&pb.WakuMessage{Timestamp: proto.Int64(2)}, 0, "C"), HighPriority)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = queue.Push(ctx, protocol.NewEnvelope(&pb.WakuMessage{Timestamp: proto.Int64(3)}, 0, "D"), NormalPriority)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = queue.Push(ctx, protocol.NewEnvelope(&pb.WakuMessage{Timestamp: proto.Int64(4)}, 0, "E"), HighPriority)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = queue.Push(ctx, protocol.NewEnvelope(&pb.WakuMessage{Timestamp: proto.Int64(5)}, 0, "F"), LowPriority)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = queue.Push(ctx, protocol.NewEnvelope(&pb.WakuMessage{Timestamp: proto.Int64(6)}, 0, "G"), NormalPriority)
|
|
|
|
require.NoError(t, err)
|
2024-08-01 13:15:05 +00:00
|
|
|
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
|
2024-08-06 20:06:53 +00:00
|
|
|
envelope, ok := <-queue.Pop(ctx)
|
2024-08-01 13:15:05 +00:00
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, "C", envelope.PubsubTopic())
|
|
|
|
|
2024-08-06 20:06:53 +00:00
|
|
|
envelope, ok = <-queue.Pop(ctx)
|
2024-08-01 13:15:05 +00:00
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, "E", envelope.PubsubTopic())
|
|
|
|
|
2024-08-06 20:06:53 +00:00
|
|
|
envelope, ok = <-queue.Pop(ctx)
|
2024-08-01 13:15:05 +00:00
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, "D", envelope.PubsubTopic())
|
|
|
|
|
2024-08-06 20:06:53 +00:00
|
|
|
envelope, ok = <-queue.Pop(ctx)
|
2024-08-01 13:15:05 +00:00
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, "G", envelope.PubsubTopic())
|
|
|
|
|
2024-08-06 20:06:53 +00:00
|
|
|
envelope, ok = <-queue.Pop(ctx)
|
2024-08-01 13:15:05 +00:00
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, "A", envelope.PubsubTopic())
|
|
|
|
|
2024-08-06 20:06:53 +00:00
|
|
|
envelope, ok = <-queue.Pop(ctx)
|
2024-08-01 13:15:05 +00:00
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, "B", envelope.PubsubTopic())
|
|
|
|
|
2024-08-06 20:06:53 +00:00
|
|
|
envelope, ok = <-queue.Pop(ctx)
|
2024-08-01 13:15:05 +00:00
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, "F", envelope.PubsubTopic())
|
|
|
|
|
|
|
|
cancel()
|
|
|
|
|
2024-08-06 20:06:53 +00:00
|
|
|
_, ok = <-queue.Pop(ctx)
|
2024-08-01 13:15:05 +00:00
|
|
|
require.False(t, ok)
|
|
|
|
|
|
|
|
}
|