Skip to content

Commit

Permalink
Merge pull request #43 from CS3219-AY2425S1/ben/fix-matching-service
Browse files Browse the repository at this point in the history
fix: error with cleaning up user from global maps
  • Loading branch information
tituschewxj authored Oct 20, 2024
2 parents 4e27b86 + 5b15263 commit f8e0cda
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 46 deletions.
13 changes: 11 additions & 2 deletions apps/matching-service/handlers/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,17 @@ func waitForResult(ws *websocket.Conn, ctx, timeoutCtx, matchCtx context.Context
// Notify the user about the match
notifyMatches(result.User, result)

// NOTE: user and other user are already cleaned up in a separate matching algorithm process
// so no clean up is required here.
// cleaning up from the global maps used still required
if _, exists := matchContexts[username]; exists {
delete(matchContexts, username)
}
if _, exists := activeConnections[username]; exists {
delete(activeConnections, username)
}
if _, exists := matchFoundChannels[username]; exists {
delete(matchFoundChannels, username)
}

return
}
}
Expand Down
126 changes: 82 additions & 44 deletions apps/matching-service/tests/websocket-test.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,19 @@
<title>Matching service: websocket test</title>
</head>
<body>
<p id="status">Status: no matching yet</p>
<button onclick="openWebSocket()">Find match</button>
<button onclick="closeConnection()">Cancel matching</button>
<p id="response"></p>
<p id="status1">Status (Conn 1): no matching yet</p>
<button onclick="openWebSocket1()">Find match (Conn 1)</button>
<button onclick="closeConnection1()">Cancel matching (Conn 1)</button>
<p id="response1"></p>

<p id="status2">Status (Conn 2): no matching yet</p>
<button onclick="openWebSocket2()">Find match (Conn 2)</button>
<button onclick="closeConnection2()">Cancel matching (Conn 2)</button>
<p id="response2"></p>

<script>
let socket = null;
let socket1 = null;
let socket2 = null;

function generateRandomDetails() {
const username = Math.random().toString(36).substring(2, 8); // Generates a random username
Expand All @@ -20,63 +27,94 @@
return [username, `${username}@${domain}`];
}

function openWebSocket() {
// Connect to WebSocket server
// User should be authenticated first
// JWT token should be sent with the initial request, in the authorization header.
// request would be checked as middleware.
if (socket != null) return;
socket = new WebSocket("ws://localhost:8081/match");
document.getElementById("response").innerText = "";

// Log connection opening
socket.onopen = function () {
console.log("WebSocket is open now.");
function openWebSocket1() {
if (socket1 != null) return;
socket1 = new WebSocket("ws://localhost:8081/match");
document.getElementById("response1").innerText = "";

// Random email & username
socket1.onopen = function () {
console.log("WebSocket 1 is open now.");
const arr = generateRandomDetails();

// send the match request when connection is open
socket.send(
socket1.send(
JSON.stringify({
type: "match_request",
topics: ["Algorithms", "Arrays"],
difficulties: ["Easy", "Medium"],
// username: "JohnDoe", // Uncomment for same user test for rejection
username: arr[0],
username: "Timothy",
})
);
document.getElementById("status1").innerText =
"Status (Conn 1): matching in progress...";
};

socket1.onmessage = function (event) {
console.log("Message from Conn 1: " + event.data);
document.getElementById("response1").innerText =
"Received from Conn 1: " + event.data;
document.getElementById("status1").innerText =
"Status (Conn 1): matching found/timeout";
};

socket1.onerror = function (error) {
console.log("WebSocket 1 error: " + error);
};

socket1.onclose = function () {
console.log("WebSocket 1 is closed now.");
socket1 = null;
};
}

function closeConnection1() {
document.getElementById("status1").innerText =
"Status (Conn 1): matching cancelled";
document.getElementById("response1").innerText = "";
socket1.close();
}

function openWebSocket2() {
if (socket2 != null) return;
socket2 = new WebSocket("ws://localhost:8081/match");
document.getElementById("response2").innerText = "";

document.getElementById("status").innerText =
"Status: matching in progress...";
socket2.onopen = function () {
console.log("WebSocket 2 is open now.");
const arr = generateRandomDetails();
socket2.send(
JSON.stringify({
type: "match_request",
topics: ["Algorithms", "Graphs"],
difficulties: ["Medium", "Hard"],
username: "Jennie",
})
);
document.getElementById("status2").innerText =
"Status (Conn 2): matching in progress...";
};

// Log response message and display it
socket.onmessage = function (event) {
console.log("Message received: " + event.data);
document.getElementById("response").innerText =
"Received: " + event.data;
document.getElementById("status").innerText =
"Status: matching found/timeout";
socket2.onmessage = function (event) {
console.log("Message from Conn 2: " + event.data);
document.getElementById("response2").innerText =
"Received from Conn 2: " + event.data;
document.getElementById("status2").innerText =
"Status (Conn 2): matching found/timeout";
};

// Log errors
socket.onerror = function (error) {
console.log("WebSocket error: " + error);
socket2.onerror = function (error) {
console.log("WebSocket 2 error: " + error);
};

// Log connection closure
socket.onclose = function () {
console.log("WebSocket is closed now.");
socket = null;
socket2.onclose = function () {
console.log("WebSocket 2 is closed now.");
socket2 = null;
};
}

function closeConnection() {
document.getElementById("status").innerText =
"Status: matching cancelled";
document.getElementById("response").innerText = "";
socket.close();
function closeConnection2() {
document.getElementById("status2").innerText =
"Status (Conn 2): matching cancelled";
document.getElementById("response2").innerText = "";
socket2.close();
}
</script>
</body>
Expand Down

0 comments on commit f8e0cda

Please sign in to comment.