github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/broker/memory/memory_test.go (about)

     1  // Copyright 2020 Asim Aslam
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     https://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // Original source: github.com/micro/go-micro/v3/broker/memory/memory_test.go
    16  
    17  package memory
    18  
    19  import (
    20  	"fmt"
    21  	"testing"
    22  
    23  	"github.com/tickoalcantara12/micro/v3/service/broker"
    24  )
    25  
    26  func TestMemoryBroker(t *testing.T) {
    27  	b := NewBroker()
    28  
    29  	if err := b.Connect(); err != nil {
    30  		t.Fatalf("Unexpected connect error %v", err)
    31  	}
    32  
    33  	topic := "test"
    34  	count := 10
    35  
    36  	fn := func(m *broker.Message) error {
    37  		return nil
    38  	}
    39  
    40  	sub, err := b.Subscribe(topic, fn)
    41  	if err != nil {
    42  		t.Fatalf("Unexpected error subscribing %v", err)
    43  	}
    44  
    45  	for i := 0; i < count; i++ {
    46  		message := &broker.Message{
    47  			Header: map[string]string{
    48  				"foo": "bar",
    49  				"id":  fmt.Sprintf("%d", i),
    50  			},
    51  			Body: []byte(`hello world`),
    52  		}
    53  
    54  		if err := b.Publish(topic, message); err != nil {
    55  			t.Fatalf("Unexpected error publishing %d", i)
    56  		}
    57  	}
    58  
    59  	if err := sub.Unsubscribe(); err != nil {
    60  		t.Fatalf("Unexpected error unsubscribing from %s: %v", topic, err)
    61  	}
    62  
    63  	if err := b.Disconnect(); err != nil {
    64  		t.Fatalf("Unexpected connect error %v", err)
    65  	}
    66  }