|
| 1 | +package gofr |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "gofr.dev/pkg/gofr/container" |
| 12 | + "gofr.dev/pkg/gofr/datasource/pubsub" |
| 13 | + "gofr.dev/pkg/gofr/logging" |
| 14 | + "gofr.dev/pkg/gofr/testutil" |
| 15 | +) |
| 16 | + |
| 17 | +var errHandler = errors.New("error in subscribing") |
| 18 | + |
| 19 | +func handleError(err string) error { |
| 20 | + return fmt.Errorf("%w: %s", errHandler, err) |
| 21 | +} |
| 22 | + |
| 23 | +var errSubscription = errors.New("subscription error") |
| 24 | + |
| 25 | +func subscriptionError(err string) error { |
| 26 | + return fmt.Errorf("%w: %s", errSubscription, err) |
| 27 | +} |
| 28 | + |
| 29 | +type mockSubscriber struct { |
| 30 | +} |
| 31 | + |
| 32 | +func (s mockSubscriber) Publish(_ context.Context, _ string, _ []byte) error { |
| 33 | + return nil |
| 34 | +} |
| 35 | + |
| 36 | +type Message struct { |
| 37 | + Topic string |
| 38 | + Value []byte |
| 39 | + MetaData interface{} |
| 40 | +} |
| 41 | + |
| 42 | +func (mockSubscriber) Subscribe(_ context.Context, topic string) (*pubsub.Message, error) { |
| 43 | + if topic == "test-topic" { |
| 44 | + return &pubsub.Message{ |
| 45 | + Topic: "test-topic", |
| 46 | + Value: []byte(`{"data":{"productId":"123","price":"599"}}`), |
| 47 | + }, nil |
| 48 | + } |
| 49 | + |
| 50 | + return &pubsub.Message{ |
| 51 | + Topic: "test-topic", |
| 52 | + Value: []byte(`{"data":{"productId":"123","price":"599"}}`), |
| 53 | + }, subscriptionError("subscription error") |
| 54 | +} |
| 55 | + |
| 56 | +func TestSubscriptionManager_HandlerError(t *testing.T) { |
| 57 | + done := make(chan struct{}) |
| 58 | + |
| 59 | + testLogs := testutil.StderrOutputForFunc(func() { |
| 60 | + mockContainer := container.Container{ |
| 61 | + Logger: logging.NewLogger(logging.ERROR), |
| 62 | + PubSub: mockSubscriber{}, |
| 63 | + } |
| 64 | + subscriptionManager := newSubscriptionManager(&mockContainer) |
| 65 | + |
| 66 | + // Run the subscriber in a goroutine |
| 67 | + go func() { |
| 68 | + subscriptionManager.startSubscriber("test-topic", |
| 69 | + func(c *Context) error { |
| 70 | + return handleError("error in test-topic") |
| 71 | + }) |
| 72 | + }() |
| 73 | + |
| 74 | + // this sleep is added to wait for StderrOutputForFunc to collect the logs inside the testLogs |
| 75 | + time.Sleep(1 * time.Millisecond) |
| 76 | + }) |
| 77 | + |
| 78 | + // signal the test to end |
| 79 | + close(done) |
| 80 | + |
| 81 | + if !strings.Contains(testLogs, "error in handler for topic test-topic: error in subscribing: error in test-topic") { |
| 82 | + t.Error("TestSubscriptionManager_HandlerError Failed! Missing log message about handler error") |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func TestSubscriptionManager_SubscribeError(t *testing.T) { |
| 87 | + done := make(chan struct{}) |
| 88 | + |
| 89 | + testLogs := testutil.StderrOutputForFunc(func() { |
| 90 | + mockContainer := container.Container{ |
| 91 | + Logger: logging.NewLogger(logging.ERROR), |
| 92 | + PubSub: mockSubscriber{}, |
| 93 | + } |
| 94 | + subscriptionManager := newSubscriptionManager(&mockContainer) |
| 95 | + |
| 96 | + // Run the subscriber in a goroutine |
| 97 | + go func() { |
| 98 | + subscriptionManager.startSubscriber("abc", |
| 99 | + func(c *Context) error { |
| 100 | + return handleError("error in abc") |
| 101 | + }) |
| 102 | + }() |
| 103 | + |
| 104 | + // this sleep is added to wait for StderrOutputForFunc to collect the logs inside the testLogs |
| 105 | + time.Sleep(1 * time.Millisecond) |
| 106 | + }) |
| 107 | + |
| 108 | + // signal the test to end |
| 109 | + close(done) |
| 110 | + |
| 111 | + if !strings.Contains(testLogs, "error while reading from Kafka, err: ") { |
| 112 | + t.Error("TestSubscriptionManager_SubscribeError Failed! Missing log message about subscription error") |
| 113 | + } |
| 114 | +} |
0 commit comments