github.com/weaviate/weaviate@v1.24.6/adapters/repos/db/backup_test.go (about) 1 // _ _ 2 // __ _____ __ ___ ___ __ _| |_ ___ 3 // \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \ 4 // \ V V / __/ (_| |\ V /| | (_| | || __/ 5 // \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___| 6 // 7 // Copyright © 2016 - 2024 Weaviate B.V. All rights reserved. 8 // 9 // CONTACT: hello@weaviate.io 10 // 11 12 package db 13 14 import ( 15 "context" 16 "errors" 17 "testing" 18 "time" 19 20 tlog "github.com/sirupsen/logrus/hooks/test" 21 ) 22 23 func TestBackupMutex(t *testing.T) { 24 l, _ := tlog.NewNullLogger() 25 t.Run("success first time", func(t *testing.T) { 26 m := backupMutex{log: l, retryDuration: time.Millisecond, notifyDuration: 5 * time.Millisecond} 27 ctx, cancel := context.WithTimeout(context.Background(), 12*time.Millisecond) 28 defer cancel() 29 if err := m.LockWithContext(ctx); err != nil { 30 t.Errorf("error want:nil got:%v ", err) 31 } 32 }) 33 t.Run("success after retry", func(t *testing.T) { 34 m := backupMutex{log: l, retryDuration: 2 * time.Millisecond, notifyDuration: 5 * time.Millisecond} 35 m.RLock() 36 go func() { 37 defer m.RUnlock() 38 time.Sleep(time.Millisecond * 15) 39 }() 40 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) 41 defer cancel() 42 if err := m.LockWithContext(ctx); err != nil { 43 t.Errorf("error want:nil got:%v ", err) 44 } 45 }) 46 t.Run("cancelled context", func(t *testing.T) { 47 m := backupMutex{log: l, retryDuration: time.Millisecond, notifyDuration: 5 * time.Millisecond} 48 m.RLock() 49 defer m.RUnlock() 50 ctx, cancel := context.WithTimeout(context.Background(), 12*time.Millisecond) 51 defer cancel() 52 err := m.LockWithContext(ctx) 53 if !errors.Is(err, context.DeadlineExceeded) { 54 t.Errorf("error want:%v got:%v", err, context.DeadlineExceeded) 55 } 56 }) 57 }