package lock import ( "context" "testing" "time" ) func TestTryAcquireLLM(t *testing.T) { lk := New(nil) if !lk.TryAcquireLLM() { t.Fatal("TryAcquireLLM on idle lock should succeed") } if _, n, _ := lk.Snapshot(); n != 1 { t.Fatalf("n = %d, want 1", n) } // While an image job is pending, TryAcquireLLM must fail. imageWaiting := make(chan struct{}) go func() { lk.AcquireImage(context.Background()) close(imageWaiting) }() deadline := time.Now().Add(2 * time.Second) for { lk.mu.Lock() queued := len(lk.imageQ) lk.mu.Unlock() if queued == 1 { break } if time.Now().After(deadline) { t.Fatal("image waiter never queued") } time.Sleep(time.Millisecond) } if lk.TryAcquireLLM() { t.Fatal("TryAcquireLLM with image pending should fail") } lk.ReleaseLLM() <-imageWaiting if lk.TryAcquireLLM() { t.Fatal("TryAcquireLLM with image active should fail") } lk.ReleaseImage() if !lk.TryAcquireLLM() { t.Fatal("TryAcquireLLM after image release should succeed") } lk.ReleaseLLM() }