11package encryption
22
33import (
4- "context"
5- "errors"
64 "sync"
75 "testing"
86 "time"
@@ -192,147 +190,6 @@ func TestDEKCache_DeleteMissing(t *testing.T) {
192190 c .delete ("nonexistent" )
193191}
194192
195- func TestDEKCache_Singleflight (t * testing.T ) {
196- c := newDEKCache (10 , time .Minute )
197-
198- dek := []byte ("0123456789abcdef0123456789abcdef" )
199-
200- // First caller starts the fetch.
201- started , _ := c .waitOrStart ("ref1" )
202- if ! started {
203- t .Fatal ("first caller should start" )
204- }
205-
206- // Second and third callers should wait.
207- var wg sync.WaitGroup
208- results := make ([][]byte , 2 )
209- errs := make ([]error , 2 )
210-
211- for i := 0 ; i < 2 ; i ++ {
212- wg .Add (1 )
213- go func (idx int ) {
214- defer wg .Done ()
215- s , wait := c .waitOrStart ("ref1" )
216- if s {
217- t .Error ("subsequent caller should not start" )
218- return
219- }
220- if errs [idx ] = wait (context .Background ()); errs [idx ] != nil {
221- return
222- }
223- results [idx ], _ = c .get ("ref1" )
224- }(i )
225- }
226-
227- // Simulate the winner completing: populate the cache, then release waiters.
228- time .Sleep (10 * time .Millisecond ) // let goroutines reach wait()
229- c .put ("ref1" , dek )
230- c .finish ("ref1" , nil )
231-
232- wg .Wait ()
233-
234- for i := 0 ; i < 2 ; i ++ {
235- if errs [i ] != nil {
236- t .Fatalf ("waiter %d got error: %v" , i , errs [i ])
237- }
238- if string (results [i ]) != string (dek ) {
239- t .Fatalf ("waiter %d got wrong dek" , i )
240- }
241- }
242-
243- // Each waiter should have received an independent copy.
244- results [0 ][0 ] = 0xFF
245- if results [1 ][0 ] == 0xFF {
246- t .Fatal ("waiters should receive independent copies" )
247- }
248- }
249-
250- func TestDEKCache_SingleflightContextCancel (t * testing.T ) {
251- c := newDEKCache (10 , time .Minute )
252-
253- started , _ := c .waitOrStart ("ref1" )
254- if ! started {
255- t .Fatal ("first caller should start" )
256- }
257-
258- ctx , cancel := context .WithCancel (context .Background ())
259- done := make (chan error , 1 )
260- go func () {
261- _ , wait := c .waitOrStart ("ref1" )
262- done <- wait (ctx )
263- }()
264-
265- time .Sleep (10 * time .Millisecond )
266- cancel ()
267-
268- select {
269- case err := <- done :
270- if ! errors .Is (err , context .Canceled ) {
271- t .Fatalf ("got %v, want context.Canceled" , err )
272- }
273- case <- time .After (time .Second ):
274- t .Fatal ("waiter ignored context cancellation" )
275- }
276-
277- // The starter can still finish without affecting the cancelled waiter.
278- c .finish ("ref1" , nil )
279- }
280-
281- func TestDEKCache_SingleflightError (t * testing.T ) {
282- c := newDEKCache (10 , time .Minute )
283- fetchErr := & testError {msg : "kms failed" }
284-
285- started , _ := c .waitOrStart ("ref1" )
286- if ! started {
287- t .Fatal ("first caller should start" )
288- }
289-
290- var wg sync.WaitGroup
291- waiterErrs := make ([]error , 2 )
292-
293- for i := 0 ; i < 2 ; i ++ {
294- wg .Add (1 )
295- go func (idx int ) {
296- defer wg .Done ()
297- _ , wait := c .waitOrStart ("ref1" )
298- waiterErrs [idx ] = wait (context .Background ())
299- }(i )
300- }
301-
302- time .Sleep (10 * time .Millisecond )
303- c .finish ("ref1" , fetchErr )
304-
305- wg .Wait ()
306-
307- for i := 0 ; i < 2 ; i ++ {
308- if waiterErrs [i ] == nil {
309- t .Fatalf ("waiter %d should have received error" , i )
310- }
311- if waiterErrs [i ].Error () != "kms failed" {
312- t .Fatalf ("waiter %d got error %q, want %q" , i , waiterErrs [i ].Error (), "kms failed" )
313- }
314- }
315- }
316-
317- func TestDEKCache_SingleflightIndependentKeys (t * testing.T ) {
318- c := newDEKCache (10 , time .Minute )
319-
320- // Start fetch for ref1.
321- started1 , _ := c .waitOrStart ("ref1" )
322- if ! started1 {
323- t .Fatal ("first caller for ref1 should start" )
324- }
325-
326- // Start fetch for ref2 — should NOT be blocked by ref1.
327- started2 , _ := c .waitOrStart ("ref2" )
328- if ! started2 {
329- t .Fatal ("first caller for ref2 should start independently" )
330- }
331-
332- c .finish ("ref1" , nil )
333- c .finish ("ref2" , nil )
334- }
335-
336193func TestDEKCache_ConcurrentAccess (t * testing.T ) {
337194 c := newDEKCache (10 , time .Minute )
338195 dek := []byte ("0123456789abcdef0123456789abcdef" )
@@ -357,9 +214,3 @@ func TestDEKCache_ConcurrentAccess(t *testing.T) {
357214 }
358215 wg .Wait ()
359216}
360-
361- type testError struct {
362- msg string
363- }
364-
365- func (e * testError ) Error () string { return e .msg }
0 commit comments