Remove mutex that was needed by jest

This commit is contained in:
Daniel Lugo 2021-09-23 21:46:41 -04:00
parent c89f3645d8
commit 8a5235461d

View file

@ -16,10 +16,6 @@ if (!fs.existsSync('./test-radata')) {
fs.mkdirSync('./test-radata') fs.mkdirSync('./test-radata')
} }
// start with true, have first test doesn't check, else all other test start to
// run right away
let isBusy = true
const instance = Gun({ const instance = Gun({
axe: false, axe: false,
multicast: false, multicast: false,
@ -30,25 +26,6 @@ const user = instance.user()
const alias = words() const alias = words()
const pass = words() const pass = words()
const release = () => {
isBusy = false
}
/**
* @returns {Promise<void>}
*/
const whenReady = () =>
new Promise(res => {
setTimeout(() => {
if (isBusy) {
whenReady().then(res)
} else {
isBusy = true
res()
}
}, 1000)
})
/** /**
* @param {number} ms * @param {number} ms
*/ */
@ -60,7 +37,6 @@ describe('gun smith', () => {
}) })
it('puts a true and reads it with once()', done => { it('puts a true and reads it with once()', done => {
// await whenReady()
logger.info('puts a true and reads it with once()') logger.info('puts a true and reads it with once()')
const a = words() const a = words()
const b = words() const b = words()
@ -76,7 +52,6 @@ describe('gun smith', () => {
.once(val => { .once(val => {
expect(val).toBe(true) expect(val).toBe(true)
done() done()
release()
}) })
}) })
@ -84,71 +59,61 @@ describe('gun smith', () => {
const a = words() const a = words()
const b = words() const b = words()
whenReady().then(() => { instance
instance .get(a)
.get(a) .get(b)
.get(b) .put(false, ack => {
.put(false, ack => { if (ack.err) {
if (ack.err) { throw new Error(ack.err)
throw new Error(ack.err) } else {
} else { instance
instance .get(a)
.get(a) .get(b)
.get(b) .once(val => {
.once(val => { expect(val).toBe(false)
expect(val).toBe(false) done()
done() })
release() }
}) })
}
})
})
}) })
it('puts numbers and reads them with once()', done => { it('puts numbers and reads them with once()', done => {
const a = words() const a = words()
const b = words() const b = words()
whenReady().then(() => { instance
instance .get(a)
.get(a) .get(b)
.get(b) .put(5)
.put(5)
instance instance
.get(a) .get(a)
.get(b) .get(b)
.once(val => { .once(val => {
expect(val).toBe(5) expect(val).toBe(5)
done() done()
release() })
})
})
}) })
it('puts strings and reads them with once()', done => { it('puts strings and reads them with once()', done => {
const a = words() const a = words()
const b = words() const b = words()
const sentence = words({ exactly: 50 }).join(' ') const sentence = words({ exactly: 50 }).join(' ')
whenReady().then(() => { instance
instance .get(a)
.get(a) .get(b)
.get(b) .put(sentence)
.put(sentence)
instance instance
.get(a) .get(a)
.get(b) .get(b)
.once(val => { .once(val => {
expect(val).toBe(sentence) expect(val).toBe(sentence)
done() done()
release() })
})
})
}) })
it('merges puts', async () => { it('merges puts', async () => {
await whenReady()
const a = { const a = {
a: 1 a: 1
} }
@ -168,75 +133,63 @@ describe('gun smith', () => {
throw new Error('Data not an object') throw new Error('Data not an object')
} }
expect(removeBuiltInGunProps(data)).toEqual(c) expect(removeBuiltInGunProps(data)).toEqual(c)
release()
}) })
it('writes primitive items into sets and correctly assigns the id to ._.get', done => { it('writes primitive items into sets and correctly assigns the id to ._.get', done => {
whenReady().then(() => { const node = instance.get(words()).get(words())
const node = instance.get(words()).get(words()) const item = node.set('hello')
const item = node.set('hello')
node.once(data => { node.once(data => {
expect(removeBuiltInGunProps(data)).toEqual({ expect(removeBuiltInGunProps(data)).toEqual({
[item._.get]: 'hello' [item._.get]: 'hello'
})
done()
release()
}) })
done()
}) })
}) })
// TODO: find out why this test fucks up the previous one if it runs before // TODO: find out why this test fucks up the previous one if it runs before
// that one // that one
it('maps over a primitive set', done => { it('maps over a primitive set', done => {
whenReady().then(() => { const node = instance.get(words()).get(words())
const node = instance.get(words()).get(words())
const items = words({ exactly: 50 }) const items = words({ exactly: 50 })
const ids = items.map(i => node.set(i)._.get) const ids = items.map(i => node.set(i)._.get)
let checked = 0 let checked = 0
node.map().on((data, id) => { node.map().on((data, id) => {
expect(items).toContain(data) expect(items).toContain(data)
expect(ids).toContain(id) expect(ids).toContain(id)
checked++ checked++
if (checked === 50) { if (checked === 50) {
done() done()
release() }
}
})
}) })
}) })
it('maps over an object set', done => { it('maps over an object set', done => {
whenReady().then(() => { const node = instance.get(words()).get(words())
const node = instance.get(words()).get(words())
const items = words({ exactly: 50 }).map(w => ({ const items = words({ exactly: 50 }).map(w => ({
word: w word: w
})) }))
const ids = items.map(i => node.set(i)._.get) const ids = items.map(i => node.set(i)._.get)
let checked = 0 let checked = 0
node.map().on((data, id) => { node.map().on((data, id) => {
expect(items).toContainEqual(removeBuiltInGunProps(data)) expect(items).toContainEqual(removeBuiltInGunProps(data))
expect(ids).toContain(id) expect(ids).toContain(id)
checked++ checked++
if (checked === 50) { if (checked === 50) {
done() done()
release() }
}
})
}) })
}) })
it('offs `on()`s', async () => { it('offs `on()`s', async () => {
await whenReady()
const node = instance.get(words()).get(words()) const node = instance.get(words()).get(words())
let called = false let called = false
@ -250,12 +203,9 @@ describe('gun smith', () => {
await node.pPut('return') await node.pPut('return')
await delay(500) await delay(500)
expect(called).toBe(false) expect(called).toBe(false)
release()
}) })
it('offs `map().on()`s', async () => { it('offs `map().on()`s', async () => {
await whenReady()
const node = instance.get(words()).get(words()) const node = instance.get(words()).get(words())
let called = false let called = false
@ -273,13 +223,9 @@ describe('gun smith', () => {
await delay(500) await delay(500)
expect(called).toBe(false) expect(called).toBe(false)
release()
}) })
it('provides an user node with create(), auth() and leave()', async () => { it('provides an user node with create(), auth() and leave()', async () => {
await whenReady()
const ack = await new Promise(res => user.create(alias, pass, res)) const ack = await new Promise(res => user.create(alias, pass, res))
expect(ack.err).toBeUndefined() expect(ack.err).toBeUndefined()
@ -298,13 +244,9 @@ describe('gun smith', () => {
expect(authAck.sea?.pub).toEqual(pub) expect(authAck.sea?.pub).toEqual(pub)
expect(user.is?.pub).toEqual(pub) expect(user.is?.pub).toEqual(pub)
user.leave() user.leave()
release()
}) })
it('reliably provides authentication information across re-forges', async () => { it('reliably provides authentication information across re-forges', async () => {
await whenReady()
/** @type {GunT.AuthAck} */ /** @type {GunT.AuthAck} */
const authAck = await new Promise(res => const authAck = await new Promise(res =>
user.auth(alias, pass, ack => res(ack)) user.auth(alias, pass, ack => res(ack))
@ -318,12 +260,9 @@ describe('gun smith', () => {
expect(user.is?.pub).toEqual(pub) expect(user.is?.pub).toEqual(pub)
user.leave() user.leave()
release()
}) })
it('provides thenables for values', async () => { it('provides thenables for values', async () => {
await whenReady()
const a = words() const a = words()
const b = words() const b = words()
const node = instance.get(a).get(b) const node = instance.get(a).get(b)
@ -345,13 +284,9 @@ describe('gun smith', () => {
.get(b) .get(b)
.then() .then()
expect(fetch).toEqual(value) expect(fetch).toEqual(value)
release()
}) })
it('provides an special thenable put()', async () => { it('provides an special thenable put()', async () => {
await whenReady()
const a = words() const a = words()
const b = words() const b = words()
const node = instance.get(a).get(b) const node = instance.get(a).get(b)
@ -362,46 +297,41 @@ describe('gun smith', () => {
const res = await node.then() const res = await node.then()
expect(res).toBe(value) expect(res).toBe(value)
release()
}) })
it('on()s and handles object>primitive>object transitions', done => { it('on()s and handles object>primitive>object transitions', done => {
whenReady().then(() => { const a = {
const a = { one: 1
one: 1 }
const b = 'two'
const lastPut = {
three: 3
}
const c = { ...a, ...lastPut }
const node = instance.get(words()).get(words())
let checked = 0
node.on(data => {
checked++
if (checked === 1) {
expect(removeBuiltInGunProps(data)).toEqual(a)
} else if (checked === 2) {
expect(data).toEqual(b)
} else if (checked === 3) {
expect(removeBuiltInGunProps(data)).toEqual(c)
done()
} }
const b = 'two'
const lastPut = {
three: 3
}
const c = { ...a, ...lastPut }
const node = instance.get(words()).get(words())
let checked = 0
node.on(data => {
checked++
if (checked === 1) {
expect(removeBuiltInGunProps(data)).toEqual(a)
} else if (checked === 2) {
expect(data).toEqual(b)
} else if (checked === 3) {
expect(removeBuiltInGunProps(data)).toEqual(c)
done()
release()
}
})
node.put(a)
setTimeout(() => {
node.put(b)
}, 400)
setTimeout(() => {
node.put(c)
}, 800)
}) })
node.put(a)
setTimeout(() => {
node.put(b)
}, 400)
setTimeout(() => {
node.put(c)
}, 800)
}) })
// ************************************************************************** // **************************************************************************
@ -409,46 +339,38 @@ describe('gun smith', () => {
// ************************************************************************** // **************************************************************************
it('writes object items into sets and correctly populates item._.get with the newly created id', done => { it('writes object items into sets and correctly populates item._.get with the newly created id', done => {
whenReady().then(() => { const node = instance.get(words()).get(words())
const node = instance.get(words()).get(words())
const obj = { const obj = {
a: 1, a: 1,
b: 'hello' b: 'hello'
} }
const item = node.set(obj) const item = node.set(obj)
node.get(item._.get).once(data => { node.get(item._.get).once(data => {
expect(removeBuiltInGunProps(data)).toEqual(obj) expect(removeBuiltInGunProps(data)).toEqual(obj)
done() done()
release()
})
}) })
}) })
it('provides an special once() that restarts gun until a value is fetched', done => { it('provides an special once() that restarts gun until a value is fetched', done => {
whenReady().then(() => { const a = words()
const a = words() const b = words()
const b = words() const node = instance.get(a).get(b)
const node = instance.get(a).get(b) const value = words()
const value = words()
node.specialOnce(data => { node.specialOnce(data => {
expect(data).toEqual(value) expect(data).toEqual(value)
done() done()
release()
})
setTimeout(() => {
node.put(value)
}, 30000)
}) })
setTimeout(() => {
node.put(value)
}, 30000)
}) })
it('provides an special then() that restarts gun until a value is fetched', async () => { it('provides an special then() that restarts gun until a value is fetched', async () => {
await whenReady()
const a = words() const a = words()
const b = words() const b = words()
const node = instance.get(a).get(b) const node = instance.get(a).get(b)
@ -461,32 +383,27 @@ describe('gun smith', () => {
const res = await node.specialThen() const res = await node.specialThen()
expect(res).toBe(value) expect(res).toBe(value)
release()
}) })
it('provides an special on() that restarts gun when a value has not been obtained in a determinate amount of time', done => { it('provides an special on() that restarts gun when a value has not been obtained in a determinate amount of time', done => {
// Kinda crappy test, this should be easier to test in real usage. // Kinda crappy test, this should be easier to test in real usage.
whenReady().then(() => { const initialProcCounter = Gun._getProcCounter()
const initialProcCounter = Gun._getProcCounter()
const node = instance.get(words()).get(words()) const node = instance.get(words()).get(words())
const secondValue = words() const secondValue = words()
node.specialOn( node.specialOn(
debounce(data => { debounce(data => {
if (data === secondValue) { if (data === secondValue) {
expect(Gun._getProcCounter()).toEqual(initialProcCounter + 1) expect(Gun._getProcCounter()).toEqual(initialProcCounter + 1)
done() done()
release() }
} })
}) )
)
setTimeout(() => { setTimeout(() => {
node.put(secondValue) node.put(secondValue)
}, 32000) }, 32000)
})
}) })
}) })