refactor read/write of structs

This commit is contained in:
Kevin Jahns
2019-04-07 23:08:08 +02:00
parent 90b3fa9dd9
commit 7a111de186
19 changed files with 418 additions and 304 deletions

36
tests/encoding.tests.js Normal file
View File

@@ -0,0 +1,36 @@
import * as t from 'lib0/testing.js'
import {
structRefs,
structGCRefNumber,
structBinaryRefNumber,
structDeletedRefNumber,
structEmbedRefNumber,
structFormatRefNumber,
structJSONRefNumber,
structStringRefNumber,
structTypeRefNumber,
GCRef,
ItemBinaryRef,
ItemDeletedRef,
ItemEmbedRef,
ItemFormatRef,
ItemJSONRef,
ItemStringRef,
ItemTypeRef
} from '../src/internals.js'
/**
* @param {t.TestCase} tc
*/
export const testStructReferences = tc => {
t.assert(structRefs.length === 8)
t.assert(structRefs[structGCRefNumber] === GCRef)
t.assert(structRefs[structBinaryRefNumber] === ItemBinaryRef)
t.assert(structRefs[structDeletedRefNumber] === ItemDeletedRef)
t.assert(structRefs[structEmbedRefNumber] === ItemEmbedRef)
t.assert(structRefs[structFormatRefNumber] === ItemFormatRef)
t.assert(structRefs[structJSONRefNumber] === ItemJSONRef)
t.assert(structRefs[structStringRefNumber] === ItemStringRef)
t.assert(structRefs[structTypeRefNumber] === ItemTypeRef)
}

View File

@@ -3,6 +3,7 @@ import * as array from './y-array.tests.js'
import * as map from './y-map.tests.js'
import * as text from './y-text.tests.js'
import * as xml from './y-xml.tests.js'
import * as encoding from './encoding.tests.js'
import { runTests } from 'lib0/testing.js'
import { isBrowser, isNode } from 'lib0/environment.js'
@@ -12,7 +13,7 @@ if (isBrowser) {
log.createVConsole(document.body)
}
runTests({
map, array, text, xml
map, array, text, xml, encoding
}).then(success => {
/* istanbul ignore next */
if (isNode) {

View File

@@ -81,14 +81,14 @@ export class TestYInstance extends Y.Y {
if (!this.tc.onlineConns.has(this)) {
this.tc.onlineConns.add(this)
const encoder = encoding.createEncoder()
syncProtocol.writeSyncStep1(encoder, this)
syncProtocol.writeSyncStep1(encoder, this.store)
// publish SyncStep1
broadcastMessage(this, encoding.toBuffer(encoder))
this.tc.onlineConns.forEach(remoteYInstance => {
if (remoteYInstance !== this) {
// remote instance sends instance to this instance
const encoder = encoding.createEncoder()
syncProtocol.writeSyncStep1(encoder, remoteYInstance)
syncProtocol.writeSyncStep1(encoder, remoteYInstance.store)
this._receive(encoding.toBuffer(encoder), remoteYInstance)
}
})
@@ -271,8 +271,11 @@ export const compare = users => {
const userMapValues = users.map(u => u.getMap('map').toJSON())
const userXmlValues = users.map(u => u.get('xml', Y.XmlElement).toString())
const userTextValues = users.map(u => u.getText('text').toDelta())
for (var i = 0; i < users.length - 1; i++) {
t.describe(`Comparing user${i} with user${i + 1}`)
for (const u of users) {
t.assert(u.store.pendingDeleteReaders.length === 0)
t.assert(u.store.pendingStructReaders.size === 0)
}
for (let i = 0; i < users.length - 1; i++) {
t.compare(userArrayValues[i].length, users[i].getArray('array').length)
t.compare(userArrayValues[i], userArrayValues[i + 1])
t.compare(userMapValues[i], userMapValues[i + 1])

View File

@@ -221,7 +221,7 @@ export const testObserveDeepProperties = tc => {
export const testObserversUsingObservedeep = tc => {
const { users, map0 } = init(tc, { users: 2 })
/**
* @type {Array<Array<string>>}
* @type {Array<Array<string|number>>}
*/
const pathes = []
let calls = 0
@@ -348,7 +348,7 @@ const mapTransactions = [
* @param {t.TestCase} tc
*/
export const testRepeatGeneratingYmapTests20 = tc => {
applyRandomTests(tc, mapTransactions, 20)
applyRandomTests(tc, mapTransactions, 8)
}
/**