-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PresenceMap: only emit a leave if a member was present
- Loading branch information
1 parent
e94ddbd
commit c28f836
Showing
3 changed files
with
50 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
'use strict'; | ||
|
||
define(['chai', 'ably'], function (chai, Ably) { | ||
const { assert } = chai; | ||
const PresenceMap = Ably.Realtime._PresenceMap; | ||
|
||
describe('PresenceMap', () => { | ||
let presenceMap; | ||
|
||
// Helper function to create a presence message | ||
const createPresenceMessage = (clientId, connectionId, action, timestamp) => ({ | ||
clientId, | ||
connectionId, | ||
timestamp, | ||
action, | ||
}); | ||
|
||
beforeEach(() => { | ||
// Initialize with a simple memberKey function that uses clientId as the key | ||
presenceMap = new PresenceMap( | ||
null, | ||
(item) => item.clientId + ':' + item.connectionId, | ||
(i, j) => i.timestamp > j.timestamp, | ||
); | ||
}); | ||
|
||
describe('remove()', () => { | ||
it('should return false when no matching member present', () => { | ||
const incoming = createPresenceMessage('client1', 'conn1', 'leave', 100); | ||
assert.isFalse(presenceMap.remove(incoming)); | ||
}); | ||
|
||
it('should return true when removing an (older) matching member', () => { | ||
const original = createPresenceMessage('client1', 'conn1', 'present', 100); | ||
presenceMap.put(original); | ||
const incoming = createPresenceMessage('client1', 'conn1', 'leave', 150); | ||
assert.isTrue(presenceMap.remove(incoming)); | ||
}); | ||
|
||
it('should return false when trying to remove a newer matching member', () => { | ||
const original = createPresenceMessage('client1', 'conn1', 'present', 100); | ||
presenceMap.put(original); | ||
const incoming = createPresenceMessage('client1', 'conn1', 'leave', 50); | ||
assert.isFalse(presenceMap.remove(incoming)); | ||
}); | ||
}); | ||
}); | ||
}); |