Skip to content

Commit

Permalink
fix race conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
16BitNarwhal committed Aug 26, 2024
1 parent 3e8669f commit 2117ba3
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 16 deletions.
7 changes: 7 additions & 0 deletions api-server/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ def get_chat_history(meeting_id):
else:
return jsonify({'error': 'Meeting ID not found'}), 404

@app.route('/api/session/<meeting_id>', methods=['GET'])
def get_users(meeting_id):
if meeting_id in session_storage:
return jsonify(session_storage[meeting_id])
else:
return jsonify({'error': 'Meeting ID not found'}), 404

def setup_chat_sockets(socketio, session_storage, log_message):
@socketio.on('join')
def handle_join(data):
Expand Down
66 changes: 50 additions & 16 deletions test/socket-meeting.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ const SERVER_URL = 'http://localhost:5000';
const CREATE_MEETING_URL = `${SERVER_URL}/api/create-meeting`;

describe('Socket.IO meeting tests', function() {
this.timeout(2000);

let meetingId;
let socket;
let socket1;
let socket2;

beforeEach(async function() {
Expand All @@ -26,46 +28,78 @@ describe('Socket.IO meeting tests', function() {
expect(meetingId).to.be.a('string'); // Check that meetingId is a string

// Connect both clients to the server
socket = io(SERVER_URL);
socket1 = io(SERVER_URL);
socket2 = io(SERVER_URL);

// Wait for both clients to connect before continuing
await Promise.all([
new Promise((resolve) => socket.on('connect', resolve)),
new Promise((resolve) => socket1.on('connect', resolve)),
new Promise((resolve) => socket2.on('connect', resolve))
]);
});

it('should join the meeting and handle user_joined event', function(done) {
socket.emit('join', { meeting_id: meetingId, username: 'User2' });
it('user1 joins meeting and handles user_joined event', function(done) {
socket1.emit('join', { meeting_id: meetingId, username: 'User1' });

socket.on('user_joined', (data) => {
socket1.on('user_joined', (data) => {
expect(data).to.have.property('username');
expect(data.username).to.equal('User2');
expect(data.username).to.equal('User1');
expect(data.meeting_id).to.include(meetingId);
done();
});
});

it('should handle chat_message event', function(done) {
it('multiple users should join the meeting and get session info', async function() {
socket1.emit('join', { meeting_id: meetingId, username: 'User1' });
socket2.emit('join', { meeting_id: meetingId, username: 'User2' });

await Promise.all([
new Promise((resolve) => socket1.on('user_joined', resolve)),
new Promise((resolve) => socket2.on('user_joined', resolve))
]);

console.log('Waiting for session info...');
fetch(`${SERVER_URL}/api/session/${meetingId}`)
.then((response) => response.json())
.then((data) => {
expect(data).to.have.property('host');
expect(data.host).to.equal('User1');

expect(data).to.have.property('users');
expect(data.users).to.include('User1');
expect(data.users).to.include('User2');

expect(data).to.have.property('chat_history');
resolve();
});
});

it('should handle chat_message event', async function() {
const testMessage = 'Hello, world!';

// socket.emit('join', { meeting_id: meetingId, username: 'User1' });
socket1.emit('join', { meeting_id: meetingId, username: 'User1' });
socket2.emit('join', { meeting_id: meetingId, username: 'User2' });

await Promise.all([
new Promise((resolve) => socket1.on('user_joined', resolve)),
new Promise((resolve) => socket2.on('user_joined', resolve))
])

// Emit a chat message from the first client
socket.emit('chat_message', { meeting_id: meetingId, sender: 'User1', text: testMessage });
socket1.emit('chat_message', { meeting_id: meetingId, sender: 'User1', text: testMessage });

// Listen for chat_message event on the second client
socket2.on('chat_message', (data) => {
expect(data).to.have.property('sender', 'User1');
expect(data).to.have.property('text', testMessage);
done();
});
await new Promise((resolve) =>
socket2.on('chat_message', (data) => {
expect(data).to.have.property('sender', 'User1');
expect(data).to.have.property('text', testMessage);
resolve();
})
);
});

afterEach(function() {
if (socket) socket.disconnect();
if (socket1) socket1.disconnect();
if (socket2) socket2.disconnect();
});
});

0 comments on commit 2117ba3

Please sign in to comment.