Skip to content

Commit

Permalink
feat: implement processing in memory
Browse files Browse the repository at this point in the history
  • Loading branch information
abetomo committed Dec 17, 2021
1 parent f7df06c commit 6e62b33
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 112 deletions.
5 changes: 0 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,6 @@ try {
} catch (err) {
console.error(err)
}

// !!! SimplyImitatedSQS creates a file to store the queue, so remove it.
if (process.env.LOCAL_TEST === '1') {
sqs.clear()
}
```

### Starting http server
Expand Down
5 changes: 0 additions & 5 deletions examples/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,6 @@ Promise.resolve().then(() => {
resolve()
})
})
}).then(() => {
if (process.env.LOCAL_TEST === '1') {
// !!! SimplyImitatedSQS creates a file to store the queue, so remove it.
sqs.clear()
}
})

/*
Expand Down
5 changes: 0 additions & 5 deletions examples/example.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,3 @@ try {
} catch (err) {
console.error(err)
}

// !!! SimplyImitatedSQS creates a file to store the queue, so remove it.
if (process.env.LOCAL_TEST === '1') {
sqs.clear()
}
3 changes: 1 addition & 2 deletions lib/__tests__/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

const SimplyImitatedSQSHttpServer = require('../server')

/* global describe, test, expect, beforeEach, afterEach */
/* global describe, test, expect, beforeEach */
describe('web server', () => {
let server = null
beforeEach(() => {
server = new SimplyImitatedSQSHttpServer()
})
afterEach(() => server.sqs.clear())

test('server is instanceOf SimplyImitatedSQSHttpServer', () => {
expect(server).toBeInstanceOf(SimplyImitatedSQSHttpServer)
Expand Down
51 changes: 1 addition & 50 deletions lib/__tests__/simply_imitated_sqs.js
Original file line number Diff line number Diff line change
@@ -1,67 +1,18 @@
'use strict'

const os = require('os')
const fs = require('fs')
const path = require('path')
const SimplyImitatedSQS = require('../simply_imitated_sqs')

/* global describe, test, expect, beforeEach, afterEach */
/* global describe, test, expect, beforeEach */
describe('simply_imitated_sqs', () => {
let sqs = null
beforeEach(() => {
sqs = new SimplyImitatedSQS()
})
afterEach(() => sqs.clear())

test('sqs is instanceOf SimplyImitatedSQS', () => {
expect(sqs).toBeInstanceOf(SimplyImitatedSQS)
})

describe('defaultDbFile()', () => {
test('File path including pid', () => {
expect(sqs.defaultDbFile())
.toMatch(/SimplyImitatedSQS-\d{1,}\.json/)
})
})

describe('sqs.dbFile variable', () => {
test('File path including pid', () => {
expect(sqs.dbFile)
.toMatch(/SimplyImitatedSQS-\d{1,}\.json/)
})

test('Specify file path at instance creation', () => {
const filePath = path.join(os.tmpdir(), 'SimplyImitatedSQS-test.json')
const _sqs = new SimplyImitatedSQS(filePath)
expect(_sqs.dbFile).toBe(filePath)
_sqs.clear()
})
})

describe('reload() and clear()', () => {
test('Remove the file with clear and delete vsq. Recreate with reload', () => {
const dbFile = sqs.dbFile
expect(fs.existsSync(dbFile)).toBeTruthy()

sqs.clear()
expect(fs.existsSync(dbFile)).toBeFalsy()
expect(sqs.vsq).toBeUndefined()

sqs.reload()
expect(fs.existsSync(dbFile)).toBeTruthy()
expect(sqs.vsq).toBeDefined()
})

test('Exceptions do not occur even if clear() is executed continuously', () => {
const func = () => {
sqs.clear()
sqs.clear()
return true
}
expect(func()).toBeTruthy()
})
})

describe('md5_(str)', () => {
test('value of md5', () => {
expect(sqs.md5_('hoge')).toBe('ea703e7aa1efda0064eaa507d9e8ab7e')
Expand Down
1 change: 0 additions & 1 deletion lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ class SimplyImitatedSQSHttpServer {

close (callback) {
if (this.server == null) return
this.sqs.clear()
if (callback == null) callback = () => process.exit()
return this.server.close(callback)
}
Expand Down
29 changes: 2 additions & 27 deletions lib/simply_imitated_sqs.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,13 @@
'use strict'

const os = require('os')
const fs = require('fs')
const path = require('path')
const crypto = require('crypto')
const EventEmitter = require('events').EventEmitter
const VerySimpleQueueLikeSQS = require('@abetomo/vsq').SQS

class SimplyImitatedSQS {
constructor (dbFile) {
constructor () {
this.vsq = new VerySimpleQueueLikeSQS()
this.dbFile = (() => {
if (dbFile != null) return dbFile
return this.defaultDbFile()
})()
this.vsq.load(this.dbFile)
}

defaultDbFile () {
const getRandomInt = () => Math.floor(Math.random() * 10000000)
return path.join(
os.tmpdir(),
`SimplyImitatedSQS-${getRandomInt()}.json`
)
}

reload () {
this.vsq = new VerySimpleQueueLikeSQS()
this.vsq.load(this.dbFile)
}

clear () {
delete this.vsq
if (fs.existsSync(this.dbFile)) fs.unlinkSync(this.dbFile)
this.vsq.load(':memory:')
}

md5_ (str) {
Expand Down
63 changes: 47 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
},
"homepage": "https://github.com/abetomo/Simply-imitated-SQS-for-testing#readme",
"dependencies": {
"@abetomo/vsq": "^4.0.0"
"@abetomo/vsq": "^4.1.1"
},
"files": [
"LICENSE",
Expand Down

0 comments on commit 6e62b33

Please sign in to comment.