Skip to content

Commit fca409f

Browse files
committed
Add Expiration Task state test (should be waiting at the start)
1 parent a8fb4b2 commit fca409f

File tree

2 files changed

+86
-3
lines changed
  • server-session/src

2 files changed

+86
-3
lines changed

server-session/src/main/java/com/iluwatar/sessionserver/App.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ public class App {
5959
private static Map<String, Integer> sessions = new HashMap<>();
6060
private static Map<String, Instant> sessionCreationTimes = new HashMap<>();
6161
private static final long SESSION_EXPIRATION_TIME = 10000;
62-
private static final Object sessionExpirationWait = new Object(); // used to make expiration task wait or work based on event (login request sent or not)
62+
private static Object sessionExpirationWait = new Object(); // used to make expiration task wait or work based on event (login request sent or not)
63+
private static Thread sessionExpirationThread;
6364

6465
/**
6566
* Main entry point.
@@ -84,7 +85,7 @@ public static void main(String[] args) throws IOException {
8485
}
8586

8687
private static void sessionExpirationTask() {
87-
new Thread(() -> {
88+
sessionExpirationThread= new Thread(() -> {
8889
while (true) {
8990
try {
9091
synchronized (sessions) {
@@ -117,7 +118,8 @@ private static void sessionExpirationTask() {
117118
Thread.currentThread().interrupt();
118119
}
119120
}
120-
}).start();
121+
});
122+
sessionExpirationThread.start();
121123
}
122124

123125
/**
@@ -129,4 +131,8 @@ public static void expirationTaskWake() {
129131
}
130132
}
131133

134+
public static Thread.State getExpirationTaskState(){
135+
return sessionExpirationThread.getState();
136+
}
137+
132138
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
3+
*
4+
* The MIT License
5+
* Copyright © 2014-2022 Ilkka Seppälä
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
package com.iluwatar.sessionserver;
26+
27+
import static java.lang.Thread.State;
28+
import static java.lang.Thread.State.WAITING;
29+
import static org.junit.jupiter.api.Assertions.assertEquals;
30+
import static org.mockito.Mockito.when;
31+
32+
import com.sun.net.httpserver.Headers;
33+
import com.sun.net.httpserver.HttpExchange;
34+
import java.io.ByteArrayOutputStream;
35+
import java.io.IOException;
36+
import java.time.Instant;
37+
import java.util.HashMap;
38+
import java.util.Map;
39+
import lombok.extern.slf4j.Slf4j;
40+
import org.junit.jupiter.api.BeforeEach;
41+
import org.junit.jupiter.api.Test;
42+
import org.mockito.Mock;
43+
import org.mockito.MockitoAnnotations;
44+
45+
/**
46+
* LoginHandlerTest.
47+
*/
48+
@Slf4j
49+
public class AppTest {
50+
51+
private LoginHandler loginHandler;
52+
//private Headers headers;
53+
private Map<String, Integer> sessions;
54+
private Map<String, Instant> sessionCreationTimes;
55+
56+
@Mock
57+
private HttpExchange exchange;
58+
59+
/**
60+
* Setup tests.
61+
*/
62+
@BeforeEach
63+
public void setUp() throws IOException {
64+
MockitoAnnotations.initMocks(this);
65+
App.main(new String [] {});
66+
}
67+
68+
@Test
69+
public void expirationTaskStartStateTest() {
70+
71+
//assert
72+
LOGGER.info("Expiration Task Status: "+String.valueOf(App.getExpirationTaskState()));
73+
assertEquals(App.getExpirationTaskState(),WAITING);
74+
75+
}
76+
77+
}

0 commit comments

Comments
 (0)