|
| 1 | +/* eslint max-nested-callbacks: ["error", 8] */ |
| 2 | +/* eslint-env mocha */ |
| 3 | +'use strict' |
| 4 | + |
| 5 | +const { Buffer } = require('buffer') |
| 6 | +const { expect } = require('./utils/chai') |
| 7 | +const range = require('just-range') |
| 8 | +const Key = require('interface-datastore').Key |
| 9 | + |
| 10 | +module.exports = (repo) => { |
| 11 | + describe('pins', () => { |
| 12 | + const dataList = range(100).map((i) => Buffer.from(`hello-${i}-${Math.random()}`)) |
| 13 | + const data = Buffer.from('hello world') |
| 14 | + const b = new Key('hello') |
| 15 | + |
| 16 | + it('exists', () => { |
| 17 | + expect(repo).to.have.property('pins') |
| 18 | + }) |
| 19 | + |
| 20 | + describe('.put', () => { |
| 21 | + it('simple', async () => { |
| 22 | + await repo.pins.put(b, data) |
| 23 | + }) |
| 24 | + |
| 25 | + it('multi write (locks)', async () => { |
| 26 | + await Promise.all([repo.pins.put(b, data), repo.pins.put(b, data)]) |
| 27 | + }) |
| 28 | + |
| 29 | + it('massive multiwrite', async function () { |
| 30 | + this.timeout(15000) // add time for ci |
| 31 | + await Promise.all(range(100).map((i) => { |
| 32 | + return repo.pins.put(new Key('hello' + i), dataList[i]) |
| 33 | + })) |
| 34 | + }) |
| 35 | + }) |
| 36 | + |
| 37 | + describe('.get', () => { |
| 38 | + it('simple', async () => { |
| 39 | + const val = await repo.pins.get(b) |
| 40 | + expect(val).to.be.eql(data) |
| 41 | + }) |
| 42 | + |
| 43 | + it('massive read', async function () { |
| 44 | + this.timeout(15000) // add time for ci |
| 45 | + await Promise.all(range(20 * 100).map(async (i) => { |
| 46 | + const j = i % dataList.length |
| 47 | + const val = await repo.pins.get(new Key('hello' + j)) |
| 48 | + expect(val).to.be.eql(dataList[j]) |
| 49 | + })) |
| 50 | + }).timeout(10 * 1000) |
| 51 | + }) |
| 52 | + |
| 53 | + describe('.has', () => { |
| 54 | + it('existing pin', async () => { |
| 55 | + const exists = await repo.pins.has(b) |
| 56 | + expect(exists).to.eql(true) |
| 57 | + }) |
| 58 | + |
| 59 | + it('non existent pin', async () => { |
| 60 | + const exists = await repo.pins.has(new Key('world')) |
| 61 | + expect(exists).to.eql(false) |
| 62 | + }) |
| 63 | + }) |
| 64 | + |
| 65 | + describe('.delete', () => { |
| 66 | + it('simple', async () => { |
| 67 | + await repo.pins.delete(b) |
| 68 | + const exists = await repo.pins.has(b) |
| 69 | + expect(exists).to.equal(false) |
| 70 | + }) |
| 71 | + }) |
| 72 | + }) |
| 73 | +} |
0 commit comments