change mock event api a bit to be easier to read

This commit is contained in:
Bruno Windels 2021-06-02 18:38:16 +02:00
parent 09bf0f3b4e
commit d8acf63e1d
2 changed files with 13 additions and 9 deletions

View file

@ -315,7 +315,7 @@ export class SendQueue {
import {HomeServer as MockHomeServer} from "../../../mocks/HomeServer.js"; import {HomeServer as MockHomeServer} from "../../../mocks/HomeServer.js";
import {createMockStorage} from "../../../mocks/Storage.js"; import {createMockStorage} from "../../../mocks/Storage.js";
import {NullLogger} from "../../../logging/NullLogger.js"; import {NullLogger} from "../../../logging/NullLogger.js";
import {event, withTextBody, withTxnId} from "../../../mocks/event.js"; import {createEvent, withTextBody, withTxnId} from "../../../mocks/event.js";
import {poll} from "../../../mocks/poll.js"; import {poll} from "../../../mocks/poll.js";
export function tests() { export function tests() {
@ -326,12 +326,12 @@ export function tests() {
const hs = new MockHomeServer(); const hs = new MockHomeServer();
// 1. enqueue and start send event 1 // 1. enqueue and start send event 1
const queue = new SendQueue({roomId: "!abc", storage, hsApi: hs.api}); const queue = new SendQueue({roomId: "!abc", storage, hsApi: hs.api});
const event1 = withTextBody(event("m.room.message", "$123"), "message 1"); const event1 = withTextBody("message 1", createEvent("m.room.message", "$123"));
await logger.run("event1", log => queue.enqueueEvent(event1.type, event1.content, null, log)); await logger.run("event1", log => queue.enqueueEvent(event1.type, event1.content, null, log));
assert.equal(queue.pendingEvents.length, 1); assert.equal(queue.pendingEvents.length, 1);
const sendRequest1 = hs.requests.send[0]; const sendRequest1 = hs.requests.send[0];
// 2. receive remote echo, before /send has returned // 2. receive remote echo, before /send has returned
const remoteEcho = withTxnId(event1, sendRequest1.arguments[2]); const remoteEcho = withTxnId(sendRequest1.arguments[2], event1);
const txn = await storage.readWriteTxn([storage.storeNames.pendingEvents]); const txn = await storage.readWriteTxn([storage.storeNames.pendingEvents]);
const removal = await logger.run("remote echo", log => queue.removeRemoteEchos([remoteEcho], txn, log)); const removal = await logger.run("remote echo", log => queue.removeRemoteEchos([remoteEcho], txn, log));
await txn.complete(); await txn.complete();
@ -339,7 +339,7 @@ export function tests() {
queue.emitRemovals(removal); queue.emitRemovals(removal);
assert.equal(queue.pendingEvents.length, 0); assert.equal(queue.pendingEvents.length, 0);
// 3. now enqueue event 2 // 3. now enqueue event 2
const event2 = withTextBody(event("m.room.message", "$456"), "message 2"); const event2 = withTextBody("message 2", createEvent("m.room.message", "$456"));
await logger.run("event2", log => queue.enqueueEvent(event2.type, event2.content, null, log)); await logger.run("event2", log => queue.enqueueEvent(event2.type, event2.content, null, log));
// even though the first pending event has been removed by the remote echo, // even though the first pending event has been removed by the remote echo,
// the second should get the next index, as the send loop is still blocking on the first one // the second should get the next index, as the send loop is still blocking on the first one

View file

@ -14,18 +14,22 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
export function event(type, id = null) { export function createEvent(type, id = null) {
return {type, event_id: id}; return {type, event_id: id};
} }
export function withContent(event, content) { export function withContent(content, event) {
return Object.assign({}, event, {content}); return Object.assign({}, event, {content});
} }
export function withTextBody(event, body) { export function withSender(sender, event) {
return withContent(event, {body, msgtype: "m.text"}); return Object.assign({}, event, {sender});
} }
export function withTxnId(event, txnId) { export function withTextBody(body, event) {
return withContent({body, msgtype: "m.text"}, event);
}
export function withTxnId(txnId, event) {
return Object.assign({}, event, {unsigned: {transaction_id: txnId}}); return Object.assign({}, event, {unsigned: {transaction_id: txnId}});
} }