|
9 | 9 | "github.com/prometheus/client_golang/prometheus"
|
10 | 10 | dto "github.com/prometheus/client_model/go"
|
11 | 11 | "github.com/stretchr/testify/assert"
|
| 12 | + |
12 | 13 | "github.com/uw-labs/substrate"
|
13 | 14 | )
|
14 | 15 |
|
@@ -79,6 +80,60 @@ func TestPublishMessagesSuccessfully(t *testing.T) {
|
79 | 80 | }
|
80 | 81 | }
|
81 | 82 |
|
| 83 | +func TestPublishMessagesSuccessfully_WithContextError(t *testing.T) { |
| 84 | + sink := AsyncMessageSink{ |
| 85 | + impl: &asyncMessageSinkMock{ |
| 86 | + publishMessageMock: func(ctx context.Context, acks chan<- substrate.Message, messages <-chan substrate.Message) error { |
| 87 | + for { |
| 88 | + select { |
| 89 | + case <-ctx.Done(): |
| 90 | + return ctx.Err() |
| 91 | + case msg := <-messages: |
| 92 | + acks <- msg |
| 93 | + } |
| 94 | + } |
| 95 | + }, |
| 96 | + }, |
| 97 | + counter: prometheus.NewCounterVec( |
| 98 | + prometheus.CounterOpts{ |
| 99 | + Help: "sink_counter", |
| 100 | + Name: "sink_counter", |
| 101 | + }, []string{"status", "topic"}), |
| 102 | + topic: "testTopic", |
| 103 | + } |
| 104 | + |
| 105 | + acks := make(chan substrate.Message) |
| 106 | + messages := make(chan substrate.Message) |
| 107 | + |
| 108 | + sinkContext, sinkCancel := context.WithCancel(context.Background()) |
| 109 | + defer sinkCancel() |
| 110 | + |
| 111 | + errs := make(chan error) |
| 112 | + go func() { |
| 113 | + defer close(errs) |
| 114 | + errs <- sink.PublishMessages(sinkContext, acks, messages) |
| 115 | + }() |
| 116 | + |
| 117 | + messages <- Message{} |
| 118 | + |
| 119 | + for { |
| 120 | + select { |
| 121 | + case err := <-errs: |
| 122 | + assert.True(t, errors.Is(err, context.Canceled)) |
| 123 | + // the error counter should not be increased when producing stops by context canceling |
| 124 | + var metric dto.Metric |
| 125 | + assert.NoError(t, sink.counter.WithLabelValues("error", "testTopic").Write(&metric)) |
| 126 | + assert.Equal(t, 0, int(*metric.Counter.Value)) |
| 127 | + return |
| 128 | + case <-acks: |
| 129 | + var metric dto.Metric |
| 130 | + assert.NoError(t, sink.counter.WithLabelValues("success", "testTopic").Write(&metric)) |
| 131 | + assert.Equal(t, 1, int(*metric.Counter.Value)) |
| 132 | + sinkCancel() |
| 133 | + } |
| 134 | + } |
| 135 | +} |
| 136 | + |
82 | 137 | func TestPublishMessagesWithError(t *testing.T) {
|
83 | 138 | producingError := errors.New("message producing error")
|
84 | 139 | sink := AsyncMessageSink{
|
|
0 commit comments