Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ public void acquireAndKeepLock(String resource, String lockContext) throws Alrea
}

private void scheduleLock(Lock newLock) {
if (lock != null) {
throw new IllegalStateException("Unable to acquire new lock, already holding lock that would get lost: " + lock);
}
Copy link

Copilot AI Jan 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new lock validation logic should be covered by a test case that verifies the IllegalStateException is thrown when attempting to schedule a lock while already holding one.

Copilot uses AI. Check for mistakes.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot: Please implement a test like that.

lock = newLock;
Duration duration = lockTTL.minusSeconds(30);
if (duration.isNegative() || duration.isZero()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* Copyright (C) 2020 Graylog, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*/
package org.graylog2.cluster.lock;

import org.graylog2.shared.SuppressForbidden;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.time.Duration;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Optional;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
public class RefreshingLockServiceTest {

@Mock
private LockService lockService;

private ScheduledExecutorService scheduler;

private RefreshingLockService refreshingLockService;

private final Lock firstLock = Lock.builder()
.resource("first-resource")
.lockedBy("node-123")
.createdAt(ZonedDateTime.now(ZoneOffset.UTC))
.updatedAt(ZonedDateTime.now(ZoneOffset.UTC))
.build();

private final Lock secondLock = Lock.builder()
.resource("second-resource")
.lockedBy("node-123")
.createdAt(ZonedDateTime.now(ZoneOffset.UTC))
.updatedAt(ZonedDateTime.now(ZoneOffset.UTC))
.build();

@BeforeEach
@SuppressForbidden("Using Executors.newSingleThreadScheduledExecutor() is okay in tests")
void setUp() {
scheduler = Executors.newSingleThreadScheduledExecutor();
refreshingLockService = new RefreshingLockService(
lockService,
scheduler,
Duration.ofMinutes(5)
);
}

@AfterEach
void tearDown() {
if (scheduler != null) {
scheduler.shutdownNow();
}
}

@Test
void throwsIllegalStateExceptionWhenAcquiringLockWhileAlreadyHoldingOne() throws AlreadyLockedException {
// Mock the lock service to return locks
when(lockService.lock(eq("first-resource"), anyString()))
.thenReturn(Optional.of(firstLock));
when(lockService.lock(eq("second-resource"), anyString()))
.thenReturn(Optional.of(secondLock));

// Acquire first lock successfully
refreshingLockService.acquireAndKeepLock("first-resource", "context-1");

// Attempt to acquire second lock while still holding the first
assertThatThrownBy(() -> refreshingLockService.acquireAndKeepLock("second-resource", "context-2"))
.isInstanceOf(IllegalStateException.class)
.hasMessageContaining("Unable to acquire new lock, already holding lock that would get lost");
}

@Test
void throwsIllegalStateExceptionWhenAcquiringLockWithMaxConcurrencyWhileAlreadyHoldingOne() throws AlreadyLockedException {
// Mock the lock service to return locks
when(lockService.lock(eq("first-resource"), eq(1)))
.thenReturn(Optional.of(firstLock));
when(lockService.lock(eq("second-resource"), eq(1)))
.thenReturn(Optional.of(secondLock));

// Acquire first lock successfully
refreshingLockService.acquireAndKeepLock("first-resource", 1);

// Attempt to acquire second lock while still holding the first
assertThatThrownBy(() -> refreshingLockService.acquireAndKeepLock("second-resource", 1))
.isInstanceOf(IllegalStateException.class)
.hasMessageContaining("Unable to acquire new lock, already holding lock that would get lost");
}
}
Loading