Skip to content
This repository was archived by the owner on Nov 12, 2025. It is now read-only.

Commit df922b6

Browse files
committed
Resolve bug which returns invalid background image occasionally
1 parent cb6a9f1 commit df922b6

3 files changed

Lines changed: 26 additions & 5 deletions

File tree

src/main/java/com/searchcode/app/service/route/CodeRouteService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public ModelAndView root(Request request, Response response) {
129129
return new ModelAndView(map, "search_ajax.ftl");
130130
}
131131

132-
map.put("photoId", CommonRouteService.getPhotoId());
132+
map.put("photoId", CommonRouteService.getPhotoId(Calendar.getInstance().get(Calendar.DAY_OF_YEAR)));
133133
if (request.queryParams().contains("photoId")) {
134134
map.put("photoId", request.queryParams("photoId"));
135135
}
@@ -445,7 +445,7 @@ public ModelAndView html(Request request, Response response) {
445445
return new ModelAndView(map, "searchresults.ftl");
446446
}
447447

448-
map.put("photoId", CommonRouteService.getPhotoId());
448+
map.put("photoId", CommonRouteService.getPhotoId(Calendar.getInstance().get(Calendar.DAY_OF_YEAR)));
449449
map.put("numDocs", this.indexService.getCodeIndexLinesCount());
450450
map.put("logoImage", CommonRouteService.getLogo());
451451
map.put("isCommunity", App.ISCOMMUNITY);

src/main/java/com/searchcode/app/service/route/CommonRouteService.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ public static String getSyntaxHighlighter() {
151151
return highlighter;
152152
}
153153

154-
public static int getPhotoId() {
155-
Random random = new Random(Calendar.getInstance().get(Calendar.DAY_OF_YEAR));
156-
return random.nextInt((42 - 1) + 1);
154+
public static int getPhotoId(int seed) {
155+
Random random = new Random();
156+
return random.nextInt(42) + 1;
157157
}
158158
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.searchcode.app.service.route;
2+
3+
import junit.framework.TestCase;
4+
5+
import java.util.Calendar;
6+
7+
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
8+
9+
10+
public class CommonRouteServiceTest extends TestCase {
11+
public void testGetPhotoId() {
12+
13+
int photoId = CommonRouteService.getPhotoId(Calendar.getInstance().get(Calendar.DAY_OF_YEAR));
14+
assertThat(photoId).isBetween(1, 42);
15+
16+
for (int i = 0; i < 10000; i++) {
17+
photoId = CommonRouteService.getPhotoId(i);
18+
assertThat(photoId).isBetween(1, 42);
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)