cml04-eventer/subscriber_test.go

79 lines
1.8 KiB
Go

package cml04eventer
import (
"context"
"encoding/json"
"testing"
"time"
"github.com/nsqio/go-nsq"
"github.com/stretchr/testify/assert"
)
// MockUnmarshaler is an implementation of Unmarshaler for testing
type MockUnmarshaler struct{}
func (m MockUnmarshaler) Unmarshal(nsqMsg *nsq.Message) (*Event, error) {
// Simply decode the JSON message for testing
var event Event
err := json.Unmarshal(nsqMsg.Body, &event)
return &event, err
}
func TestSubscriberImpl_SubscribeEvent(t *testing.T) {
//eventChan := make(chan *Event)
// Create an instance of MockLoggerAdapter
mockLogger := &MockLoggerAdapter{}
// Test configuration for the Subscriber
config := &SubscriberConfig{
NSQAddress: "localhost",
NSQPort: 4150,
Unmarshaler: MockUnmarshaler{},
Channel: "test-channel",
}
// Create an instance of SubscriberImpl for testing
subscriber, err := NewSubscriber(config, mockLogger)
assert.NoError(t, err)
// Create a context for testing
ctx := context.Background()
// Subscribe to the event
eventChan, err := subscriber.SubscribeEvent(ctx, "test-topic")
assert.NoError(t, err)
t.Log("Subscribed to event")
t.Log(eventChan)
// Wait for the message to be processed (you can adapt this as needed)
time.Sleep(1 * time.Second)
// Close the subscriber
err = subscriber.Close()
assert.NoError(t, err)
}
func TestSubscriberImpl_Close(t *testing.T) {
// Create an instance of MockLoggerAdapter
mockLogger := &MockLoggerAdapter{}
// Test configuration for the Subscriber
config := &SubscriberConfig{
NSQAddress: "localhost",
NSQPort: 4150,
Unmarshaler: MockUnmarshaler{},
Channel: "test-channel",
}
// Create an instance of SubscriberImpl for testing
subscriber, err := NewSubscriber(config, mockLogger)
assert.NoError(t, err)
// Close the subscriber
err = subscriber.Close()
assert.NoError(t, err)
}