-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathsession-store.test.js
More file actions
200 lines (147 loc) · 6.43 KB
/
Copy pathsession-store.test.js
File metadata and controls
200 lines (147 loc) · 6.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
const test = require('node:test');
const assert = require('node:assert/strict');
const { createSessionStore } = require('./session-store');
test('createSession mints a fresh session with the expected shape', () => {
const store = createSessionStore({ now: () => 1000 });
const { id, session } = store.createSession();
assert.equal(typeof id, 'string');
assert.ok(id.length > 0);
assert.deepEqual(session.requestLog, []);
assert.deepEqual(session.feedItems, {});
assert.deepEqual(session.webSubSecrets, {});
assert.equal(session.sockets.size, 0);
assert.equal(session.createdAt, 1000);
assert.equal(session.lastOutgoingAt, 1000);
});
test('get returns the session created under an id, or undefined for an unknown id', () => {
const store = createSessionStore({ now: () => 1000 });
const { id, session } = store.createSession();
assert.equal(store.get(id), session);
assert.equal(store.get('unknown-id'), undefined);
});
test('getOrCreate creates a session under an unknown id, then returns the same session on repeat calls', () => {
const store = createSessionStore({ now: () => 1000 });
const first = store.getOrCreate('bookmarked-id');
const second = store.getOrCreate('bookmarked-id');
assert.equal(first, second);
assert.equal(store.get('bookmarked-id'), first);
});
test('touchOutgoing updates lastOutgoingAt to the current time', () => {
let currentTime = 1000;
const store = createSessionStore({ now: () => currentTime });
const { id, session } = store.createSession();
currentTime = 5000;
store.touchOutgoing(id);
assert.equal(session.lastOutgoingAt, 5000);
});
test('touchOutgoing is a safe no-op for an unknown id', () => {
const store = createSessionStore({ now: () => 1000 });
assert.doesNotThrow(() => store.touchOutgoing('unknown-id'));
});
test('isIdle is true for an unknown id', () => {
const store = createSessionStore({ now: () => 1000 });
assert.equal(store.isIdle('unknown-id', 500), true);
});
test('isIdle boundary: exactly at the threshold is not idle, one past it is', () => {
let currentTime = 1000;
const store = createSessionStore({ now: () => currentTime });
const { id } = store.createSession();
currentTime = 1000 + 500;
assert.equal(store.isIdle(id, 500), false);
currentTime = 1000 + 501;
assert.equal(store.isIdle(id, 500), true);
});
test('isIdle is false while the session has a live socket, however long since lastOutgoingAt', () => {
let currentTime = 1000;
const store = createSessionStore({ now: () => currentTime });
const { id, session } = store.createSession();
session.sockets.add({});
currentTime = 1000 + 501;
assert.equal(store.isIdle(id, 500), false);
});
test('sweep evicts only sessions idle beyond the threshold', () => {
let currentTime = 1000;
const store = createSessionStore({ now: () => currentTime });
const stale = store.createSession();
currentTime = 1000 + 500;
const fresh = store.createSession();
currentTime = 1000 + 501;
const evicted = store.sweep(500);
assert.equal(evicted, 1);
assert.equal(store.get(stale.id), undefined);
assert.equal(store.get(fresh.id), fresh.session);
});
test('sweep does not evict a session that has a live socket, however long since lastOutgoingAt', () => {
let currentTime = 1000;
const store = createSessionStore({ now: () => currentTime });
const { id, session } = store.createSession();
session.sockets.add({});
currentTime = 1000 + 501;
const evicted = store.sweep(500);
assert.equal(evicted, 0);
assert.equal(store.get(id), session);
});
const EIGHT_DAYS_MS = 8 * 24 * 60 * 60 * 1000;
test('isIdle stops exempting a live-socket session once it exceeds the absolute max session age', () => {
let currentTime = 1000;
const store = createSessionStore({ now: () => currentTime });
const { id, session } = store.createSession();
session.sockets.add({});
currentTime = 1000 + EIGHT_DAYS_MS;
assert.equal(store.isIdle(id, 500), true);
});
test('isIdle still returns false past the absolute max session age given recent outgoing activity', () => {
let currentTime = 1000;
const store = createSessionStore({ now: () => currentTime });
const { id, session } = store.createSession();
session.sockets.add({});
currentTime = 1000 + EIGHT_DAYS_MS;
store.touchOutgoing(id);
assert.equal(store.isIdle(id, 500), false);
});
test('sweep evicts a long-lived socket-holding session once it exceeds the absolute max session age, terminating its socket', () => {
let currentTime = 1000;
const store = createSessionStore({ now: () => currentTime });
const { id, session } = store.createSession();
let terminated = false;
session.sockets.add({ terminate: () => { terminated = true; } });
currentTime = 1000 + EIGHT_DAYS_MS;
const evicted = store.sweep(500);
assert.equal(evicted, 1);
assert.equal(store.get(id), undefined);
assert.equal(terminated, true);
});
test('appendLog unshifts an entry into the session\'s requestLog, newest-first', () => {
const store = createSessionStore({ now: () => 1000 });
const { id, session } = store.createSession();
store.appendLog(id, { id: '1' });
store.appendLog(id, { id: '2' });
assert.deepEqual(session.requestLog, [{ id: '2' }, { id: '1' }]);
});
test('appendLog is a safe no-op for an unknown id', () => {
const store = createSessionStore({ now: () => 1000 });
assert.doesNotThrow(() => store.appendLog('unknown-id', { id: '1' }));
});
test('appendLog caps the requestLog at 100 entries, dropping the oldest', () => {
const store = createSessionStore({ now: () => 1000 });
const { id, session } = store.createSession();
for (let i = 0; i < 101; i++) {
store.appendLog(id, { id: String(i) });
}
assert.equal(session.requestLog.length, 100);
assert.equal(session.requestLog[0].id, '100');
assert.equal(session.requestLog[99].id, '1');
});
test('size reflects the number of live sessions, including after a sweep', () => {
let currentTime = 1000;
const store = createSessionStore({ now: () => currentTime });
assert.equal(store.size(), 0);
const stale = store.createSession();
currentTime = 1000 + 500;
store.createSession();
assert.equal(store.size(), 2);
currentTime = 1000 + 501;
store.sweep(500);
assert.equal(store.size(), 1);
assert.equal(store.get(stale.id), undefined);
});