Debian Codegraph @

paho/session/state/sendquota_test.go

raw
1 /*
2 * Copyright (c) 2024 Contributors to the Eclipse Foundation
3 *
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v2.0
6 * and Eclipse Distribution License v1.0 which accompany this distribution.
7 *
8 * The Eclipse Public License is available at
9 * https://www.eclipse.org/legal/epl-2.0/
10 * and the Eclipse Distribution License is available at
11 * http://www.eclipse.org/org/documents/edl-v10.php.
12 *
13 * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
14 */
15
16 package state
17
18 import (
19 "context"
20 "errors"
21 "math/rand"
22 "sync"
23 "sync/atomic"
24 "testing"
25 "time"
26 )
27
28 // TestQuotaBasics performs basic testing of allocating/releasing message ids in inflight
29 func TestQuotaBasics(t *testing.T) {
30 t.Parallel()
31
32 it := newSendQuota(5)
33
34 // We should be able to acquire until the quota is hit
35 ctx, cancel := context.WithTimeout(context.Background(), time.Second)
36 for i := uint32(0); i < 5; i++ {
37 if err := it.Acquire(ctx); err != nil {
38 t.Fatalf("failed to get slot: %v", err)
39 }
40 }
41 cancel()
42
43 // subsequent waiters should block until Release is called
44 for i := uint32(1); i < 5; i++ {
45 release := make(chan struct{})
46 released := make(chan error, 1)
47 ctx, cancel = context.WithTimeout(context.Background(), 20*time.Millisecond)
48 go func() {
49 <-release
50 it.Release()
51 close(released)
52 }()
53
54 aResult := make(chan error, 1)
55 go func() {
56 aResult <- it.Acquire(ctx)
57 }()
58 select {
59 case err := <-aResult:
60 t.Fatalf("slot should not be gained before previous one released: %d, %v", i, err)
61 case <-time.After(10 * time.Millisecond):
62 }
63
64 close(release)
65 select {
66 case <-released:
67 case <-time.After(10 * time.Millisecond):
68 t.Fatalf("slot should have been released")
69 }
70 cancel()
71 }
72
73 // Waiters should be released in order
74 ctx, cancel = context.WithTimeout(context.Background(), 20*time.Millisecond)
75 aResult := make(chan struct {
76 uint32
77 error
78 })
79 for i := uint32(0); i < 5; i++ {
80 go func(i uint32) {
81 aResult <- struct {
82 uint32
83 error
84 }{i, it.Acquire(ctx)}
85 }(i)
86 time.Sleep(time.Millisecond) // we want slots to be requested in known order
87 }
88 select {
89 case result := <-aResult:
90 t.Fatalf("await should not complete before slot is available: %v", result.error)
91 default:
92 }
93 for i := uint32(0); i < 5; i++ {
94 it.Release()
95 time.Sleep(time.Millisecond) // provide time for release to be processed
96 }
97 time.Sleep(time.Millisecond) // allow time for slots to be allocated
98 for i := uint32(0); i < 5; i++ {
99 select {
100 case result := <-aResult:
101 if result.error != nil {
102 t.Fatalf("unexpected error: %v", result.error)
103 }
104 if result.uint32 != i {
105 t.Fatalf("release out of order; expected %d, got %d", i, result.uint32)
106 }
107 default:
108 t.Fatalf("await should have compelted")
109 }
110 }
111
112 // Check internal state
113 if it.quota != 0 {
114 t.Fatalf("expected 0 quota: %v", it.quota)
115 }
116 if len(it.waiters) != 0 {
117 t.Fatalf("expected waiters to be empty: %v", it.waiters)
118 }
119
120 // Empty waiters
121 for i := uint32(0); i < 5; i++ {
122 it.Release()
123 }
124 // Check internal state
125 if it.quota != 5 {
126 t.Fatalf("expected 5 quota: %v", it.quota)
127 }
128 if len(it.waiters) != 0 {
129 t.Fatalf("expected waiters to be empty: %v", it.waiters)
130 }
131
132 // Releasing additional slots should have no impact
133 it.Release()
134 if it.quota != 5 {
135 t.Fatalf("expected 5 quota: %v", it.quota)
136 }
137 }
138
139 // TestQuotaContext checks that we can cancel waiters
140 func TestQuotaContext(t *testing.T) {
141 t.Parallel()
142
143 it := newSendQuota(5)
144
145 ctx, cancel := context.WithCancel(context.Background())
146 cancel()
147
148 // Even with cancelled context Await should return a slot if it's free
149 for i := uint32(0); i < 5; i++ {
150 if err := it.Acquire(ctx); err != nil {
151 t.Fatalf("failed to get slot: %v", err)
152 }
153 }
154 // We should not wait if there is not a free slot
155 if err := it.Acquire(ctx); !errors.Is(err, context.Canceled) {
156 t.Fatalf("should fail if context cancelled: %v", err)
157 }
158
159 // Slots freed due to the context expiring should be removed
160 // from `waiters` meaning that if we free ons slot we should
161 // be able to acquire another
162 if err := it.Release(); err != nil {
163 t.Fatalf("failed to release slot: %v", err)
164 }
165
166 if err := it.Acquire(ctx); err != nil {
167 t.Fatalf("failed to get slot after one was released: %v", err)
168 }
169 }
170
171 // TestQuotaLoad tests send quota under load
172 func TestQuotaLoad(t *testing.T) {
173 t.Parallel()
174
175 const receiveMax = 20
176 const messageCount = 20000
177 var inFlight atomic.Int64
178
179 it := newSendQuota(receiveMax)
180 ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
181 defer cancel()
182 countErr := make(chan int64)
183 awaitErr := make(chan error)
184
185 var wg sync.WaitGroup
186 wg.Add(messageCount)
187 for i := uint32(0); i < messageCount; i++ {
188 go func(messageID uint32) {
189 defer wg.Done()
190 if err := it.Acquire(ctx); err != nil {
191 awaitErr <- err
192 }
193 curr := inFlight.Add(1)
194 if curr > receiveMax {
195 countErr <- curr
196 }
197 time.Sleep(time.Duration(rand.Intn(100000)))
198 inFlight.Add(-1)
199 it.Release()
200 }(i)
201 }
202
203 done := make(chan struct{})
204 go func() {
205 wg.Wait()
206 close(done)
207 }()
208
209 for {
210 select {
211 case cnt := <-countErr:
212 t.Fatalf("messages in flight exceeded receive max: %d", cnt)
213 case err := <-awaitErr:
214 t.Fatalf("error awaiting: %v", err)
215 case <-done:
216 return // all good
217 }
218 }
219
220 // Check internal state
221 if it.quota != receiveMax {
222 t.Fatalf("expected quota of %d, got %d", receiveMax, it.quota)
223 }
224 if len(it.waiters) != 0 {
225 t.Fatalf("expected waiters to be empty: %v", it.waiters)
226 }
227 }