diff --git a/src/main/java/com/lion/lionwebsite/Util/GalleryUtil.java b/src/main/java/com/lion/lionwebsite/Util/GalleryUtil.java index 1ac7fef..09e2e21 100644 --- a/src/main/java/com/lion/lionwebsite/Util/GalleryUtil.java +++ b/src/main/java/com/lion/lionwebsite/Util/GalleryUtil.java @@ -194,10 +194,13 @@ public class GalleryUtil { public static String getMpvKey(String url){ String gid = String.valueOf(parseGid(url)); - return gid2MpvKey.computeIfAbsent(gid, k -> { + String key = gid2MpvKey.get(gid); + if (key == null) { + // refreshMpvKey writes the cache itself; never call it inside computeIfAbsent. refreshMpvKey(url); - return gid2MpvKey.get(k); - }); + key = gid2MpvKey.get(gid); + } + return key; } public static void refreshMpvKey(String url) { @@ -210,7 +213,7 @@ public class GalleryUtil { content = requests(mpvUrl, "get", header, null); }catch (Exception e){ log.error("刷新mpvKey失败, url: {}", url, e); - gid2MpvKey.put(parseGid(url) + "", null); + gid2MpvKey.remove(parseGid(url) + ""); return; } Document document = Jsoup.parse(content); @@ -343,22 +346,21 @@ public class GalleryUtil { } httpResponse = httpClient.execute(httpPost); } - HttpEntity responseEntity = httpResponse.getEntity(); - int statusCode = httpResponse.getStatusLine().getStatusCode(); - StringBuilder stringBuilder = new StringBuilder(); - - if(statusCode == 200){ - BufferedReader reader = new BufferedReader(new InputStreamReader(responseEntity.getContent())); - String str; - while((str = reader.readLine()) != null) - stringBuilder.append(str).append("\n"); - } else{ - log.warn("{}:{}", url, statusCode); + try (httpResponse) { + HttpEntity responseEntity = httpResponse.getEntity(); + int statusCode = httpResponse.getStatusLine().getStatusCode(); + StringBuilder stringBuilder = new StringBuilder(); + if (statusCode == 200 && responseEntity != null) { + try (BufferedReader reader = new BufferedReader(new InputStreamReader(responseEntity.getContent()))) { + String str; + while ((str = reader.readLine()) != null) + stringBuilder.append(str).append("\n"); + } + } else { + log.warn("{}:{}", url, statusCode); + } + return stringBuilder.toString(); } - - httpResponse.close(); - - return stringBuilder.toString(); } public static Integer parseGid(String link){ diff --git a/src/test/java/com/lion/lionwebsite/Util/GalleryKeyCacheTest.java b/src/test/java/com/lion/lionwebsite/Util/GalleryKeyCacheTest.java new file mode 100644 index 0000000..9d0a2f7 --- /dev/null +++ b/src/test/java/com/lion/lionwebsite/Util/GalleryKeyCacheTest.java @@ -0,0 +1,38 @@ +package com.lion.lionwebsite.Util; + +import org.junit.jupiter.api.Test; +import java.io.IOException; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; +import static org.mockito.ArgumentMatchers.*; + +class GalleryKeyCacheTest { + @Test void coldCacheAllowsRefreshToPopulateIt() { + String url = "https://example.org/g/987654321/key/"; + GalleryUtil.gid2MpvKey.remove("987654321"); + try (var methods = mockStatic(GalleryUtil.class)) { + methods.when(() -> GalleryUtil.parseGid(url)).thenReturn(987654321); + methods.when(() -> GalleryUtil.getMpvKey(url)).thenCallRealMethod(); + methods.when(() -> GalleryUtil.refreshMpvKey(url)).thenAnswer(call -> { + GalleryUtil.gid2MpvKey.put("987654321", "cached-key"); + return null; + }); + assertEquals("cached-key", GalleryUtil.getMpvKey(url)); + assertEquals("cached-key", GalleryUtil.getMpvKey(url)); + methods.verify(() -> GalleryUtil.refreshMpvKey(url), times(1)); + } finally { GalleryUtil.gid2MpvKey.remove("987654321"); } + } + + @Test void failedRefreshRemovesOldKeyWithoutInsertingNull() { + String url = "https://example.org/g/987654321/key/"; + GalleryUtil.gid2MpvKey.put("987654321", "old-key"); + try (var methods = mockStatic(GalleryUtil.class)) { + methods.when(() -> GalleryUtil.parseGid(url)).thenReturn(987654321); + methods.when(() -> GalleryUtil.refreshMpvKey(url)).thenCallRealMethod(); + methods.when(() -> GalleryUtil.requests(anyString(), anyString(), any(), any())) + .thenThrow(new IOException("test failure")); + assertDoesNotThrow(() -> GalleryUtil.refreshMpvKey(url)); + assertFalse(GalleryUtil.gid2MpvKey.containsKey("987654321")); + } finally { GalleryUtil.gid2MpvKey.remove("987654321"); } + } +}