| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package queue |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"errors" |
| 9 |
"sync" |
| 10 |
"sync/atomic" |
| 11 |
"testing" |
| 12 |
"time" |
| 13 |
|
| 14 |
"github.com/stretchr/testify/assert" |
| 15 |
"github.com/stretchr/testify/require" |
| 16 |
|
| 17 |
"go.opentelemetry.io/collector/component/componenttest" |
| 18 |
"go.opentelemetry.io/collector/exporter/exporterhelper/internal/request" |
| 19 |
) |
| 20 |
|
| 21 |
func TestMemoryQueue(t *testing.T) { |
| 22 |
set := newSettings(request.SizerTypeItems, 7) |
| 23 |
q := newMemoryQueue[intRequest](set) |
| 24 |
require.NoError(t, q.Start(context.Background(), componenttest.NewNopHost())) |
| 25 |
require.NoError(t, q.Offer(context.Background(), 1)) |
| 26 |
assert.EqualValues(t, 1, q.Size()) |
| 27 |
assert.EqualValues(t, 7, q.Capacity()) |
| 28 |
|
| 29 |
require.NoError(t, q.Offer(context.Background(), 3)) |
| 30 |
assert.EqualValues(t, 4, q.Size()) |
| 31 |
|
| 32 |
|
| 33 |
require.ErrorIs(t, q.Offer(context.Background(), 4), ErrQueueIsFull) |
| 34 |
assert.EqualValues(t, 4, q.Size()) |
| 35 |
|
| 36 |
assert.True(t, consume(q, func(_ context.Context, el intRequest) error { |
| 37 |
assert.EqualValues(t, 1, el) |
| 38 |
return nil |
| 39 |
})) |
| 40 |
assert.EqualValues(t, 3, q.Size()) |
| 41 |
|
| 42 |
assert.True(t, consume(q, func(_ context.Context, el intRequest) error { |
| 43 |
assert.EqualValues(t, 3, el) |
| 44 |
return nil |
| 45 |
})) |
| 46 |
assert.EqualValues(t, 0, q.Size()) |
| 47 |
|
| 48 |
require.NoError(t, q.Shutdown(context.Background())) |
| 49 |
assert.False(t, consume(q, func(context.Context, intRequest) error { t.FailNow(); return nil })) |
| 50 |
require.NoError(t, q.Shutdown(context.Background())) |
| 51 |
} |
| 52 |
|
| 53 |
func TestMemoryQueueBlockingCancelled(t *testing.T) { |
| 54 |
set := newSettings(request.SizerTypeItems, 5) |
| 55 |
set.BlockOnOverflow = true |
| 56 |
q := newMemoryQueue[intRequest](set) |
| 57 |
require.NoError(t, q.Start(context.Background(), componenttest.NewNopHost())) |
| 58 |
require.NoError(t, q.Offer(context.Background(), 3)) |
| 59 |
ctx, cancel := context.WithCancel(context.Background()) |
| 60 |
wg := sync.WaitGroup{} |
| 61 |
wg.Add(1) |
| 62 |
go func() { |
| 63 |
defer wg.Done() |
| 64 |
assert.ErrorIs(t, q.Offer(ctx, 3), context.Canceled) |
| 65 |
}() |
| 66 |
cancel() |
| 67 |
wg.Wait() |
| 68 |
assert.EqualValues(t, 3, q.Size()) |
| 69 |
assert.True(t, consume(q, func(_ context.Context, el intRequest) error { |
| 70 |
assert.EqualValues(t, 3, el) |
| 71 |
return nil |
| 72 |
})) |
| 73 |
require.NoError(t, q.Shutdown(context.Background())) |
| 74 |
} |
| 75 |
|
| 76 |
func TestMemoryQueueDrainWhenShutdown(t *testing.T) { |
| 77 |
set := newSettings(request.SizerTypeItems, 7) |
| 78 |
q := newMemoryQueue[intRequest](set) |
| 79 |
require.NoError(t, q.Start(context.Background(), componenttest.NewNopHost())) |
| 80 |
require.NoError(t, q.Offer(context.Background(), 1)) |
| 81 |
require.NoError(t, q.Offer(context.Background(), 3)) |
| 82 |
|
| 83 |
assert.True(t, consume(q, func(_ context.Context, el intRequest) error { |
| 84 |
assert.EqualValues(t, 1, el) |
| 85 |
return nil |
| 86 |
})) |
| 87 |
assert.EqualValues(t, 3, q.Size()) |
| 88 |
require.NoError(t, q.Shutdown(context.Background())) |
| 89 |
assert.EqualValues(t, 3, q.Size()) |
| 90 |
assert.True(t, consume(q, func(_ context.Context, el intRequest) error { |
| 91 |
assert.EqualValues(t, 3, el) |
| 92 |
return nil |
| 93 |
})) |
| 94 |
assert.EqualValues(t, 0, q.Size()) |
| 95 |
assert.False(t, consume(q, func(context.Context, intRequest) error { t.FailNow(); return nil })) |
| 96 |
require.NoError(t, q.Shutdown(context.Background())) |
| 97 |
} |
| 98 |
|
| 99 |
func TestMemoryQueueOfferInvalidSize(t *testing.T) { |
| 100 |
set := newSettings(request.SizerTypeItems, 1) |
| 101 |
q := newMemoryQueue[intRequest](set) |
| 102 |
require.NoError(t, q.Start(context.Background(), componenttest.NewNopHost())) |
| 103 |
require.ErrorIs(t, q.Offer(context.Background(), -1), errInvalidSize) |
| 104 |
require.NoError(t, q.Shutdown(context.Background())) |
| 105 |
} |
| 106 |
|
| 107 |
func TestMemoryQueueRejectOverCapacityElements(t *testing.T) { |
| 108 |
set := newSettings(request.SizerTypeItems, 1) |
| 109 |
set.BlockOnOverflow = true |
| 110 |
q := newMemoryQueue[intRequest](set) |
| 111 |
require.NoError(t, q.Start(context.Background(), componenttest.NewNopHost())) |
| 112 |
require.ErrorIs(t, q.Offer(context.Background(), 8), errSizeTooLarge) |
| 113 |
require.NoError(t, q.Shutdown(context.Background())) |
| 114 |
} |
| 115 |
|
| 116 |
func TestMemoryQueueOfferZeroSize(t *testing.T) { |
| 117 |
set := newSettings(request.SizerTypeItems, 1) |
| 118 |
q := newMemoryQueue[intRequest](set) |
| 119 |
require.NoError(t, q.Start(context.Background(), componenttest.NewNopHost())) |
| 120 |
require.NoError(t, q.Offer(context.Background(), 0)) |
| 121 |
require.NoError(t, q.Shutdown(context.Background())) |
| 122 |
|
| 123 |
assert.False(t, consume(q, func(context.Context, intRequest) error { t.FailNow(); return nil })) |
| 124 |
} |
| 125 |
|
| 126 |
func TestMemoryQueueOverflow(t *testing.T) { |
| 127 |
set := newSettings(request.SizerTypeItems, 1) |
| 128 |
q := newMemoryQueue[intRequest](set) |
| 129 |
require.NoError(t, q.Start(context.Background(), componenttest.NewNopHost())) |
| 130 |
require.NoError(t, q.Offer(context.Background(), 1)) |
| 131 |
require.ErrorIs(t, q.Offer(context.Background(), 1), ErrQueueIsFull) |
| 132 |
require.NoError(t, q.Shutdown(context.Background())) |
| 133 |
} |
| 134 |
|
| 135 |
func TestMemoryQueueWaitForResultPassErrorBack(t *testing.T) { |
| 136 |
wg := sync.WaitGroup{} |
| 137 |
myErr := errors.New("test error") |
| 138 |
set := newSettings(request.SizerTypeItems, 100) |
| 139 |
set.WaitForResult = true |
| 140 |
q := newMemoryQueue[intRequest](set) |
| 141 |
require.NoError(t, q.Start(context.Background(), componenttest.NewNopHost())) |
| 142 |
wg.Add(1) |
| 143 |
go func() { |
| 144 |
defer wg.Done() |
| 145 |
_, req, done, ok := q.Read(context.Background()) |
| 146 |
assert.True(t, ok) |
| 147 |
assert.EqualValues(t, 1, req) |
| 148 |
done.OnDone(myErr) |
| 149 |
}() |
| 150 |
require.ErrorIs(t, q.Offer(context.Background(), intRequest(1)), myErr) |
| 151 |
require.NoError(t, q.Shutdown(context.Background())) |
| 152 |
wg.Wait() |
| 153 |
} |
| 154 |
|
| 155 |
func TestMemoryQueueWaitForResultCancelIncomingRequest(t *testing.T) { |
| 156 |
wg := sync.WaitGroup{} |
| 157 |
stop := make(chan struct{}) |
| 158 |
set := newSettings(request.SizerTypeItems, 100) |
| 159 |
set.WaitForResult = true |
| 160 |
q := newMemoryQueue[intRequest](set) |
| 161 |
require.NoError(t, q.Start(context.Background(), componenttest.NewNopHost())) |
| 162 |
|
| 163 |
|
| 164 |
wg.Add(1) |
| 165 |
go func() { |
| 166 |
defer wg.Done() |
| 167 |
_, _, done, ok := q.Read(context.Background()) |
| 168 |
assert.True(t, ok) |
| 169 |
<-stop |
| 170 |
done.OnDone(nil) |
| 171 |
}() |
| 172 |
|
| 173 |
ctx, cancel := context.WithCancel(context.Background()) |
| 174 |
wg.Add(1) |
| 175 |
go func() { |
| 176 |
defer wg.Done() |
| 177 |
<-time.After(time.Second) |
| 178 |
cancel() |
| 179 |
}() |
| 180 |
require.ErrorIs(t, q.Offer(ctx, intRequest(1)), context.Canceled) |
| 181 |
close(stop) |
| 182 |
require.NoError(t, q.Shutdown(context.Background())) |
| 183 |
wg.Wait() |
| 184 |
} |
| 185 |
|
| 186 |
func TestMemoryQueueWaitForResultSizeAndCapacity(t *testing.T) { |
| 187 |
wg := sync.WaitGroup{} |
| 188 |
stop := make(chan struct{}) |
| 189 |
set := newSettings(request.SizerTypeItems, 100) |
| 190 |
set.WaitForResult = true |
| 191 |
q := newMemoryQueue[intRequest](set) |
| 192 |
require.NoError(t, q.Start(context.Background(), componenttest.NewNopHost())) |
| 193 |
|
| 194 |
|
| 195 |
wg.Add(1) |
| 196 |
go func() { |
| 197 |
defer wg.Done() |
| 198 |
_, _, done, ok := q.Read(context.Background()) |
| 199 |
assert.True(t, ok) |
| 200 |
<-stop |
| 201 |
done.OnDone(nil) |
| 202 |
}() |
| 203 |
|
| 204 |
assert.EqualValues(t, 0, q.Size()) |
| 205 |
assert.EqualValues(t, 100, q.Capacity()) |
| 206 |
wg.Add(1) |
| 207 |
go func() { |
| 208 |
defer wg.Done() |
| 209 |
assert.NoError(t, q.Offer(context.Background(), intRequest(1))) |
| 210 |
}() |
| 211 |
assert.Eventually(t, func() bool { return q.Size() == 1 }, 1*time.Second, 10*time.Millisecond) |
| 212 |
assert.EqualValues(t, 100, q.Capacity()) |
| 213 |
close(stop) |
| 214 |
require.NoError(t, q.Shutdown(context.Background())) |
| 215 |
wg.Wait() |
| 216 |
} |
| 217 |
|
| 218 |
func BenchmarkMemoryQueueWaitForResult(b *testing.B) { |
| 219 |
wg := sync.WaitGroup{} |
| 220 |
consumed := &atomic.Int64{} |
| 221 |
set := newSettings(request.SizerTypeItems, 100) |
| 222 |
set.WaitForResult = true |
| 223 |
q := newMemoryQueue[intRequest](set) |
| 224 |
require.NoError(b, q.Start(context.Background(), componenttest.NewNopHost())) |
| 225 |
|
| 226 |
|
| 227 |
wg.Add(1) |
| 228 |
go func() { |
| 229 |
defer wg.Done() |
| 230 |
for { |
| 231 |
_, req, done, ok := q.Read(context.Background()) |
| 232 |
if !ok { |
| 233 |
return |
| 234 |
} |
| 235 |
consumed.Add(int64(req)) |
| 236 |
done.OnDone(nil) |
| 237 |
} |
| 238 |
}() |
| 239 |
|
| 240 |
b.ReportAllocs() |
| 241 |
for b.Loop() { |
| 242 |
for range 100 { |
| 243 |
require.NoError(b, q.Offer(context.Background(), intRequest(1))) |
| 244 |
} |
| 245 |
} |
| 246 |
require.NoError(b, q.Shutdown(context.Background())) |
| 247 |
assert.Equal(b, int64(b.N)*100, consumed.Load()) |
| 248 |
} |
| 249 |
|
| 250 |
func consume[T any](q readableQueue[T], consumeFunc func(context.Context, T) error) bool { |
| 251 |
ctx, req, done, ok := q.Read(context.Background()) |
| 252 |
if !ok { |
| 253 |
return false |
| 254 |
} |
| 255 |
done.OnDone(consumeFunc(ctx, req)) |
| 256 |
return true |
| 257 |
} |