github.com/angryronald/go-kit@v0.0.0-20240505173814-ff2bd9c79dbf/publisher/kafka/publisher.service_test.go (about)

     1  package kafka
     2  
     3  import (
     4  	"context"
     5  	"log"
     6  	"testing"
     7  
     8  	"gocloud.dev/pubsub"
     9  	"gocloud.dev/pubsub/kafkapubsub"
    10  
    11  	"github.com/golang/mock/gomock"
    12  	"github.com/google/uuid"
    13  	"github.com/pkg/errors"
    14  	"github.com/stretchr/testify/assert"
    15  )
    16  
    17  var pubsubTopics map[string]*pubsub.Topic
    18  var subscription map[string]*pubsub.Subscription
    19  
    20  func init() {
    21  	pubsubTopics = map[string]*pubsub.Topic{}
    22  	subscription = map[string]*pubsub.Subscription{}
    23  }
    24  
    25  const testGroupID = "test-group"
    26  
    27  // make sure kafka already running, please run docker-compose up inside /test/docker/kafka dir
    28  
    29  // #2 run this after running the subscriber part
    30  func TestRabbitmqPublisher_Publish(t *testing.T) {
    31  	var err error
    32  
    33  	// Create a testing context.
    34  	ctx := context.Background()
    35  	// The set of brokers in the Kafka cluster.
    36  	addrs := []string{"localhost:9092"}
    37  	// The Kafka client configuration to use.
    38  	config := kafkapubsub.MinimalConfig()
    39  
    40  	// Open the topic.
    41  	pubsubTopics[topicName], err = kafkapubsub.OpenTopic(addrs, config, topicName, nil)
    42  	if err != nil {
    43  		log.Fatalf("Failed to open Kafka topic: %v", err)
    44  	}
    45  	defer pubsubTopics[topicName].Shutdown(ctx)
    46  
    47  	// Open the subscription with the specified group ID.
    48  	subscription[topicName], err = kafkapubsub.OpenSubscription(addrs, config, testGroupID, []string{topicName}, nil)
    49  	if err != nil {
    50  		log.Fatalf("Failed to open Kafka subscription: %v", err)
    51  	}
    52  	defer subscription[topicName].Shutdown(ctx)
    53  
    54  	// Create a mock controller.
    55  	ctrl := gomock.NewController(t)
    56  	defer ctrl.Finish()
    57  
    58  	// Create a publisher.
    59  	publisher := NewPublisher(pubsubTopics)
    60  
    61  	// Publish a message.
    62  	if err := publisher.Publish(ctx, topicName, uuid.NewString(), []byte(`{"ID":"123456"}`)); err != nil {
    63  		log.Fatalf("error when publishing event: %v (stack trace: %v)", err, errors.WithStack(err))
    64  		t.Errorf("error when publishing event: %v", err)
    65  	}
    66  
    67  	// // Verify that the message is received by the subscription.
    68  	// msg, err := subscription[topicName].Receive(ctx)
    69  	// if err != nil {
    70  	// 	t.Errorf("error when consuming event: %v", err)
    71  	// }
    72  
    73  	// msg.Ack()
    74  	// assert.Nil(t, ctx.Err())
    75  }
    76  
    77  // #1 Make sure to run this one first, since kafka subscriber will lost the message if the subscriber start after the message publish
    78  func TestRabbitmqPublisher_Subscribe(t *testing.T) {
    79  	var err error
    80  
    81  	// Create a testing context.
    82  	ctx := context.Background()
    83  	// The set of brokers in the Kafka cluster.
    84  	addrs := []string{"localhost:9092"}
    85  	// The Kafka client configuration to use.
    86  	config := kafkapubsub.MinimalConfig()
    87  
    88  	// Open the subscription with the specified group ID.
    89  	subscription[topicName], err = kafkapubsub.OpenSubscription(addrs, config, testGroupID, []string{topicName}, nil)
    90  	if err != nil {
    91  		log.Fatalf("Failed to open Kafka subscription: %v", err)
    92  	}
    93  	defer subscription[topicName].Shutdown(ctx)
    94  
    95  	// Create a mock controller.
    96  	ctrl := gomock.NewController(t)
    97  	defer ctrl.Finish()
    98  
    99  	// Verify that the message is received by the subscription.
   100  	msg, err := subscription[topicName].Receive(ctx)
   101  	if err != nil {
   102  		t.Errorf("error when consuming event: %v", err)
   103  	}
   104  
   105  	msg.Ack()
   106  	assert.Nil(t, ctx.Err())
   107  }