Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cleanup of ThreadLocal for reusable threads from the pool. #1582

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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 @@ -101,4 +101,9 @@ public String getName() {
return (String) claims.get(NAME);
}
}

public static void clear() {
currentRequest.remove();
currentUserUri.remove();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.fairspace.saturn.auth;

import java.io.IOException;

import jakarta.servlet.*;

public class RequestContextCleanupFilter implements Filter {

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
try {
chain.doFilter(request, response);
} finally {
RequestContext.clear();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.fairspace.saturn.auth;

import lombok.RequiredArgsConstructor;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.Customizer;
Expand Down Expand Up @@ -36,4 +37,12 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti

return http.build();
}

@Bean
public FilterRegistrationBean<RequestContextCleanupFilter> threadLocalCleanupFilter() {
FilterRegistrationBean<RequestContextCleanupFilter> registrationBean = new FilterRegistrationBean<>();
registrationBean.setFilter(new RequestContextCleanupFilter());
registrationBean.addUrlPatterns("/*");
return registrationBean;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package io.fairspace.saturn.auth;

import java.util.Optional;

import org.apache.jena.graph.Node;
import org.junit.Test;
import org.springframework.security.core.context.SecurityContextHolder;

import io.fairspace.saturn.TestUtils;

import static org.junit.Assert.*;

public class RequestContextTest {

@Test
public void getCurrentUserStringUri_shouldReturnUserUri() {
String userUri = "http://example.com/user";
RequestContext.setCurrentUserStringUri(userUri);
assertEquals(Optional.of(userUri), RequestContext.getCurrentUserStringUri());
}

@Test
public void getCurrentUserStringUri_shouldReturnEmptyWhenNoUriSet() {
RequestContext.clear();
assertEquals(Optional.empty(), RequestContext.getCurrentUserStringUri());
}

@Test
public void getUserURI_shouldReturnUserUriFromJwt() {
TestUtils.mockAuthentication("testUser");
Node userUri = RequestContext.getUserURI();
assertNotNull(userUri);
}

@Test
public void getUserURI_shouldReturnNullWhenNoJwt() {
SecurityContextHolder.clearContext();
RequestContext.clear();
assertNull(RequestContext.getUserURI());
}

@Test
public void getClaims_shouldReturnClaimsFromJwt() {
TestUtils.mockAuthentication("testUser");
RequestContext.SaturnClaims claims = RequestContext.getClaims();
assertEquals("testUser", claims.getSubject());
}

@Test
public void getClaims_shouldReturnEmptyClaimsWhenNoJwt() {
SecurityContextHolder.clearContext();
RequestContext.SaturnClaims claims = RequestContext.getClaims();
assertNull(claims.getSubject());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import org.springframework.boot.autoconfigure.security.oauth2.resource.servlet.OAuth2ResourceServerAutoConfiguration;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.bean.override.mockito.MockitoBean;

import io.fairspace.saturn.auth.JwtAuthConverterProperties;
import io.fairspace.saturn.services.IRIModule;
Expand All @@ -16,7 +16,7 @@
@Import(BaseControllerTest.CustomObjectMapperConfig.class)
public class BaseControllerTest {

@MockBean
@MockitoBean
private JwtAuthConverterProperties jwtAuthConverterProperties;

@TestConfiguration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.test.web.servlet.MockMvc;

import io.fairspace.saturn.config.enums.Feature;
Expand All @@ -23,7 +23,7 @@ class FeaturesControllerTest extends BaseControllerTest {
@Autowired
private MockMvc mockMvc;

@MockBean
@MockitoBean
private FeatureProperties featureProperties;

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.test.web.servlet.MockMvc;

import io.fairspace.saturn.services.maintenance.MaintenanceService;

import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
Expand All @@ -23,7 +21,7 @@ class MaintenanceControllerTest extends BaseControllerTest {
@Autowired
private MockMvc mockMvc;

@MockBean
@MockitoBean
private MaintenanceService maintenanceService;

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.test.web.servlet.MockMvc;

import io.fairspace.saturn.services.metadata.MetadataService;
Expand All @@ -16,21 +16,16 @@
import static org.hamcrest.Matchers.containsString;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

@WebMvcTest(MetadataController.class)
public class MetadataControllerTest extends BaseControllerTest {

@Autowired
private MockMvc mockMvc;

@MockBean
@MockitoBean
private MetadataService metadataService;

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.test.web.servlet.MockMvc;

import io.fairspace.saturn.controller.dto.SearchResultDto;
Expand All @@ -28,10 +28,10 @@ class SearchControllerTest extends BaseControllerTest {
@Autowired
private MockMvc mockMvc;

@MockBean
@MockitoBean
private SearchService searchService;

@MockBean
@MockitoBean
private FileSearchService fileSearchService;

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.test.web.servlet.MockMvc;

import io.fairspace.saturn.services.users.User;
Expand All @@ -26,7 +26,7 @@ class UserControllerTest extends BaseControllerTest {
@Autowired
private MockMvc mockMvc;

@MockBean
@MockitoBean
private UserService service;

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.test.web.servlet.MockMvc;

import io.fairspace.saturn.config.properties.ViewsProperties;
Expand Down Expand Up @@ -37,10 +37,10 @@ public class ViewControllerTest extends BaseControllerTest {
@Autowired
private MockMvc mockMvc;

@MockBean
@MockitoBean
private ViewService viewService;

@MockBean(name = "queryService")
@MockitoBean(name = "queryService")
private QueryService queryService;

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.test.web.servlet.MockMvc;

import io.fairspace.saturn.services.workspaces.Workspace;
Expand All @@ -36,7 +36,7 @@ class WorkspaceControllerTest extends BaseControllerTest {
@Autowired
private MockMvc mockMvc;

@MockBean
@MockitoBean
private WorkspaceService workspaceService;

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.test.web.servlet.MockMvc;

import io.fairspace.saturn.controller.BaseControllerTest;
Expand All @@ -29,7 +29,7 @@ public class GlobalExceptionHandlerTest extends BaseControllerTest {
@Autowired
private MockMvc mockMvc;

@MockBean
@MockitoBean
private TestController.TestInnerClass testInnerClass;

@Test
Expand Down
Loading