|
| 1 | +package advisorylock |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "runtime" |
| 6 | + "sync" |
| 7 | + "sync/atomic" |
| 8 | + "time" |
| 9 | + |
| 10 | + . "github.com/onsi/ginkgo/v2" |
| 11 | + . "github.com/onsi/gomega" |
| 12 | + |
| 13 | + "github.com/mudler/LocalAI/core/services/testutil" |
| 14 | +) |
| 15 | + |
| 16 | +var _ = Describe("RunLeaderLoop", func() { |
| 17 | + Context("PostgreSQL leader election", func() { |
| 18 | + BeforeEach(func() { |
| 19 | + if runtime.GOOS == "darwin" { |
| 20 | + Skip("testcontainers requires Docker, not available on macOS CI") |
| 21 | + } |
| 22 | + }) |
| 23 | + |
| 24 | + It("executes function on tick", func() { |
| 25 | + db := testutil.SetupTestDB() |
| 26 | + const lockKey int64 = 5000 |
| 27 | + |
| 28 | + var callCount int32 |
| 29 | + ctx, cancel := context.WithCancel(context.Background()) |
| 30 | + defer cancel() |
| 31 | + |
| 32 | + go RunLeaderLoop(ctx, db, lockKey, 50*time.Millisecond, func() { |
| 33 | + atomic.AddInt32(&callCount, 1) |
| 34 | + }) |
| 35 | + |
| 36 | + Eventually(func() int32 { |
| 37 | + return atomic.LoadInt32(&callCount) |
| 38 | + }, 500*time.Millisecond, 10*time.Millisecond).Should(BeNumerically(">=", 1), |
| 39 | + "expected function to be called at least once") |
| 40 | + }) |
| 41 | + |
| 42 | + It("stops when context is cancelled", func() { |
| 43 | + db := testutil.SetupTestDB() |
| 44 | + const lockKey int64 = 5001 |
| 45 | + |
| 46 | + var callCount int32 |
| 47 | + ctx, cancel := context.WithCancel(context.Background()) |
| 48 | + |
| 49 | + done := make(chan struct{}) |
| 50 | + go func() { |
| 51 | + RunLeaderLoop(ctx, db, lockKey, 50*time.Millisecond, func() { |
| 52 | + atomic.AddInt32(&callCount, 1) |
| 53 | + }) |
| 54 | + close(done) |
| 55 | + }() |
| 56 | + |
| 57 | + // Let it run a bit then cancel |
| 58 | + time.Sleep(150 * time.Millisecond) |
| 59 | + cancel() |
| 60 | + |
| 61 | + // RunLeaderLoop should return |
| 62 | + Eventually(done, 500*time.Millisecond).Should(BeClosed()) |
| 63 | + |
| 64 | + // Record count after cancellation |
| 65 | + countAfterCancel := atomic.LoadInt32(&callCount) |
| 66 | + time.Sleep(150 * time.Millisecond) |
| 67 | + countLater := atomic.LoadInt32(&callCount) |
| 68 | + |
| 69 | + Expect(countLater).To(Equal(countAfterCancel), |
| 70 | + "function should stop being called after context cancellation") |
| 71 | + }) |
| 72 | + |
| 73 | + It("only one leader executes at a time (two concurrent loops)", func() { |
| 74 | + db := testutil.SetupTestDB() |
| 75 | + const lockKey int64 = 5002 |
| 76 | + |
| 77 | + var ( |
| 78 | + mu sync.Mutex |
| 79 | + maxRunning int32 |
| 80 | + running int32 |
| 81 | + ) |
| 82 | + |
| 83 | + ctx, cancel := context.WithCancel(context.Background()) |
| 84 | + defer cancel() |
| 85 | + |
| 86 | + fn := func() { |
| 87 | + cur := atomic.AddInt32(&running, 1) |
| 88 | + mu.Lock() |
| 89 | + if cur > maxRunning { |
| 90 | + maxRunning = cur |
| 91 | + } |
| 92 | + mu.Unlock() |
| 93 | + |
| 94 | + time.Sleep(30 * time.Millisecond) |
| 95 | + |
| 96 | + atomic.AddInt32(&running, -1) |
| 97 | + } |
| 98 | + |
| 99 | + // Start two competing leader loops with the same lock key |
| 100 | + go RunLeaderLoop(ctx, db, lockKey, 50*time.Millisecond, fn) |
| 101 | + go RunLeaderLoop(ctx, db, lockKey, 50*time.Millisecond, fn) |
| 102 | + |
| 103 | + // Let them run for a while |
| 104 | + time.Sleep(400 * time.Millisecond) |
| 105 | + cancel() |
| 106 | + |
| 107 | + mu.Lock() |
| 108 | + observed := maxRunning |
| 109 | + mu.Unlock() |
| 110 | + |
| 111 | + Expect(observed).To(BeNumerically("<=", 1), |
| 112 | + "expected at most 1 goroutine running the leader function at a time") |
| 113 | + }) |
| 114 | + }) |
| 115 | +}) |
0 commit comments