2021-02-12 16:33:16 +03:00
|
|
|
package broker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
2021-07-23 12:03:18 +03:00
|
|
|
|
2024-03-04 01:05:40 +03:00
|
|
|
"go.unistack.org/micro/v3/broker"
|
2021-10-02 19:55:07 +03:00
|
|
|
"go.unistack.org/micro/v3/metadata"
|
2021-02-12 16:33:16 +03:00
|
|
|
)
|
|
|
|
|
2021-07-22 22:53:44 +03:00
|
|
|
func TestMemoryBatchBroker(t *testing.T) {
|
|
|
|
b := NewBroker()
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2024-04-22 08:47:50 +03:00
|
|
|
if err := b.Init(); err != nil {
|
|
|
|
t.Fatalf("Unexpected init error %v", err)
|
|
|
|
}
|
|
|
|
|
2021-07-22 22:53:44 +03:00
|
|
|
if err := b.Connect(ctx); err != nil {
|
|
|
|
t.Fatalf("Unexpected connect error %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
topic := "test"
|
|
|
|
count := 10
|
|
|
|
|
2024-03-04 01:05:40 +03:00
|
|
|
fn := func(evts broker.Events) error {
|
2021-07-22 22:53:44 +03:00
|
|
|
return evts.Ack()
|
|
|
|
}
|
|
|
|
|
|
|
|
sub, err := b.BatchSubscribe(ctx, topic, fn)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error subscribing %v", err)
|
|
|
|
}
|
|
|
|
|
2024-03-04 01:05:40 +03:00
|
|
|
msgs := make([]*broker.Message, 0, count)
|
2021-07-22 22:53:44 +03:00
|
|
|
for i := 0; i < count; i++ {
|
2024-03-04 01:05:40 +03:00
|
|
|
message := &broker.Message{
|
2021-07-22 22:53:44 +03:00
|
|
|
Header: map[string]string{
|
2021-07-23 12:03:18 +03:00
|
|
|
metadata.HeaderTopic: topic,
|
|
|
|
"foo": "bar",
|
|
|
|
"id": fmt.Sprintf("%d", i),
|
2021-07-22 22:53:44 +03:00
|
|
|
},
|
|
|
|
Body: []byte(`"hello world"`),
|
|
|
|
}
|
|
|
|
msgs = append(msgs, message)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := b.BatchPublish(ctx, msgs); err != nil {
|
|
|
|
t.Fatalf("Unexpected error publishing %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := sub.Unsubscribe(ctx); err != nil {
|
|
|
|
t.Fatalf("Unexpected error unsubscribing from %s: %v", topic, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := b.Disconnect(ctx); err != nil {
|
|
|
|
t.Fatalf("Unexpected connect error %v", err)
|
|
|
|
}
|
|
|
|
}
|
2021-09-28 23:43:43 +03:00
|
|
|
|
2021-02-12 16:33:16 +03:00
|
|
|
func TestMemoryBroker(t *testing.T) {
|
|
|
|
b := NewBroker()
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2024-04-22 08:47:50 +03:00
|
|
|
if err := b.Init(); err != nil {
|
|
|
|
t.Fatalf("Unexpected init error %v", err)
|
|
|
|
}
|
|
|
|
|
2021-02-12 16:33:16 +03:00
|
|
|
if err := b.Connect(ctx); err != nil {
|
|
|
|
t.Fatalf("Unexpected connect error %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
topic := "test"
|
|
|
|
count := 10
|
|
|
|
|
2024-12-09 16:23:25 +03:00
|
|
|
fn := func(_ broker.Event) error {
|
2021-02-12 16:33:16 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
sub, err := b.Subscribe(ctx, topic, fn)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error subscribing %v", err)
|
|
|
|
}
|
|
|
|
|
2024-03-04 01:05:40 +03:00
|
|
|
msgs := make([]*broker.Message, 0, count)
|
2021-02-12 16:33:16 +03:00
|
|
|
for i := 0; i < count; i++ {
|
2024-03-04 01:05:40 +03:00
|
|
|
message := &broker.Message{
|
2021-02-12 16:33:16 +03:00
|
|
|
Header: map[string]string{
|
2021-07-23 12:03:18 +03:00
|
|
|
metadata.HeaderTopic: topic,
|
|
|
|
"foo": "bar",
|
|
|
|
"id": fmt.Sprintf("%d", i),
|
2021-02-12 16:33:16 +03:00
|
|
|
},
|
2021-07-01 15:56:22 +03:00
|
|
|
Body: []byte(`"hello world"`),
|
2021-02-12 16:33:16 +03:00
|
|
|
}
|
2021-07-22 22:53:44 +03:00
|
|
|
msgs = append(msgs, message)
|
2021-02-12 16:33:16 +03:00
|
|
|
|
|
|
|
if err := b.Publish(ctx, topic, message); err != nil {
|
2021-07-22 22:53:44 +03:00
|
|
|
t.Fatalf("Unexpected error publishing %d err: %v", i, err)
|
2021-02-12 16:33:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-22 22:53:44 +03:00
|
|
|
if err := b.BatchPublish(ctx, msgs); err != nil {
|
|
|
|
t.Fatalf("Unexpected error publishing %v", err)
|
|
|
|
}
|
|
|
|
|
2021-02-12 16:33:16 +03:00
|
|
|
if err := sub.Unsubscribe(ctx); err != nil {
|
|
|
|
t.Fatalf("Unexpected error unsubscribing from %s: %v", topic, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := b.Disconnect(ctx); err != nil {
|
|
|
|
t.Fatalf("Unexpected connect error %v", err)
|
|
|
|
}
|
|
|
|
}
|