2021-02-12 16:33:16 +03:00
|
|
|
package broker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
2021-07-23 12:03:18 +03:00
|
|
|
|
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()
|
|
|
|
|
|
|
|
if err := b.Connect(ctx); err != nil {
|
|
|
|
t.Fatalf("Unexpected connect error %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
topic := "test"
|
|
|
|
count := 10
|
|
|
|
|
|
|
|
fn := func(evts Events) error {
|
|
|
|
return evts.Ack()
|
|
|
|
}
|
|
|
|
|
|
|
|
sub, err := b.BatchSubscribe(ctx, topic, fn)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error subscribing %v", err)
|
|
|
|
}
|
|
|
|
|
2021-09-30 21:00:02 +03:00
|
|
|
msgs := make([]*Message, 0, count)
|
2021-07-22 22:53:44 +03:00
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
message := &Message{
|
|
|
|
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()
|
|
|
|
|
|
|
|
if err := b.Connect(ctx); err != nil {
|
|
|
|
t.Fatalf("Unexpected connect error %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
topic := "test"
|
|
|
|
count := 10
|
|
|
|
|
|
|
|
fn := func(p Event) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
sub, err := b.Subscribe(ctx, topic, fn)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error subscribing %v", err)
|
|
|
|
}
|
|
|
|
|
2021-09-30 21:00:02 +03:00
|
|
|
msgs := make([]*Message, 0, count)
|
2021-02-12 16:33:16 +03:00
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
message := &Message{
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|