2022-01-07 15:58:30 +01:00
|
|
|
import o from "ospec"
|
2022-02-28 17:23:22 +01:00
|
|
|
import {ConnectMode, ENTITY_EVENT_BATCH_EXPIRE_MS, EventBusClient} from "../../../src/api/worker/EventBusClient"
|
|
|
|
import {GroupType, OperationType} from "../../../src/api/common/TutanotaConstants"
|
2022-01-07 15:58:30 +01:00
|
|
|
import type {EntityUpdate} from "../../../src/api/entities/sys/EntityUpdate"
|
2022-02-28 12:12:01 +01:00
|
|
|
import {createEntityUpdate} from "../../../src/api/entities/sys/EntityUpdate"
|
2022-01-07 15:58:30 +01:00
|
|
|
import {EntityRestClientMock} from "./EntityRestClientMock"
|
|
|
|
import {EntityClient} from "../../../src/api/common/EntityClient"
|
2022-02-28 17:23:22 +01:00
|
|
|
import {defer, delay, noOp} from "@tutao/tutanota-utils"
|
2022-01-07 15:58:30 +01:00
|
|
|
import {WorkerImpl} from "../../../src/api/worker/WorkerImpl"
|
2022-01-13 11:57:55 +01:00
|
|
|
import {LoginFacadeImpl} from "../../../src/api/worker/facades/LoginFacade"
|
2022-02-28 17:23:22 +01:00
|
|
|
import {createUser, User} from "../../../src/api/entities/sys/User"
|
2022-01-07 15:58:30 +01:00
|
|
|
import {createGroupMembership} from "../../../src/api/entities/sys/GroupMembership"
|
|
|
|
import {InstanceMapper} from "../../../src/api/worker/crypto/InstanceMapper"
|
2022-01-12 14:43:01 +01:00
|
|
|
import {EntityRestCache} from "../../../src/api/worker/rest/EntityRestCache"
|
|
|
|
import {QueuedBatch} from "../../../src/api/worker/search/EventQueue"
|
|
|
|
import {OutOfSyncError} from "../../../src/api/common/error/OutOfSyncError"
|
2022-02-28 17:23:22 +01:00
|
|
|
import {explain, matchers, object, verify, when} from "testdouble"
|
2022-02-28 12:12:01 +01:00
|
|
|
import {MailFacade} from "../../../src/api/worker/facades/MailFacade"
|
|
|
|
import {Indexer} from "../../../src/api/worker/search/Indexer"
|
|
|
|
import {createWebsocketEntityData, WebsocketEntityData} from "../../../src/api/entities/sys/WebsocketEntityData"
|
|
|
|
import {createWebsocketCounterData, WebsocketCounterData} from "../../../src/api/entities/sys/WebsocketCounterData"
|
|
|
|
import {createWebsocketCounterValue} from "../../../src/api/entities/sys/WebsocketCounterValue"
|
2022-02-28 17:23:22 +01:00
|
|
|
import {createEntityEventBatch, EntityEventBatchTypeRef} from "../../../src/api/entities/sys/EntityEventBatch"
|
|
|
|
import {getElementId} from "../../../src/api/common/utils/EntityUtils"
|
2022-01-13 11:57:55 +01:00
|
|
|
|
2022-01-07 15:58:30 +01:00
|
|
|
o.spec("EventBusClient test", function () {
|
2022-01-12 14:43:01 +01:00
|
|
|
let ebc: EventBusClient
|
|
|
|
let cacheMock: EntityRestCache
|
|
|
|
let restClient: EntityRestClientMock
|
|
|
|
let workerMock: WorkerImpl
|
|
|
|
let loginMock: LoginFacadeImpl
|
2022-02-28 12:12:01 +01:00
|
|
|
let mailMock: MailFacade
|
|
|
|
let indexerMock: Indexer
|
2022-02-28 17:23:22 +01:00
|
|
|
let socket: WebSocket
|
|
|
|
let user: User
|
|
|
|
|
|
|
|
function initEventBus() {
|
|
|
|
const entityClient = new EntityClient(restClient)
|
|
|
|
const intanceMapper = new InstanceMapper()
|
|
|
|
ebc = new EventBusClient(
|
|
|
|
workerMock,
|
|
|
|
indexerMock,
|
|
|
|
cacheMock,
|
|
|
|
mailMock,
|
|
|
|
loginMock,
|
|
|
|
entityClient,
|
|
|
|
intanceMapper,
|
|
|
|
() => socket,
|
|
|
|
)
|
|
|
|
}
|
2022-02-28 12:12:01 +01:00
|
|
|
|
2022-01-12 14:43:01 +01:00
|
|
|
o.beforeEach(async function () {
|
2022-02-28 12:12:01 +01:00
|
|
|
cacheMock = object({
|
|
|
|
async entityEventsReceived(batch: QueuedBatch): Promise<Array<EntityUpdate>> {
|
|
|
|
return batch.events.slice()
|
2022-01-12 14:43:01 +01:00
|
|
|
},
|
2022-02-28 12:12:01 +01:00
|
|
|
async getLastEntityEventBatchForGroup(groupId: Id): Promise<Id | null> {
|
|
|
|
return null
|
2022-01-12 14:43:01 +01:00
|
|
|
},
|
2022-02-28 17:23:22 +01:00
|
|
|
async recordSyncTime(): Promise<void> {
|
|
|
|
return
|
2022-01-12 14:43:01 +01:00
|
|
|
},
|
2022-02-28 17:23:22 +01:00
|
|
|
async timeSinceLastSync(): Promise<number | null> {
|
|
|
|
return null
|
2022-01-12 14:43:01 +01:00
|
|
|
},
|
2022-02-28 12:12:01 +01:00
|
|
|
async purgeStorage(): Promise<void> {
|
|
|
|
}
|
|
|
|
} as EntityRestCache)
|
2022-01-12 14:43:01 +01:00
|
|
|
|
2022-02-28 17:23:22 +01:00
|
|
|
user = createUser({
|
2022-01-12 14:43:01 +01:00
|
|
|
userGroup: createGroupMembership({
|
|
|
|
group: "userGroupId",
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
|
2022-02-28 12:12:01 +01:00
|
|
|
loginMock = object("login")
|
|
|
|
when(loginMock.entityEventsReceived(matchers.anything())).thenResolve(undefined)
|
|
|
|
when(loginMock.getLoggedInUser()).thenReturn(user)
|
|
|
|
when(loginMock.isLoggedIn()).thenReturn(true)
|
2022-02-28 17:23:22 +01:00
|
|
|
when(loginMock.createAuthHeaders()).thenReturn({})
|
2022-01-12 14:43:01 +01:00
|
|
|
|
2022-02-28 12:12:01 +01:00
|
|
|
mailMock = object("mail")
|
|
|
|
when(mailMock.entityEventsReceived(matchers.anything())).thenResolve(undefined)
|
|
|
|
|
|
|
|
workerMock = object("worker")
|
|
|
|
when(workerMock.entityEventsReceived(matchers.anything(), matchers.anything())).thenResolve(undefined)
|
|
|
|
when(workerMock.updateCounter(matchers.anything())).thenResolve(undefined)
|
|
|
|
when(workerMock.updateWebSocketState(matchers.anything())).thenResolve(undefined)
|
2022-02-28 17:23:22 +01:00
|
|
|
when(workerMock.createProgressMonitor(matchers.anything())).thenResolve(42)
|
|
|
|
when(workerMock.progressWorkDone(matchers.anything(), matchers.anything())).thenResolve(undefined)
|
2022-02-28 12:12:01 +01:00
|
|
|
|
|
|
|
indexerMock = object("indexer")
|
2022-01-12 14:43:01 +01:00
|
|
|
|
|
|
|
restClient = new EntityRestClientMock()
|
|
|
|
|
2022-02-28 17:23:22 +01:00
|
|
|
|
|
|
|
socket = object<WebSocket>()
|
|
|
|
|
|
|
|
initEventBus()
|
2022-01-12 14:43:01 +01:00
|
|
|
})
|
|
|
|
|
2022-02-28 17:23:22 +01:00
|
|
|
o.spec("initEntityEvents ", function () {
|
|
|
|
const mailGroupId = "mailGroupId"
|
|
|
|
|
|
|
|
o.beforeEach(function () {
|
|
|
|
user.memberships = [
|
|
|
|
createGroupMembership({
|
|
|
|
groupType: GroupType.Mail,
|
|
|
|
group: mailGroupId,
|
|
|
|
})
|
|
|
|
]
|
|
|
|
})
|
|
|
|
|
|
|
|
o("initial connect: when the cache is clean it downloads one batch and initializes cache", async function () {
|
|
|
|
when(cacheMock.getLastEntityEventBatchForGroup(mailGroupId)).thenResolve(null)
|
|
|
|
when(cacheMock.timeSinceLastSync()).thenResolve(null)
|
|
|
|
const batch = createEntityEventBatch({_id: [mailGroupId, "-----------1"]})
|
|
|
|
restClient.addListInstances(batch)
|
|
|
|
|
|
|
|
await ebc.connect(ConnectMode.Initial)
|
|
|
|
await socket.onopen?.(new Event("open"))
|
|
|
|
|
|
|
|
// Did not download anything besides single batch
|
|
|
|
verify(restClient.loadRange(EntityEventBatchTypeRef, mailGroupId, matchers.anything(), matchers.not(1), matchers.anything()), {times: 0})
|
|
|
|
verify(cacheMock.recordSyncTime())
|
|
|
|
// TODO: cache is initialized with batch id here
|
|
|
|
})
|
|
|
|
|
|
|
|
o("initial connect: when the cache is initialized, missed events are loaded", async function () {
|
|
|
|
when(cacheMock.getLastEntityEventBatchForGroup(mailGroupId)).thenResolve("------------")
|
|
|
|
when(cacheMock.timeSinceLastSync()).thenResolve(1)
|
|
|
|
const update = createEntityUpdate({
|
|
|
|
type: "Mail",
|
|
|
|
application: "tutanota",
|
|
|
|
instanceListId: mailGroupId,
|
|
|
|
instanceId: "newBatchId",
|
|
|
|
})
|
|
|
|
const batch = createEntityEventBatch({
|
|
|
|
_id: [mailGroupId, "-----------1"],
|
|
|
|
events: [update],
|
|
|
|
})
|
|
|
|
restClient.addListInstances(batch)
|
|
|
|
|
|
|
|
const eventsReceivedDefer = defer()
|
|
|
|
when(cacheMock.entityEventsReceived({events: [update], batchId: getElementId(batch), groupId: mailGroupId}))
|
|
|
|
.thenDo(() => eventsReceivedDefer.resolve(undefined))
|
|
|
|
|
|
|
|
await ebc.connect(ConnectMode.Initial)
|
|
|
|
await socket.onopen?.(new Event("open"))
|
|
|
|
|
|
|
|
await eventsReceivedDefer.promise
|
|
|
|
|
|
|
|
verify(cacheMock.purgeStorage(), {times: 0})
|
|
|
|
verify(cacheMock.recordSyncTime())
|
|
|
|
})
|
|
|
|
|
|
|
|
o("reconnect: when the cache is out of sync with the server, the cache is purged", async function () {
|
|
|
|
when(cacheMock.getLastEntityEventBatchForGroup(mailGroupId)).thenResolve("lastBatchId")
|
|
|
|
// Make initial connection to simulate reconnect (populate lastEntityEventIds
|
|
|
|
await ebc.connect(ConnectMode.Initial)
|
|
|
|
await socket.onopen?.(new Event("open"))
|
|
|
|
|
|
|
|
// Make it think that it's actually a reconnect
|
|
|
|
when(cacheMock.timeSinceLastSync()).thenResolve(ENTITY_EVENT_BATCH_EXPIRE_MS + 100)
|
|
|
|
|
|
|
|
// initialize events first as well as current time
|
|
|
|
await ebc.connect(ConnectMode.Reconnect)
|
|
|
|
await socket.onopen?.(new Event("open"))
|
|
|
|
|
2022-02-28 12:12:01 +01:00
|
|
|
verify(cacheMock.purgeStorage(), {times: 1})
|
2022-02-28 17:23:22 +01:00
|
|
|
verify(workerMock.sendError(matchers.isA(OutOfSyncError)))
|
|
|
|
})
|
|
|
|
|
|
|
|
o("initial connect: when the cache is out of sync with the server, the cache is purged", async function () {
|
|
|
|
when(cacheMock.getLastEntityEventBatchForGroup(mailGroupId)).thenResolve("lastBatchId")
|
|
|
|
when(cacheMock.timeSinceLastSync()).thenResolve(ENTITY_EVENT_BATCH_EXPIRE_MS + 100)
|
|
|
|
|
|
|
|
await ebc.connect(ConnectMode.Reconnect)
|
|
|
|
await socket.onopen?.(new Event("open"))
|
|
|
|
|
|
|
|
verify(cacheMock.purgeStorage(), {times: 1})
|
|
|
|
verify(workerMock.sendError(matchers.isA(OutOfSyncError)))
|
2022-01-12 14:43:01 +01:00
|
|
|
})
|
|
|
|
})
|
2022-02-28 17:23:22 +01:00
|
|
|
|
2022-02-28 12:12:01 +01:00
|
|
|
o("parallel received event batches are passed sequentially to the entity rest cache", async function () {
|
2022-01-12 14:43:01 +01:00
|
|
|
o.timeout(500)
|
2022-02-28 17:23:22 +01:00
|
|
|
ebc.connect(ConnectMode.Initial)
|
|
|
|
await socket.onopen?.(new Event("open"))
|
2022-01-12 14:43:01 +01:00
|
|
|
|
2022-02-28 17:23:22 +01:00
|
|
|
const messageData1 = createEntityMessage(1)
|
|
|
|
const messageData2 = createEntityMessage(2)
|
2022-01-12 14:43:01 +01:00
|
|
|
|
2022-02-28 12:12:01 +01:00
|
|
|
// Casting ot object here because promise stubber doesn't allow you to just return the promise
|
|
|
|
// We never resolve the promise
|
|
|
|
when(cacheMock.entityEventsReceived(matchers.anything()) as object).thenReturn(new Promise(noOp))
|
2022-01-12 14:43:01 +01:00
|
|
|
|
|
|
|
|
|
|
|
// call twice as if it was received in parallel
|
2022-02-28 17:23:22 +01:00
|
|
|
const p1 = socket.onmessage?.({
|
|
|
|
data: messageData1,
|
|
|
|
} as MessageEvent<string>)
|
2022-01-12 14:43:01 +01:00
|
|
|
|
2022-02-28 17:23:22 +01:00
|
|
|
const p2 = socket.onmessage?.(
|
2022-02-28 15:19:23 +01:00
|
|
|
{
|
2022-01-12 14:43:01 +01:00
|
|
|
data: messageData2,
|
2022-02-28 15:19:23 +01:00
|
|
|
} as MessageEvent<string>,
|
2022-01-12 14:43:01 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
await Promise.all([p1, p2])
|
2022-02-28 12:12:01 +01:00
|
|
|
|
|
|
|
// Is waiting for cache to process the first event
|
|
|
|
verify(cacheMock.entityEventsReceived(matchers.anything()), {times: 1})
|
|
|
|
},
|
2022-01-12 14:43:01 +01:00
|
|
|
)
|
2022-02-28 12:12:01 +01:00
|
|
|
|
|
|
|
o("counter update", async function () {
|
2022-02-28 17:23:22 +01:00
|
|
|
const counterUpdate = createCounterData({mailGroupId: "group1", counterValue: 4, listId: "list1"})
|
|
|
|
await ebc.connect(ConnectMode.Initial)
|
2022-01-12 14:43:01 +01:00
|
|
|
|
2022-02-28 17:23:22 +01:00
|
|
|
await socket.onmessage?.(
|
2022-02-28 12:12:01 +01:00
|
|
|
{
|
|
|
|
data: createCounterMessage(counterUpdate),
|
|
|
|
} as MessageEvent,
|
2022-01-12 14:43:01 +01:00
|
|
|
)
|
2022-02-28 12:12:01 +01:00
|
|
|
verify(workerMock.updateCounter(counterUpdate))
|
|
|
|
},
|
2022-01-12 14:43:01 +01:00
|
|
|
)
|
|
|
|
|
2022-02-28 17:23:22 +01:00
|
|
|
function createEntityMessage(eventBatchId: number): string {
|
2022-02-28 12:12:01 +01:00
|
|
|
const event: WebsocketEntityData = createWebsocketEntityData({
|
|
|
|
eventBatchId: String(eventBatchId),
|
|
|
|
eventBatchOwner: "ownerId",
|
|
|
|
eventBatch: [
|
|
|
|
createEntityUpdate({
|
|
|
|
_id: "eventbatchid",
|
|
|
|
application: "tutanota",
|
|
|
|
type: "Mail",
|
|
|
|
instanceListId: "listId1",
|
|
|
|
instanceId: "id1",
|
|
|
|
operation: OperationType.UPDATE,
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
}
|
|
|
|
)
|
2022-01-12 14:43:01 +01:00
|
|
|
return "entityUpdate;" + JSON.stringify(event)
|
|
|
|
}
|
|
|
|
|
2022-02-28 12:12:01 +01:00
|
|
|
type CounterMessageParams = {mailGroupId: Id, counterValue: number, listId: Id}
|
|
|
|
|
|
|
|
function createCounterData({mailGroupId, counterValue, listId}: CounterMessageParams): WebsocketCounterData {
|
|
|
|
return createWebsocketCounterData({
|
|
|
|
_format: "0",
|
|
|
|
mailGroup: mailGroupId,
|
|
|
|
counterValues: [
|
|
|
|
createWebsocketCounterValue({
|
|
|
|
_id: "counterupdateid",
|
|
|
|
count: String(counterValue),
|
|
|
|
mailListId: listId,
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function createCounterMessage(event: WebsocketCounterData): string {
|
2022-01-12 14:43:01 +01:00
|
|
|
return "unreadCounterUpdate;" + JSON.stringify(event)
|
|
|
|
}
|
2022-01-07 15:58:30 +01:00
|
|
|
})
|