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 result print, fail in case of missing cookies #4

Open
wants to merge 2 commits into
base: main
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
27 changes: 15 additions & 12 deletions src/main/java/org/example/tomcat/cloud/TestServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Cookie;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
Expand All @@ -44,26 +46,27 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro
response.setContentType("application/json;charset=UTF-8");

HttpSession session = request.getSession();
Integer counter;
if (session.isNew()) {
/* we have a new session */
counter = 0;
} else {
counter = (Integer) session.getAttribute("counter");
if (counter == null) {
counter = 1;
} else {
Integer counter;
if (session.isNew()) {
/* we have a new session */
counter = 0;
} else {
counter = (Integer) session.getAttribute("counter");
if (counter == null) {
counter = 0;
}
counter++;
}
session.setAttribute("counter", counter);
}
session.setAttribute("counter", counter);
}

String id = session.getId();
String isNew = session.isNew() ? "true" : "false";
String server = InetAddress.getLocalHost().getHostAddress();
String hostname = InetAddress.getLocalHost().getHostName();
String last = session.getLastAccessedTime() + "";

response.addCookie(new Cookie("JSESSIONID", id));

try (PrintWriter out = response.getWriter()) {
out.println(String.format(TEMPLATE, counter, id, isNew, server, hostname, last));
}
Expand Down
11 changes: 7 additions & 4 deletions test-demo-webapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
class testdemowebapp:

def __init__(self):
self.url = "http://localhost:8000/demo-1.0/demo"
self.counter = 0
self.url = "http://localhost:8000/demo-1.0/demo"
self.JSESSIONID = None

# Return False if sticky True otherwise
Expand All @@ -26,7 +25,7 @@ def request_response(self):
self.JSESSIONID = c.value
elif self.JSESSIONID != c.value:
return False
return True
return len(r.cookies) > 0

# Return True if sticky False if error or not sticky
def request_response_loop(self, n):
Expand All @@ -45,6 +44,7 @@ def start(result_queue, n, tid):
result_queue.put((tid, 'done' if mydemo.request_response_loop(n) else 'failed'))

if __name__ == "__main__":
res = "OK"
args = sys.argv[1:]
count = 1
loop = 1000
Expand All @@ -64,5 +64,8 @@ def start(result_queue, n, tid):
for i in range(count):
r = q.get()
if r[1] != 'done':
print("Failed")
res = "NOK"
break

print(res)